user993683
user993683

Reputation:

Physics simulation basics

Consider a 3D solar system simulation.

My simulation loop:

[Calculate new positions]->[Render]->[Calculate new positions]->[Render] etc....

This loop runs at, say, 25FPS.

A planet in my simulation is travelling very fast towards the sun (on a collision course). Lets look at the simulation loop:

  1. Position is calculated for the planet and the sun (they have not collided yet, but are very close to each other).
  2. Scene is rendered.
  3. Since FPS is not infinite, there is a small pause before the next iteration.
  4. Position is calculated for planet and sun - and here's the crux of my problem - they have not collided because during the 'small pause' the planet has travelled 'through' the sun and their positions are now a large distance apart. The collision detection algorithm does not detect any collision.

The only solutions I can see here are:

For anyone interested, I'm using three.js.

Upvotes: 1

Views: 417

Answers (2)

fibonatic
fibonatic

Reputation: 216

You could look at the change of angle between two objects, if this would be close to 180 degrees or pi radian it would be very likely that they would have collided. Calculating the change of angle between two objects can be done using a little bit of linear algebra (using the dot product):

dx0 = object[i].x - object[j].x;
dy0 = object[i].y - object[j].y;
dz0 = object[i].z - object[j].z;
dx1 = object[i].x + timeStep * object[i].u - object[j].x - timeStep * object[j].u;
dy1 = object[i].y + timeStep * object[i].v - object[j].y - timeStep * object[j].v;
dz1 = object[i].z + timeStep * object[i].w - object[j].z - timeStep * object[j].w;
dangle = acos((dx0 * dx1 + dy0 * dy1 + dz0 * dz1) / (sqrt(dx0 * dx0 + dy0 * dy0 + dz0 * dz0) * sqrt(dx1 * dx1 + dy1 * dy1 + dz1 * dz1)));

You do not have to take the arc cosine, since cos(dangle) will be near -1 for dangle close to 180 degrees.

I am not sure which range you should take to successfully detect collision, a little bit of trial and error on this part. And I would suggest that this wouldn't be the only step in collision detection and after this check at which time they would have collided, assuming that both objects move in a straight line and constant speed during this time step. The equation to solve would be an quadratic equation, which can have two possible answers (one of which would be if they would glitch through each other and collided on the other side). But if you are not interested in the exact time at which they collided you could still use the discriminant to determine if there would be an collision.

Upvotes: 0

DrC
DrC

Reputation: 7698

Happens with projectiles in shooter games. One solution is to have your movement stage shoot a line segment along the forward path to see if it would hit anything before the next frame. That assumes that only the one object is moving rapidly though. I suppose you could also run a segment between the last position and the new position although I've not done it that way myself. I've also had success just extending my collider definition forward into an ellipse that covers the unchecked area.

Upvotes: 3

Related Questions