Jump to content
  • Advertisement
Sign in to follow this  
EvilNando

sphere - sphere collision help

This topic is 2541 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have no idea what Im doing wrong but..

whenever 2 spheres collide they transport to the top of screen (0,0) and stay there

-___-




////////////////////////////////////////////////////////////////////////
void PhysicsGame::ResolveCollisions(Sphere* sphereA, Sphere* sphereB)
{
// - masses
float m1 = sphereA->Mass;
float m2 = sphereB->Mass;

// - inverse mass
float im1 = 1 / m1;
float im2 = 1 / m2;

Vector2 delta = sphereA->Position - sphereB->Position;
float d = delta.length();

// minimum translation distance to push balls apart after intersecting
Vector2 mtd = delta * ( sphereA->Radius + sphereB->Radius ) - d / d;

// push-pull them apart based off their mass
sphereA->Position = mtd * im1 / (im1 + im2);
sphereB->Position = sphereA->Position - mtd * im2 / (im1 + im2);

// impact speed
Vector2 v = sphereA->Velocity - sphereB->Velocity;
float vn = glm::dot(v, glm::normalize(mtd));

// sphere intersecting but moving away from each other already
if (vn > 0.0f) return;

// collision impulse
float i = (- (1.0f + BOUNCINESS) * vn) / (im1 + im2);
Vector2 impulse = mtd * i;

// change in momentum
sphereA->Velocity += impulse * im1;
sphereB->Velocity -= impulse * im2;
}



////////////////////////////////////////////////////////////////////////
void PhysicsGame::CheckCollisions(Sphere* sphereA, Sphere* sphereB)
{
// for easy reading...

// - positions
float x1 = sphereA->Position.x;
float x2 = sphereB->Position.x;

float y1 = sphereA->Position.y;
float y2 = sphereB->Position.y;

// - radiuses
float r1 = sphereA->Radius;
float r2 = sphereB->Radius;

// - distances
float dx = x2 - x1;
float dy = y2 - y1;
float d = sqrt(dx*dx + dy*dy);


//if ( d < 0.00001f ) return;

if ( d < r1 + r2 )
ResolveCollisions(sphereA, sphereB);
}

Share this post


Link to post
Share on other sites
Advertisement
Should this

// minimum translation distance to push balls apart after intersecting
Vector2 mtd = delta * ( sphereA->Radius + sphereB->Radius ) - d / d;

not look like this

// minimum translation distance to push balls apart after intersecting
Vector2 mtd = delta * ( sphereA->Radius + sphereB->Radius - d) / d;

Share this post


Link to post
Share on other sites
I think you're missing the obvious here.

sphereA->Position = mtd * im1 / (im1 + im2);
sphereB->Position = sphereA->Position - mtd * im2 / (im1 + im2);

should be:

sphereA->Position += mtd * im1 / (im1 + im2);
sphereB->Position -= mtd * im2 / (im1 + im2);

ontop of that, the line Ashaman pointed out is also an issue.

Share this post


Link to post
Share on other sites
ah yeah normalizing mtd did the trick

now the simulations works better but still... the balls accelerate too fast .. but only on the X axis, maybe I should try incresing / decreasing my mass values?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!