Collision

Started by
12 comments, last by izhbq412 20 years, 8 months ago
Depends how detailed you want the accuracy to be, you can simply reflect the velocity off the surface, or you can go into details about torque on the object, which you can calculate using the friction and impulse force (theres a post on the maths and physics forum with formulas for this). In the simpler case, its quite easy to work out using the angle at which the ball hits the polygon as the angle which it leaves the polygon. As has already been recommended, draw a diagram and work out the formulas, simply trigonometry.
-keyboard
Advertisement
I think izhbq412 means collision response... how the circle bounces off an edge of the polygon.

I assume you have already detected the collision with the polygon, right?

Well, given an edge (10,20) to (50, 5), you must calculate a normal for that edge. This normal represents the direction that that edge is facing. I won''t use vector math here since you haven''t used it in your original request, but this is how you do it. I will use nx, ny as the floats for my normal.

// calculate the normal (and normalize it)
nx = -(y2 - y1);
ny = (x2 - x1);
normallength = sqrt(nx * nx + ny * ny);
nx *= (1 / normallength);
ny *= (1 / normallength);

Now that you have the normal, you can calculate the reflection of the original sphere velocity pretty easily. In vector math it is r = a - 2(a.n).n, but I shall give you the formulae here.

// first calculate our dot product (required for reflection)
dot = (xv * nx) + (yv * ny);

// now calculate our reflected velocities...
rxv = xv - ((2 * dot) * nx);
ryv = yv - ((2 * dot) * ny);

you can now assign (rxv, ryv) to your (xv, yv) for your circle.

Now, here is the important bit. In order to get the normal facing the right direction you have to adopt a convention.
For each side of the polygon, if your edge goes from (x1,y1) to (x2,y2) then the normal will always face on the left of that edge or on the right (it depends if the top left of your screen is (0,0) or the bottom left of your screen is (0,0))

Hope this helped,
FReY
do unto others... and then run like hell.
10x FReY it worked
I''m still trying to understand exactly HOW it worked I''ll play with the code a little so that I''m sure I understand it.

10x to all of you! You''ve been very helpful.
_______________The essence of balance is detachment. To embrace a cause, to grow fond or spiteful, is to lose one''s balance after which, no action can be trusted. Our burden is not for the dependent of spirit. - Mayar, Third Keeper
Well, just to help you out a bit.

The trick comes in with the definition of dot-product for vectors:

a.b = |a||b|.cosA

now, the dot product of a vector a with a unit vector n
will give you the length of that vector a projected along n.
(this is called the resolution of a long n).

This result is used in the reflectance formula, except with the dot product equivalent a.b = (ax * bx) + (ay * by))

Now, since the angle between the incoming vector (original velocity) and the normal is always greater than 90 degrees, the dot product of those 2 vectors will give you a negative number. (because CosA for angles of A greater than 90 degrees is -1). Multiplying by that dot product therefore reverses the direction of the normal, so in essence you are subtracting a negative vector from the original.

Now draw a vector diagram and draw the components of that reflectance formula. You will easily see why it works then.

Hope this helped too.
FReY
do unto others... and then run like hell.

This topic is closed to new replies.

Advertisement