• Advertisement

Archived

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

Collision

This topic is 5267 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all, I''ve got a problem making a simple 2D game and I need some help. What I want to do is to have a circle colliding with a polygon. I''ve already found a way to calculate if the circle is colliding with either side of the polygon so now I only need a way to make the ball collide correctly. Its velocity is stored as 2 floats xv and yv. Any ideas?

Share this post


Link to post
Share on other sites
Advertisement
You know... the ball comes with certain velocity and a certain angle towards a side of a polygon which is for example with endpoints x1=10 y1=20 and x2=50 y2=5. And the ball reaches the side with velocity, say, xv=-5 yv=3. How should xv and yv change?

Share this post


Link to post
Share on other sites
Can you draw a diagram of your problem? Its easier to understand then .

Share this post


Link to post
Share on other sites
OK but... ah... how am I supposed to post it? I mean I need to upload it on a free server first, right? I know a lot of free servers but none of them is visible outside BG (of course)

Share this post


Link to post
Share on other sites
quote:
Original post by ACAC
Can you draw a diagram of your problem? Its easier to understand then .


Do you mean something like this?

Y
6|
5|
4|
3|
2|
1|------*-----*>
0|_ _ _ _ _ _ _
0 1 2 3 4 5 6X

each * represents 1 frame.. so that would be the speed of 0yv and 3xv (only it starts at 1,0 in the coordinate system)

Edit: it doesn't look the same after i posted it :/ .. well, u get the picture :>

[edited by - RefikuL on August 18, 2003 6:19:26 AM]

Share this post


Link to post
Share on other sites
yes, that's how it is

PS: only that (0,0) is the upper-left corner, of course

[edited by - izhbq412 on August 18, 2003 6:25:51 AM]

Share this post


Link to post
Share on other sites
I think it would just change to xv5(when it bounces off the left wall) instead of -5 and keep the y3 value until it hits the "roof", and then change y3 to y-3.. too bad i lack at programming skills to test this myself :D

Edit: it only works on 90° walls tho.. :o otherwise u probable need to figure out the angle and make advanced calculation

[edited by - RefikuL on August 18, 2003 6:28:03 AM]

Share this post


Link to post
Share on other sites
You mean xv = -xv or yv = -yv? That works only with rectangles.

[edited by - izhbq412 on August 18, 2003 6:31:07 AM]

Share this post


Link to post
Share on other sites
Well then I dont think I can help any more. Maybe you should post this in mathematchics forum.. btw I think I saw a post on something similar yesterday..

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

  • Advertisement