0

我一直在为我正在做的 3D 事物创建碰撞检测问题。

我进行碰撞检测的方式是,首先将旧的 xyz 坐标存储到变量中,然后调用移动函数,然后调用碰撞函数。如果移动后发生碰撞,相机 - 就像在播放器中(现在)一样 - 将被设置回旧的 xyz 坐标。

我希望角色能够沿着立方体的侧面“滑动”——这样如果你在 x 轴上发生碰撞,你仍然可以沿着 z 轴轻微滑动。然而,在拐角处,角色完全停止了——因为同时存在 x 和 z 碰撞。我决定为 z 和 x 碰撞创建单独的变量以阻止这种情况的发生,但现在我可以在拐角处进入立方体内部 - 但只能在 X 轴上。我不知道如何解决这个问题,我尝试了各种各样的东西(比如代码中的最新变量),但我就是无法弄清楚。帮助将不胜感激。这是代码的相关部分:

def otherCollision(self,x,y,z):
       print(float(Camera.x))
       xcol = 0
       zcol = 0
       latest = 0
       if (-Camera.x >= cubez[0][0] - 1) and \
          (-Camera.x <= cubez[0][0] + cubez[0][3] + 1) and \
          (-Camera.z >= cubez[0][2] - 1) and \
          (-Camera.z <= cubez[0][2] + cubez[0][5] + 1):

            if (-Camera.x >= cubez[0][0] - 1) and \
               (-Camera.x <= cubez[0][0]) or \
               (-Camera.x <= cubez[0][0] - 1 + cubez[0][3] + 2) and \
               (-Camera.x >= cubez[0][0] - 1 + cubez[0][3] + 1): #>
                #Camera.x = x
                xcol = 1
                latest = 1

            if (-Camera.z >= cubez[0][2] - 1) and \
               (-Camera.z <= cubez[0][2]) or \
               (-Camera.z <= cubez[0][2] - 1 + cubez[0][5] + 2) and \
               (-Camera.z >= cubez[0][2] - 1 + cubez[0][5] + 1):    #>=
                #Camera.z = z
                zcol = 1
                latest = 2

       if xcol == 1 and zcol == 0:
           Camera.x = x
       if zcol == 1 and xcol == 0:
           Camera.z = z
       if xcol == 1 and zcol == 1 and latest == 2:
           Camera.x = x
       if xcol == 1 and zcol == 1 and latest == 1:
           Camera.z = z

值得一提的是,cubez列表中有一个列表——第一个索引是对象的编号,下一个索引是我们要查找的值。他们是,按顺序,x,y,z,width,height,depth

我正在使用 pyglet 1.2alpha,但我认为这与帖子不太相关,因为很明显问题出在我的逻辑上。

4

1 回答 1

0

我认为最好的解决方案是为你的演员添加一个速度属性(在你的例子中是“角色”还是“相机”?)。然后根据碰撞,将速度属性归零。

给定一个像这样的简单向量类:

class Vector(object):
    def __init__(self, x=0.0, y=0.0, z=0.0):
        self.x = x
        self.y = y
        self.z = z

...你的演员看起来像这样:

class Actor(object):
    def __init__(self):
        self.pos = Vector()
        self.vel = Vector()

现在,当您更新场景时,请执行以下步骤(抽象为您的示例):

  1. 计算施加在演员身上的力,并更新vel属性。
  2. 确定碰撞,并vel相应地更新属性。
  3. pos使用来自 的数据更新演员的vel

为简单起见,以下示例使用 2D 世界(z总是0),并且更新的恒定时间步长(dt被认为是 1 秒)。

class Actor(object):
    #(...)
    def update(self, dt=1):
        # Step 1 - assumes a constant movement to the top right
        self.vel.x = 1
        self.vel.y = 1

        # Step 2 - cube is your collision tested object
        cube = Cube()
        new_pos = self.pos + (self.vel * dt)
        if new_pos.x > cube.left and new_pos.x < cube.right:
            self.vel.x = 0
        if new_pos.y > cube.bottom and new_pos.y < cube.top:
            self.vel.y = 0

        # Step 3
        self.pos += self.vel * dt

首先你应该得到这样的工作。当正常运行时,为您的actor添加一个边界框,以便在侧面而不是在中心进行碰撞。

于 2013-09-08T09:36:51.250 回答