C++ 3D完美碰撞检测
我很难通过经验学习碰撞检测。我正在制作一款《我的世界》盒子游戏,并且正处于实现碰撞检测的阶段。
我已经完成了 x 和 y 轴',因为一切都由立方体组成,我想制作自己的碰撞检测器并使其尽可能轻。
有没有一种方法可以实现“像素完美”碰撞,即当玩家的边界框(或圆圈)接触到它注册为碰撞的框时?现在这就是我所做的:
if(-TOUCH_DISTANCE-1 < yPlayer-yBox && yPlayer-yBox < TOUCH_DISTANCE-1)
{
collisionNorth = true;
}
if(-TOUCH_DISTANCE+1 < yPlayer-yBox && yPlayer-yBox < TOUCH_DISTANCE+1)
{
collisionSouth = true;
}
它基本上检测到一定范围内的碰撞,这意味着错误,我不喜欢:(。注意+/-1,它将“碰撞墙”偏移到碰撞墙的相应一侧。 这在较低的速度下有效
,但是一旦有一些动作(当我破解速度变量时),就无法再检测到碰撞,因为我走得太快并直接穿过立方体......有没有办法使其成为 wallhax0r 证明吗?
这在 z 轴上尤其烦人,当玩家高速下落时,即使定义了可观的碰撞余量,它最终也会看起来很糟糕(玩家半埋)。
I'm having hard time learning collision detection by experience. I'm making a box game, à la Minecraft, and am at the stage of implementing collision detection.
I've done x and y axis', since everything consists of cubes I would like to make my own collision detector and make it as light as possible.
Is there a way to make "pixel perfect" collisions, that is when the player's bounding box (or circle) touches a box it registers as a collision? Right now this is what I did:
if(-TOUCH_DISTANCE-1 < yPlayer-yBox && yPlayer-yBox < TOUCH_DISTANCE-1)
{
collisionNorth = true;
}
if(-TOUCH_DISTANCE+1 < yPlayer-yBox && yPlayer-yBox < TOUCH_DISTANCE+1)
{
collisionSouth = true;
}
It basically detects the collision within a certain margin and that means errors, which I don't like :(. Notice the +/-1 which offsets the "collision wall" to the respective side of the box.
This works, on lower speeds, but once there's some action (when I crack up the speed variable) the collision can't be detected anymore since I go too fast and pass right through the cube... Is there a way to make it wallhax0r proof?
This is especially annoying on z axis, when the player falls at high speed and even when defining a respectable collision margin it will ultimately look nasty (player half buried).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您已经确定了使用离散数学对对象路径进行建模的问题之一。在时间
t
时,对象位于“此处”,在时间t + delta
时,对象位于“那里”——实际上并未经过其间的点。您可以通过减少增量来获得更高的准确性,但最终您将达到该时间间隔内可以计算的极限。
如果您可以通过使用相对较大的时间增量和松散的边界框来检测正在接近的碰撞,那么您可以提高准确性,但您将再次达到极限。
转换为连续模型可能会有所帮助,但可能需要更多的计算能力。当你的物体很近时,你可以切换到这个,这样你就不会一直这样做。
抱歉,我没有得到明确的答案,只有提示。
You've identified one of the problems of using discrete mathematics to model the path of an object. At time
t
the object is "here" and at timet + delta
it's "there" - without actually having passed through the points in between.You can get more accuracy by decreasing delta, but ultimately you are going to hit the limit of what you can calculate in that time interval.
If you can detect a collision approaching by using a relatively large time delta and loose bounding box you could then crank up the accuracy, but again you are going to hit limits.
Converting to a continuous model might help - but may take more computational power. You could switch to this when your objects are close so you're not doing it all the time.
Sorry I've not got a definite answer, only pointers.
通常您需要做的是跟踪对象的先前位置以及当前位置。然后,当您更新时,您可以检查对象是否在该时间段内相交,而不是它们“此时”是否相交。
Usually what you need to do is keep track of the previous position of the objects as well as the current position. Then when you update, you can check if the objects intersected during the time period, rather than if they intersect 'at the moment'.