Author Topic: Math for a bouncing ball...  (Read 5852 times)

0 Members and 1 Guest are viewing this topic.

Offline energy

  • Amiga 1200
  • ****
  • Posts: 280
  • Karma: 25
    • View Profile
Math for a bouncing ball...
« on: September 16, 2007 »
Hi...
Did anybody coded a bouncing ball...
Not all directiones ...
Simply falling straight down.... and bouncing out...

Thanks a lot....
coding: jwasm,masm
hobby: www.scd2003.de

Offline psygate

  • Completly Insane.
  • Atari ST
  • ***
  • Posts: 173
  • Karma: 7
  • That boy needs therapy.
    • View Profile
Re: Math for a bouncing ball...
« Reply #1 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.
« Last Edit: September 16, 2007 by psygate »
He who controlles the minds commands the many.
He who commands the many, conqueres the minds.

Offline energy

  • Amiga 1200
  • ****
  • Posts: 280
  • Karma: 25
    • View Profile
Re: Math for a bouncing ball...
« Reply #2 on: September 16, 2007 »
Thank yu !!!!
will test.... :cheers:
coding: jwasm,masm
hobby: www.scd2003.de

Offline bikemadness

  • Amiga 1200
  • ****
  • Posts: 312
  • Karma: 25
  • Hard players don't go home.
    • View Profile
Re: Math for a bouncing ball...
« Reply #3 on: September 17, 2007 »
I used it as a basis for a scrollcomp competition entry, here.

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

Have a Yahappy day.
« Last Edit: September 18, 2007 by bikemadness »
Have a Yahappy day.
I don't know what is wrong with the world - but I know how to fix it.

Offline Rbz

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 2757
  • Karma: 493
    • View Profile
    • https://www.rbraz.com/
Re: Math for a bouncing ball...
« Reply #4 on: September 17, 2007 »
You can find more info about bouncing here:

http://freespace.virgin.net/hugo.elias/models/m_bounce.htm
Challenge Trophies Won:

Offline energy

  • Amiga 1200
  • ****
  • Posts: 280
  • Karma: 25
    • View Profile
Re: Math for a bouncing ball...
« Reply #5 on: September 18, 2007 »
Thank yu all!!!!
 :cheers:
coding: jwasm,masm
hobby: www.scd2003.de

Offline rain_storm

  • Here comes the Rain
  • DBF Aficionado
  • ******
  • Posts: 3088
  • Karma: 182
  • Rain never hurt nobody
    • View Profile
    • org_100h
Re: Math for a bouncing ball...
« Reply #6 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

Challenge Trophies Won:

Offline madsravn

  • ZX 81
  • *
  • Posts: 13
  • Karma: 0
    • View Profile
Re: Math for a bouncing ball...
« Reply #7 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

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Math for a bouncing ball...
« Reply #8 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
Challenge Trophies Won:

Offline rain_storm

  • Here comes the Rain
  • DBF Aficionado
  • ******
  • Posts: 3088
  • Karma: 182
  • Rain never hurt nobody
    • View Profile
    • org_100h
Re: Math for a bouncing ball...
« Reply #9 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

Challenge Trophies Won:

Offline psygate

  • Completly Insane.
  • Atari ST
  • ***
  • Posts: 173
  • Karma: 7
  • That boy needs therapy.
    • View Profile
Re: Math for a bouncing ball...
« Reply #10 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:
He who controlles the minds commands the many.
He who commands the many, conqueres the minds.

Offline rain_storm

  • Here comes the Rain
  • DBF Aficionado
  • ******
  • Posts: 3088
  • Karma: 182
  • Rain never hurt nobody
    • View Profile
    • org_100h
Re: Math for a bouncing ball...
« Reply #11 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

Challenge Trophies Won: