简体   繁体   English

LWJGL 碰撞 3D OpenGL

[英]LWJGL Collision 3D OpenGL

Currently, I'm working on Collision Detection system for my engine.目前,我正在为我的引擎开发碰撞检测系统。 My Code works very fine but how can I prevent the player from passing through an object?我的代码工作得很好,但我怎样才能防止玩家通过一个对象?

My code:我的代码:

public void isCollide(Player player, Entity entity) {

    if ( 
        ( player.getPosition().x <= entity.getScale() + entity.getPosition().x && player.getScale() + player.getPosition().x >= entity.getPosition().x ) &&
        ( player.getPosition().y <= entity.getScale() + entity.getPosition().y && player.getScale() + player.getPosition().y >= entity.getPosition().y ) &&
        ( player.getPosition().z <= entity.getScale() + entity.getPosition().z && player.getScale() + player.getPosition().z >= entity.getPosition().z ) 
       ) 
    {
        System.out.println("Player is colliding!!!");
    }

}

Collision detection can be quite complicated to implement and getting collisions to look physically correct is part of what makes collisions difficult.碰撞检测的实现可能非常复杂,让碰撞看起来物理上正确是使碰撞变得困难的部分原因。

However from the code you've provided it looks like you are asking about axis aligned bounding box (AABB) collisions which is not so complicated at all.但是,从您提供的代码来看,您似乎在询问轴对齐边界框 (AABB) 碰撞,这根本不是那么复杂。

AABB collisions is a very simple way of approximating collisions. AABB 碰撞是一种非常简单的碰撞近似方法。 The general idea is very simple;总体思路很简单; take each object in the game and create a bounding box for the object.获取游戏中的每个对象并为该对象创建一个边界框。 For axis aligned bounding box collisions this means that the bounding boxes you create are aligned with the world axes and each collision box is represented with six numbers;对于轴对齐的边界框碰撞,这意味着您创建的边界框与世界轴对齐,并且每个碰撞框用六个数字表示; min x and max x, min y and max y, and min z and max z.最小 x 和最大 x,最小 y 和最大 y,以及最小 z 和最大 z。

For performing a collision check you can take any two of these boxes and simply check if they overlap on each axis.为了执行碰撞检查,您可以使用这些框中的任意两个,并简单地检查它们是否在每个轴上重叠。 If they overlap on the x, y and z axis then these objects are colliding.如果它们在 x、y 和 z 轴上重叠,那么这些对象就会发生碰撞。

To specifically address this question:具体解决这个问题:

How can I prevent the player from passing through an object?如何防止玩家穿过物体?

This is actually fairly straightforward as well when you are doing AABB collisions.当您进行 AABB 碰撞时,这实际上也相当简单。 The answer here is to simply reverse the movement that created the collision in the first place.这里的答案是简单地逆转最初产生碰撞的运动。 So you have a player and an AABB for the player.所以你有一个播放器和一个播放器的 AABB。 When updating the players position you translate your players bounding box by some fixed amount.更新球员位置时,您将球员边界框平移固定数量。 When the translation is done you perform your collision check.翻译完成后,您将执行碰撞检查。 If you find that the player is colliding with something then you undo the translation that you just performed, moving the player out of the object it was colliding with.如果您发现玩家正在与某物发生碰撞,那么您可以撤消刚刚执行的平移,将玩家从它所碰撞的对象中移出。

When you finally draw your scene, what you see is the player not colliding anymore because each time you moved the player in to a collision, you undo the movement.当您最终绘制场景时,您看到的是玩家不再发生碰撞,因为每次将玩家移动到碰撞中时,您都会撤消移动。

Of course this method is not without issues, as it's a basic technique.当然,这种方法并非没有问题,因为它是一项基本技术。 For example if the players speed is too high it can cause the player to phase through objects.例如,如果玩家速度太高,可能会导致玩家通过物体。 You can also get in to situations where undoing the players movement is not enough to undo the collision especially if there are many objects in the scene.您还可能会遇到以下情况:撤消玩家的运动不足以撤消碰撞,尤其是在场景中有许多对象的情况下。 There is also plenty of issues with using AABB's, take the following image for example;使用AABB也有很多问题,以下图为例;

在此处输入图片说明

If you assume that the black line is some object in your game then the red box is the AABB that you create to do collision checks with.如果您假设黑线是游戏中的某个对象,那么红色框就是您创建的用于进行碰撞检查的 AABB。 For this object clearly there is a way for other bounding boxes to intersect with this box which will register a collisions but most of the box is filled with empty space.对于这个对象,显然有一种方法可以让其他边界框与这个框相交,这将记录碰撞,但大部分框都填充了空白空间。

These two AABBs will register a collision even though the objects contained within them aren't colliding.即使它们中包含的对象没有发生碰撞,这两个 AABB 也会记录碰撞。

在此处输入图片说明

To fix these issues you need something more clever such as oriented bounding boxes and a method for checking collisions between oriented bounding boxes.要解决这些问题,您需要更聪明的方法,例如定向边界框和检查定向边界框之间碰撞的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM