Collision & Physical Response

Non-platform specific questions.

Collision & Physical Response

Postby jazz_e_bob » Sun Oct 05, 2003 7:34 am

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?
Controlling complexity is the essence of computer programming.
User avatar
jazz_e_bob
 
Posts: 742
Joined: Tue Jul 01, 2003 9:38 pm
Location: Bloke from Cockatoo Creek Australia
Score: 14 Give a positive score

Postby makslane » Sun Oct 05, 2003 1:29 pm

Has a small bug in collisions calculations.
I don't know when this happens :-(
makslane
Site Admin
 
Posts: 3947
Joined: Sat Apr 05, 2003 6:47 pm
Score: 182 Give a positive score

Postby jazz_e_bob » Sun Oct 05, 2003 9:26 pm

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);
Controlling complexity is the essence of computer programming.
User avatar
jazz_e_bob
 
Posts: 742
Joined: Tue Jul 01, 2003 9:38 pm
Location: Bloke from Cockatoo Creek Australia
Score: 14 Give a positive score

Postby jazz_e_bob » Sun Oct 05, 2003 9:34 pm

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);
Controlling complexity is the essence of computer programming.
User avatar
jazz_e_bob
 
Posts: 742
Joined: Tue Jul 01, 2003 9:38 pm
Location: Bloke from Cockatoo Creek Australia
Score: 14 Give a positive score

Even Better Still...

Postby jazz_e_bob » Mon Oct 06, 2003 5:17 pm

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);
Controlling complexity is the essence of computer programming.
User avatar
jazz_e_bob
 
Posts: 742
Joined: Tue Jul 01, 2003 9:38 pm
Location: Bloke from Cockatoo Creek Australia
Score: 14 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest