Tuesday, June 5, 2018

collision detection - Sphere intersection Stuck


I have two spheres that are bounded by spheres for collision detection. I do not want them to intersect. My intersection works. But it gets stuck. Once the function returns true, I cannot get it out. I tried



collideBool = ObjectOne.Intersect(ObjectTwo);

if(collide == true)
{
leftright = leftright;
updown = updown;
}
else
{
if(GetAsyncKeyState(VK_RIGHT))

leftright -= dt;
if(GetAsyncKeyState(VK_LEFT))
leftright += dt;
if(GetAsyncKeyState(VK_DOWN))
updown += dt;
if(GetAsyncKeyState(VK_UP))
updown -= dt;
}

collide = false;


My sphere gets stuck. What is a way around this?



Answer



Check for collision against the new position and undo if it would cause a collision.


float oldleftright = leftright, oldupdown = updown;

if(GetAsyncKeyState(VK_RIGHT))
leftright -= dt;
if(GetAsyncKeyState(VK_LEFT))
leftright += dt;

if(GetAsyncKeyState(VK_DOWN))
updown += dt;
if(GetAsyncKeyState(VK_UP))
updown -= dt;

bool collide = ObjectOne.Intersect(ObjectTwo);
if (collide)
{
leftright = oldleftright;
updown = oldupdown;

}
collide = false;

No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...