[英]3D collision in OpenGL
I'm trying to create collision for my OpenGL application. 我正在尝试为我的OpenGL应用程序创建碰撞。
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.
如果摄像机位于平台内,则执行所有3个测试。
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. 碰撞分辨率是解决碰撞的行为,并且比仅执行布尔
IsColliding
函数要IsColliding
。
Additional information to search for would be: Separating Axis Test (SAT). 要搜索的其他信息包括:分离轴测试(SAT)。 Since you're dealing with AABBs (presumably) you can quite easily put together a simple resolution that just moves your camera outside.
由于您正在处理AABB(大概),您可以很容易地将一个简单的分辨率放在一起,只需将相机移到外面。
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. 当然,实际实施会涉及更多。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.