Dark Bit Factory & Gravity

PROGRAMMING => General coding questions => Topic started by: energy on September 16, 2007

Title: Math for a bouncing ball...
Post by: energy on September 16, 2007
Hi...
Did anybody coded a bouncing ball...
Not all directiones ...
Simply falling straight down.... and bouncing out...

Thanks a lot....
Title: Re: Math for a bouncing ball...
Post by: psygate on September 16, 2007
ok, you need some variables:

ay, the acceleration in direction to the ground (alway positive)
ax, the acceleration with which the ball's moving in x direction (neg. or positive)
vy, the velocity in y direction
vx, the velocitiy in x direction

now that you have your varialbes, just use them!

vx+=ax
vy+=ay

And if the ball hits the ground, just mulitply vy with -1 so (if vx=ground then vx*=-1)

excuse my shitty text, but I just had an migraine attack and haven't recovered yet...

If you have any more questions, just post.
Title: Re: Math for a bouncing ball...
Post by: energy on September 16, 2007
Thank yu !!!!
will test.... :cheers:
Title: Re: Math for a bouncing ball...
Post by: bikemadness on September 17, 2007
I used it as a basis for a scrollcomp competition entry, here.

http://dbfinteractive.com/index.php?topic=1357.0 (http://dbfinteractive.com/index.php?topic=1357.0)

Have a Yahappy day.
Title: Re: Math for a bouncing ball...
Post by: Rbz on September 17, 2007
You can find more info about bouncing here:

http://freespace.virgin.net/hugo.elias/models/m_bounce.htm
Title: Re: Math for a bouncing ball...
Post by: energy on September 18, 2007
Thank yu all!!!!
 :cheers:
Title: Re: Math for a bouncing ball...
Post by: rain_storm on September 18, 2007
While we are on the subject of bouncing balls, does anyone here know how to properly detect collisions between a circle and a rectangle? I used the old 'if distance to surface of box is less than radius' check but this meathod falls short when the circle collides with the corner of the box since it will only allow the circle to collide with only a single surface. This is something which has had me baffled for a long time and I havent been able to come up with a solution
Title: Re: Math for a bouncing ball...
Post by: madsravn on September 19, 2007
Hi,

I have never done collision detection between circle and rectangle before. But when I read your question, it struck me - Can't you describe your rectangle as a area with four straight lines as borders? And then you can check whether the circle hits any of your lines. You get my point? I'm at school right now, so I'm not able to code an example, but if you still have problems tomorrow, I shall code a little sometin sometin for ya :)

Happy coding
Title: Re: Math for a bouncing ball...
Post by: Jim on September 19, 2007
If the box is axis aligned...
First, check to see whether any of the points of the rect is inside the circle, which is easy.
Subtract the centre of the circle from the corner and see if the length of the vector is less than the circle radius.
Next, check if the circle's centre-x is between the box's x coordinates.  Then check to see if box y - circle y is less than radius.  There are 4 edges to check in that way.

Jim
Title: Re: Math for a bouncing ball...
Post by: rain_storm on September 20, 2007
That sounds like the way to go about it I figured the corners would need to be included in the routine you cant rely on the edges alone. I would rather avoid checking all corners until I am sure that the circle is going to collide with the box. Perhaps only check all vertices within a given range that are also part of a collideable surface
Title: Re: Math for a bouncing ball...
Post by: psygate on September 25, 2007
I'd say, you need two points of the rectangle, make a line-equation between them and check whether the ballradius hits or not... I'll try to code that, don't expect too much by now. :inspired:
Title: Re: Math for a bouncing ball...
Post by: rain_storm on September 26, 2007
Code: [Select]
sub distance(px,py, x1,y1, x2,y2)
  local dx,dy,clip_line
  dx = x2 - x1
  dy = y2 - y1
  clip_line = ((px-x1)*dx + (py-y1)*dy) / (dx*dx + dy*dy)
  closest_point_x = (x1 + clip_line*dx)
  closest_point_y = (y1 + clip_line*dy)
  dx = px - closest_point_x
  dy = py - closest_point_y
  return sqrt(dx*dx + dy*dy)
end sub

that subroutine will calculate the distance between a point and a straight line where
px,py = point x,y
x1,y1,x2,y2 = two points that lie along that line
the subroutine assumes that the line is infinate in length and will return the shortest distance to that infinate line. if the value returned by the subroutine is less than ball radius then closest_point_x and closest_point_y must be used to check if the clostest point is beyond the actual extents of the true line.

Thats actually the easy part the hard part is changing the movement vector of the circle so that the ball bounces off both edges and corners in a realistic manor.
Im thinking that the line of action is the line between the circles center and the edge it collides with, the vector has to be mirrored accross this line somehow, This holds true for colliding with surfaces too but the difference is that the line of action is drawn between the circles center point and the point along the surface that first touches the circle