Archived

This topic is now archived and is closed to further replies.

Ajsoftware

Bouncy Ball Program

Recommended Posts

I have a 2d surface and a ball that collides with the surface. I want to figure out the new velocities of the ball after the collision. the surface is described as line(x1,y1)-(x2,y2) the ball is circle(ballx,bally) The ball also has both x and y velocities ball_x_velocity, ball_y_velocity Here is my current collision code which works only when the ball is impacting the line perpendicularly. xforce = x2 - x1 yforce = x2 - x1 totalforce = ABS(xforce) + ABS(yforce) xforce = xforce / totalforce yforce = yforce / totalforce ballforce = ballxvelocity + ballyvelocity ballyvelocity = ballforce * -yforce ballxvelocity = ballforce * xforce This dosn''t work too good any sugestions?

Share this post


Link to post
Share on other sites
I''m not sure what you''re trying to accomplish exactly...but if you JUST want the ball to bounce up, you only need 4 variables for the ball, x,y, x velocity, y velocity. When the ball hits the line, just reverse the y velocity.

As in Y_Velocity = Y_Velocity * -1.

In any case I''d reccomend starting from that, and then add in complexity once you have something you know that works...

-=Lohrno

Share this post


Link to post
Share on other sites
quote:
Original post by Ajsoftware
I have a 2d surface and a ball that collides with the surface. I want to figure out the new velocities of the ball after the collision.

the surface is described as line(x1,y1)-(x2,y2)
the ball is circle(ballx,bally)
The ball also has both x and y velocities
ball_x_velocity, ball_y_velocity

Here is my current collision code which works only when the ball is impacting the line perpendicularly.


xforce = x2 - x1
yforce = x2 - x1
totalforce = ABS(xforce) + ABS(yforce)
xforce = xforce / totalforce
yforce = yforce / totalforce
ballforce = ballxvelocity + ballyvelocity
ballyvelocity = ballforce * -yforce
ballxvelocity = ballforce * xforce

This dosn't work too good any sugestions?



Are you familiar with the Law of Reflection? Basically, the law states that the angle of incidence is equal to the angle of reflection when reflecting a ray against a plane. The angle of incidence is the angle formed by the incoming ray and a line segment perpendicular to the plane at the point of reflection. The angle of reflection is the angle formed by the reflected ray and the perpendicular line segment.

This assumes a "perfect" model--ie, no friction, no deformation of the colliding body or the surface, perfect reflection (no force or velocity lost), etc... It is not a realistic model as far as physics goes, but is good enough for an approximation.

In the most common cases, the line segment is either horizontal or vertical, making the reflection as simple as changing the sign of either the x or the y component of the object velocity--changing the sign of x for a reflection from a vertical line, changing the sign of y for a reflection from a horizontal line.

In the case of the reflecting line segment being an arbitrary line, the problem is complicated just a tad.

This can be solved using a little bit of vector math. Suppose we have the following vectors (all unit vectors):

N = Normal vector to the reflecting surface. In the case of the surface being a line segment, this is a vector representing a line perpendicular to the line.

V = Object movement vector. Basically, the unit vector of the object's velocity.

R = Object's reflected vector (the unknown we are trying to determine).

The normal vector is easily calculated from the reflecting line segment, by calculating the unit vector representing the line segment, then swapping the x and y values, and multiplying one or the other by -1:

Normalx=-(y2-y1)
Normaly=(x2-x1)

And to get the unit vector, divide by the magnitude of the vector:
Nx=Normalx / |Normal|
Ny=Normaly / |Normal|

(At heart, this is basically equivalent to taking the inverse slope of the line.)

Now, according to the Law (I am the Word. Heh, sorry...) the angle between N and R is equal to the angle between N and -V.

Since these angles are equal, it follows that N dot R = N dot -V. (I use dot to represent the dot product of two vectors.)

A little switching around results in:

R = V-2(N dot V)N

Remember that the dot product of two vectors A and B is equal to AxBx + AyBy. Calculate this value for N dot V:

NdotV = N dot V = NxVx + NyVy

and use it in the equations:

Rx = Vx - 2*NdotV*Nx
Ry = Vy - 2*NdotV*Ny

to calculate the vector of reflection.

You will need to modify this reflected vector by the magnitude of the object's velocity, as R is calculated as a unit vector.


Be warned that this is for the most part off the top of my head. I tested it with a few of the simple cases (horizontal and vertical line segments, etc...) but have not tested it exhaustively, so there may be mathematical errors. If anybody sees anything wrong with this, let me know.



EDIT: Fix my subscript tags.
EDIT AGAIN: Munge a few things around for clarity.


Josh
vertexnormal AT linuxmail DOT org


Check out Golem: Lands of Shadow, an isometrically rendered hack-and-slash inspired equally by Nethack and Diablo.



[edited by - VertexNormal on November 14, 2003 10:56:30 PM]

[edited by - VertexNormal on November 14, 2003 11:00:08 PM]

Share this post


Link to post
Share on other sites