Page 1 of 1

Collision & Physical Response

PostPosted: Sun Oct 05, 2003 7:34 am
by jazz_e_bob
I can't seem to get this to work properly. Here is my test file.

http://www.maybanana.com/pantzboi/images/collision.zip

What am I doing wrong here?

PostPosted: Sun Oct 05, 2003 1:29 pm
by makslane
Has a small bug in collisions calculations.
I don't know when this happens :-(

PostPosted: Sun Oct 05, 2003 9:26 pm
by jazz_e_bob
Oh well! :)

No matter then.

I'll go back to my circle collision script. It has much improved since you implemented local variables. The only downside is that it only works with circular objects.

All the best.

/* actor collides with another actor (collide) */
collisionDistance = radius + collide.radius;
actualDistance = distance (x, y, collide.x, collide.y);
collNormalAngle = atan2(collide.y - y, collide.x - x);
/* position exactly touching - no intersection */
moveDist1 = (collisionDistance - actualDistance) * (collide.mass/(mass + collide.mass));
moveDist2 = (collisionDistance - actualDistance) * (mass/(mass + collide.mass));
x = x + moveDist1 * cos(collNormalAngle + 180);
y = y + moveDist1 * sin(collNormalAngle + 180);
/* collision response */
nX = cos(collNormalAngle);
nY = sin(collNormalAngle);
a1 = xvelocity * nX + yvelocity * nY;
a2 = collide.xvelocity * nX + collide.yvelocity * nY;
optimisedP = ( 2 * (a1 - a2)) / ( mass + collide.mass);
/* now find out the resultant vectors */
xvelocity = xvelocity - (optimisedP * collide.mass * nX);
yvelocity = yvelocity - (optimisedP * collide.mass * nY);
collide.xvelocity = collide.xvelocity + (optimisedP * mass * nX);
collide.yvelocity = collide.yvelocity + (optimisedP + mass * nY);

PostPosted: Sun Oct 05, 2003 9:34 pm
by jazz_e_bob
Whoops. Found a bug in my code. Here is the new one. :)


/* actor collides with another actor (collide) */
collisionDistance = radius + collide.radius;
actualDistance = distance (x, y, collide.x, collide.y);
collNormalAngle = atan2(collide.y - y, collide.x - x);
/* position exactly touching - no intersection */
moveDist1 = (collisionDistance - actualDistance) * (collide.mass/(mass + collide.mass));
moveDist2 = (collisionDistance - actualDistance) * (mass/(mass + collide.mass));
x = x + moveDist1 * cos(collNormalAngle + 180);
y = y + moveDist1 * sin(collNormalAngle + 180);
/* collision response */
nX = cos(collNormalAngle);
nY = sin(collNormalAngle);
a1 = xvelocity * nX + yvelocity * nY;
a2 = collide.xvelocity * nX + collide.yvelocity * nY;
optimisedP = ( 2 * (a1 - a2)) / ( mass + collide.mass);
/* now find out the resultant vectors */
xvelocity = xvelocity - (optimisedP * collide.mass * nX);
yvelocity = yvelocity - (optimisedP * collide.mass * nY);
collide.xvelocity = collide.xvelocity + (optimisedP * mass * nX);
collide.yvelocity = collide.yvelocity + (optimisedP * mass * nY);

Even Better Still...

PostPosted: Mon Oct 06, 2003 5:17 pm
by jazz_e_bob
Latest Version:

double massCollider = 10;
double massCollidee = 10;
double nX;
double nY;
double a1;
double a2;
double optimisedP;
/* player has collided */
double collisionDistance = (width/2) + (collide.width/2);
double actualDistance = distance (x, y, collide.x, collide.y);
double collNormalAngle = atan2(collide.y - y, collide.x - x);
/* position exactly touching - no intersection */
double moveDist1 = (collisionDistance - actualDistance) * (massCollidee/(massCollider + massCollidee));
double moveDist2 = (collisionDistance - actualDistance) * (massCollider/(massCollider + massCollidee));
x = x + moveDist1 * cos(collNormalAngle + 180);
y = y + moveDist1 * sin(collNormalAngle + 180);
collide.x = collide.x + moveDist2 * cos(collNormalAngle + 180);
collide.y = collide.y + moveDist2 * sin(collNormalAngle + 180);
/* collision response */
nX = cos(collNormalAngle);
nY = sin(collNormalAngle);
a1 = xvelocity * nX + yvelocity * nY;
a2 = collide.xvelocity * nX + collide.yvelocity * nY;
optimisedP = ( 2 * (a1 - a2)) / ( massCollider + massCollidee);
/* now find out the resultant vectors */
xvelocity = xvelocity - (optimisedP * massCollidee * nX);
yvelocity = yvelocity - (optimisedP * massCollidee * nY);
collide.xvelocity = collide.xvelocity + (optimisedP * massCollider * nX);
collide.yvelocity = collide.yvelocity + (optimisedP * massCollider * nY);