Advertisement

Collision response... etc

Started by February 12, 2003 01:25 PM
7 comments, last by DanielH 22 years ago
ok... i have 10 asteroids (well circles actually) and another circle that is the player controlled "spaceship". Checking for collision is very easy but what´s then? How does the spaceship angle change at collision? How do i calculate the right acceleration for a spaceship? I know nothing will slow down of it self in space... but doesn´t that happens in lots of spacegames?
For a simple game, I''d just use simple pool-ball-style collision response, e.g. simple linear momentum conservation perhaps with a coefficient of restitution. Check out gamasutra.com, the programming features section. There is a good article on this subject there.

As for objects slowing down vs. not slowing down in space, well in reality the vacuum of space isn''t a perfect vacuum. There are particles floating around out there---they''re just not close together. The effect is minute, basically negligible unless the space craft is near a planet that has an atmosphere, but there would be *some* very *tiny*, *infinitesimal* deceleration in the void of space.

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net
Advertisement
I´m in 9th grade and I´m not very good in English. Can you explain a bit more detailed what you really mean? I have no idea what a simple linear momentum conservation is

I read the article, what i understand it is about collision detection and not response.. or? Anyway it had some math I havent learned in school yet... What does those || means? like || Q || C ||
First, you should store your spaceship in (positionX, positionY, velocityX, velocityY) form, rather than (positionX, positionY, angle, speed) form. That makes handling collisions more easy. If you are good at math, you can also use vectors.

Here is the code for handling the collisions between two circle-shaped objects (x1, y1 - coordinates of first object, x2, y2 - coordinates of second object, v1x, v1y - x and y components of first object's velocity, v2x, v2y - x and y components of second object's velocity, m1 - mass of first object, m2 - mass of second object, r1 - first object radius, r2 - second object radius):


      float tx1 = x1 + v1x; // make temporary positionsfloat ty1 = y1 + v1y; // so that we can step backfloat tx2 = x2 + v2x; // and don't get stuck into another objectfloat ty2 = y2 + v2y;float dx = tx2 - tx1;float dy = ty2 - ty1;float r = sqrt(dx * dx + dy * dy); // calculate distance between object centersif(r < r1 + r2){// there is a collision, do some math	float rx = dx / r;	float ry = dy / r;	float k1 = 2 * m2 * (rx * (v2x - v1x) + ry * (v2y - v1y)) / (m1 + m2);	float k2 = 2 * m1 * (rx * (v1x - v2x) + ry * (v1y - v2y)) / (m1 + m2);// update velocities, but keep previous positions	v1x += k1 * rx;	v1y += k1 * ry;	v2x += k2 * rx;	v2y += k2 * ry;}else{// no collision, move to new positions	x1 = tx1;	y1 = ty1;	x2 = tx2;	y2 = ty2;}  


In case if you want one object not to move after collision, the collision code is something like that:


        float rx = dx / r;float ry = dy / r;float k = 2 * (rx * vx + ry * vy) / m;vx += k * rx;vy += k * ry;  


but I'm not sure, because I've lost the original source code.


[edited by - Advanced Bug on February 14, 2003 6:14:29 AM]
It seams to work, but if my mass is anything else then 1 the "ball" goes throught the other one.

And the collision detection isnt good eighter. In high speeds it goes through annyway... I know that gamasutra article is about that but it´s hard for me to understand.

This is my code

procedure Move();var  i :integer;  collision:boolean;  tx:single;  ty:single;  dx:single;  dy:single;  rx:single;  ry:single;  r:single;  k:single;begin  collision := false;  tx := ss.x + ss.v.x;  ty := ss.y + ss.v.y;  for i := 0 to 9 do  begin    dx := asteroid.x - tx;    dy := asteroid.y - ty;<br>    r := sqrt(sqr(dx) + sqr(dy));<br>    if (r < (ss.r + asteroid.r))<br>    then begin<br>                collision := true;<br><br>               	rx := dx / r;<br>                ry := dy / r;<br>                k := 2 * (rx * ss.v.x + ry * ss.v.y) / ss.m;<br>                ss.v.x := ss.v.x - k * rx;<br>                ss.v.y := ss.v.y - k * ry;<br><br>                break;<br>    end;<br>  end;<br><br><br>//  if not collision then<br>  begin<br>        ss.x := tx;<br>        ss.y := ty;<br>  end;<br>end;<br> </pre>   </i>   
Can someone please post some collision detection code between one static and one moving circle.

(this was harder then i thought)
Advertisement
quote:
Original post by DanielH
It seams to work, but if my mass is anything else then 1 the "ball" goes throught the other one.



I made a mistake. The code for one moving and one static object is

k = 2 * (rx * vx + ry * vy); // NO MASS HERE
vx = vx - k * rx;
vy = vy - k * ry;

and it doesn''t uses object mass.

quote:
Original post by DanielH
And the collision detection isnt good eighter. In high speeds it goes through annyway... I know that gamasutra article is about that but it´s hard for me to understand.



The most simple solution is to move objects several times (with decreased velocities) each frame. That is, for example, each frame you update your objects 10 times, with velocities vx / 10 and vy / 10.
Wouldn´t that affect the speed quite a bit? Since i need to check distance on every pixel move and there is at least 10 static balls.

I can check for collision without using sqrt but how do i use ur code then?


like

deltaXSqr = (A.X - B.X)^2
deltaYSqr = (A.Y - B.Y)^2
sumRadii = (A.R + B.R)^2

if deltaXSqr + deltaYSqr < sumRadii
etc...
quote:
Original post by DanielH
Wouldn´t that affect the speed quite a bit? Since i need to check distance on every pixel move and there is at least 10 static balls.



I really don''t think that it will cause any speed problems. It is possible to calculate the exact equations for moving-to-static and even moving-to-moving circle collisions, but I think you should first try the simple solution, because it''s not a good idea to spend lots of time optimizing code that takes, for example, 0.1% of all program execution time.

This topic is closed to new replies.

Advertisement