Bouncing within a circle
Hi,
I''m working on a asteroids kind of game within a 3D tunnel.
The asteroids have a x and y velocity and and also move towards the player along the z-axis.
For simplicity the collision is only for x and y. So the problem can be descibed as 2D bouncing balls within a circle.
For now, I''m only interested in the bounce-circle problem, not the bounce between different balls.
To test for a collision, I have the following formula:
if (ball.x * ball.x + ball.y * ball.y) > radius * radius)->hit!
This works perfect, but then I need the new angle for the velocity of the ball. Just inverting vx,vy doesn''t work.
I think I can easy calculate the normal of the intersection plane by taking the intersection point on the circle, inverting x and y and normalize this 2D vector. This way it points from the intersection point on the circle towards the middle of the circle.
What now? Do I need the angle between the velocity vector and the intersection plane normal.
Code snippet:
{
// dx = velocity on x axis
// dy = velocity on y axis
// ox, oy, oz = origin of object in worldspace
// size = size of the object (radius of bounding sphere)
ox += _dx;
oy += _dy;
const int radius = 512;
float dist = (float) fabs(sqrt((ox * ox) + (oy * oy)));
if (dist > (radius - size)) //there''s a hit!
{
//how much too far?
float d = dist - (radius -size);
//how much is this from total?
float perx = (float) fabs(d/_dx);
float pery = (float) fabs(d/_dy);
//go back to edge
ox -= (perx * _dx);
oy -= (pery * _dy);
//change direction
_dx = -_dx;
_dy = -_dy;
//add new direction
ox += (perx * _dx);
oy += (pery * _dy);
}
// x,y,z object position in camspace
x = cam.x + ox;
y = -cam.y + oy;
z = oz;
}
This is the problem part:
//change direction
_dx = -_dx;
_dy = -_dy;
This is far from realisic, cause they just bounce back in opposite direction.
Hope I made it a little easy to understand!
Thanks in advance!
Gr,
BoRReL
I won''t give you any code or anything cuz I suck at programming, but generally, on a hard surface, objects bounce back at the same angle they came in, relative to the angle of the surface the bumped into. So if the angle of the surface a ball bumped into is, say, 35 degrees, and the ball comes at 145 degrees, then it will bounce back at 145-90-35 degrees or something like that. Anyway. It was just a little something. Not sure if my formula is good, though..
You must determine a line from the middle of the screen to the point where the ball collides and a normal for(tangent to the tunnel). The direction where it came from is on the other side of the line("T''s tail") relative to its prior angle. Hope I helped...
------------------------------If there be no heaven,may there atleast be a hell.-------------------------------Afterlife-
Thanks for your replies!
Now if for example the intersectionpoint is (128,128) with a circle radius of 256:
/----\
/......X
|.....o|
\....../
\----/
That would be the X of this pretty circle
The normal of the intersection plane would be (-128,-128) which is (-0.5,-0.5) normalized.
If my object was left beneath the intersection point (being the 'o' in the picture) with a velocity of fe (10,10), How do I calculate the proper new velocity, which should be (-10,10)(going left, going up)??
Thanks in advance!
Gr,
BoRReL
Edited by - BoRReL on July 7, 2001 8:02:34 PM
Edited by - BoRReL on July 7, 2001 8:04:06 PM
Edited by - BoRReL on July 7, 2001 8:05:06 PM
Now if for example the intersectionpoint is (128,128) with a circle radius of 256:
/----\
/......X
|.....o|
\....../
\----/
That would be the X of this pretty circle

The normal of the intersection plane would be (-128,-128) which is (-0.5,-0.5) normalized.
If my object was left beneath the intersection point (being the 'o' in the picture) with a velocity of fe (10,10), How do I calculate the proper new velocity, which should be (-10,10)(going left, going up)??
Thanks in advance!
Gr,
BoRReL
Edited by - BoRReL on July 7, 2001 8:02:34 PM
Edited by - BoRReL on July 7, 2001 8:04:06 PM
Edited by - BoRReL on July 7, 2001 8:05:06 PM
Perhaps you could use some mathematical algorithms...
(v_x1 + collision_x1)>2 + (v_y1 + collison_y1)>2 = (v_x2 + collision_x1)>2 + (v_y2 + collison_y1)>2
and
(v_x1 + collision_x2)>2 + (v_y1 + collison_y2)>2 = (v_x2 + collision_x2)>2 + (v_y2 + collison_y2)>2
(relative "distance" of the speeds from the opposite collision points are the same, or something...)
">2" means to the second exponent and collision_x2 and -y2 are the opposite collision points. Only uknown variables are v_x2 and -y2 (speeds are relative to the other coordinates). Using those algs would also require some "if()" checking, because square can be positive and negative, the prior speeds define the "polarity". Finding out what vx2&y2 are from those algs can be really bitchy and I'm not even sure if it'll work that way, but still maths hold the answer to your question. Hope I'm right...
--------------
| Fill the void |
--------------
Edited by - Afterlife on July 8, 2001 7:46:49 AM
(v_x1 + collision_x1)>2 + (v_y1 + collison_y1)>2 = (v_x2 + collision_x1)>2 + (v_y2 + collison_y1)>2
and
(v_x1 + collision_x2)>2 + (v_y1 + collison_y2)>2 = (v_x2 + collision_x2)>2 + (v_y2 + collison_y2)>2
(relative "distance" of the speeds from the opposite collision points are the same, or something...)
">2" means to the second exponent and collision_x2 and -y2 are the opposite collision points. Only uknown variables are v_x2 and -y2 (speeds are relative to the other coordinates). Using those algs would also require some "if()" checking, because square can be positive and negative, the prior speeds define the "polarity". Finding out what vx2&y2 are from those algs can be really bitchy and I'm not even sure if it'll work that way, but still maths hold the answer to your question. Hope I'm right...
--------------
| Fill the void |
--------------
Edited by - Afterlife on July 8, 2001 7:46:49 AM
------------------------------If there be no heaven,may there atleast be a hell.-------------------------------Afterlife-
Thanks for replying!
Yet I think the formulae can''t be right.
If you could solve the formula (wich I doubt)
then v_x2 would be the same as v_x1 and v_y2 the same as v_y1!
Same goes for the other.
Also a square is ALWAYS positive!
But hey, math defenatly holds the answer
I guess there some cosinus or sinus involved
Yet I think the formulae can''t be right.

If you could solve the formula (wich I doubt)
|
then v_x2 would be the same as v_x1 and v_y2 the same as v_y1!
Same goes for the other.
Also a square is ALWAYS positive!

But hey, math defenatly holds the answer

I guess there some cosinus or sinus involved

Oops I have forgotten how to compute the distance between two points. hmm ... can't..quite..make it... argh, have to check the math book.. oh yeah now I remember(I think..) : (prevx1-colx1)>2 + (prevy1-colx1)>2 = (prevx2-colx1)>2 + (prevy2-colx1)>2
and
(prevx1-colx2)>2 + (prevy1-colx2)>2 = (prevx2-colx2)>2 + (prevy2-colx2)>2
(Of course not the speeds, the previouse coordinates, doh. Better have a CAT-scan). If you record a point before the collision you can get the next point on the other side of the normal after the collision with those formulaes and you could calcuate a line from the collision point through that next point(the one relative to the older point) and make the ball follow the line's track. This would make x and y speeds absolete (only one speed if it would follow the lines track) and would be pretty hard to use in real life anyway. But it's a nice theory, don't you think?
And what I ment with the square is that if x>2 = 9, x can be +3 or -3...
PS. I'd like to point out that despite my current "goofyminded summer-brain" -state, I have managed to put together a function that draws a line from x1,y1 to x2,y2. Pretty impressive, huh?
Edited by - Afterlife on July 8, 2001 9:41:09 AM
and
(prevx1-colx2)>2 + (prevy1-colx2)>2 = (prevx2-colx2)>2 + (prevy2-colx2)>2
(Of course not the speeds, the previouse coordinates, doh. Better have a CAT-scan). If you record a point before the collision you can get the next point on the other side of the normal after the collision with those formulaes and you could calcuate a line from the collision point through that next point(the one relative to the older point) and make the ball follow the line's track. This would make x and y speeds absolete (only one speed if it would follow the lines track) and would be pretty hard to use in real life anyway. But it's a nice theory, don't you think?

And what I ment with the square is that if x>2 = 9, x can be +3 or -3...
PS. I'd like to point out that despite my current "goofyminded summer-brain" -state, I have managed to put together a function that draws a line from x1,y1 to x2,y2. Pretty impressive, huh?

Edited by - Afterlife on July 8, 2001 9:41:09 AM
------------------------------If there be no heaven,may there atleast be a hell.-------------------------------Afterlife-
July 08, 2001 09:23 AM
just a hypothesis, never done anything like this before. I tend to think in terms of geometry, and not algebra
first you take the intersection point with the circle wall.
You''ve already found this.
Next, use a tangent line of the circle to base the angle of intersection from.
\
/---\
/ x\<-- the tangent line
| o |\
\ / \
\---/
you can get this by taking a perpendicular of the radius at this point.
Then it''s just like any other wall you would bounce it off of.
Am I correct?
first you take the intersection point with the circle wall.
You''ve already found this.
Next, use a tangent line of the circle to base the angle of intersection from.
\
/---\
/ x\<-- the tangent line
| o |\
\ / \
\---/
you can get this by taking a perpendicular of the radius at this point.
Then it''s just like any other wall you would bounce it off of.
Am I correct?
K, don't want to go through other responces, but here's a way to do it. Get normal of surface you're bouncing off of. Now, the angle of the object going onto this surface, will be the mirror of it coming off the surface, if you consider the normal the mirror. So, it your normal is going down/left at 225 degrees, and the ball comes in below that at 230 degrees, then it will leave at 220 degrees. Now, I was sloppy with my degrees, as I was picturing the lines the object is travelling on as always going away from the surface. So... (very poor explanation I know) the ball would actually be "travelling" at the angle 50 degrees. (+180). Ya get me? So,
NewAngle = (OldAngle+180)-NormalAngle
or something like that.
Edited by - Thrump on July 8, 2001 11:38:27 AM
NewAngle = (OldAngle+180)-NormalAngle
or something like that.
Edited by - Thrump on July 8, 2001 11:38:27 AM
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement