Reputation: 299
I'm trying to create collision for my OpenGL application.
I have code that successfully tests whether my camera is inside my platform object:
void checkInsidePlatform()
{
float halfW = gymPlatform -> getW() / 2;
float Height = gymPlatform -> getH();
float halfD = gymPlatform -> getD() / 2;
float platformRight = gymPlatform -> getX() + halfW + 1;
float platformTop = gymPlatform -> getY() + Height + 1;
float platformFront = gymPlatform -> getZ() - halfD - 1;
if(testPlatformCollision())
{
//Below code doesnt work (NEED HELP HERE)
if(myCamera -> curPos -> x < platformRight)
{
myCamera -> curPos -> platformRight;
}
if(myCamera -> curPos -> z > platformFront)
{
myCamera -> curPos -> platformFront;
}
if(myCamera -> curPos -> y < platformTop)
{
myCamera -> curPos -> platformTop;
}
}
}
bool testPlatformCollision()
{
float halfW = gymPlatform -> getW() / 2;
float Height = gymPlatform -> getH();
float halfD = gymPlatform -> getD() / 2;
float platformLeft = gymPlatform -> getX() - halfW - 1;
float platformRight = gymPlatform -> getX() + halfW + 1;
float platformTop = gymPlatform -> getY() + Height + 1;
float platformFront = gymPlatform -> getZ() - halfD - 1;
float platformBack = gymPlatform -> getZ() + halfD + 1;
if((myCamera -> curPos -> x > platformLeft) && (myCamera -> curPos -> x < platformRight))
{
if((myCamera -> curPos -> z > platformFront) && (myCamera -> curPos -> z < platformBack))
{
if(myCamera -> curPos -> y < platformTop)
{
return true;
}
}
}
return false;
}
But now I'm stuck. I'm not sure how to move the camera outside of the platform if it goes inside. If the camera is inside the platform, all 3 tests are performed.
Upvotes: 0
Views: 450
Reputation: 1915
You need to perform collision resolution. Collision resolution is the act of resolving a collision, and is quite a bit more involved than just performing a boolean IsColliding
function.
Additional information to search for would be: Separating Axis Test (SAT). Since you're dealing with AABBs (presumably) you can quite easily put together a simple resolution that just moves your camera outside.
Here's a quick description: find the direction that the camera should move so that it is outside of the box. This direction should be the shortest path possible to move outside. Find the distance to move, and then perform that move operation.
Of course actual implementation gets a little more involved.
Upvotes: 2