简体   繁体   English

XNA 3D边界框碰撞不触发

[英]XNA 3D Bounding Box Collision Not Triggering

I've been browsing for about a hour and a half orso for a solution to this problem. 我已经浏览了大约一个半小时左右,以解决此问题。 I did see some threads on Xbox Live Indie Development forums but the actual forums aren't loading (Have they been taken down?) And everywhere else I look I can't find an answer. 我确实在Xbox Live Indie Development论坛上看到了一些主题,但是实际的论坛没有加载(它们是否已被删除?),我在其他任何地方都找不到答案。

The problem I'm having is that I can't get any sort of Intersect to trigger between two BoundingBoxes. 我遇到的问题是我无法在两个BoundingBox之间触发任何相交。 I've created a cube in 3D space and then put a box at the opposing vertices, that box seems to be fine from what I can tell in the Output. 我已经在3D空间中创建了一个多维数据集,然后在相对的顶点处放置了一个框,从我在输出中可以看到的角度来看,该框似乎还不错。 As is the camera's BoundingBox <-- For it I took the player's position and +- 1 on every axis for the min/max. 就像相机的BoundingBox <-一样,我将播放器的位置和每个轴的+/- 1设为最小/最大。 I originally intended to just re-use the playerposition for both min and max but that wasn't working so I tried this but it still doesn't work. 我原本打算只在最小和最大两个位置重用播放器位置,但这没有用,所以我尝试了一下,但仍然不起作用。

Here's some snippets of my code. 这是我的代码片段。

    void CheckCollision(Vector3 inPos, Vector3 inOldPos) //The idea for the inPos and
old position was that I'd reset the player's position to the old pos if there's a collision
    {
        if (block.collisionBox.Intersects(cam.cameraBox))
        {
            Debug.WriteLine("HELP"); //This doesn't trigger
        }
    }

The next is the Update in the main Game class. 接下来是主游戏类中的更新。

        protected override void Update(GameTime gameTime)
    {
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            Exit();

        CheckCollision(cam.Position, cam.comparisonVector);

        base.Update(gameTime);
    }

Now moving onto the Cube's class. 现在进入多维数据集的课程。

        private void SetUpVertices()
    {
        vertices = new VertexPositionColor[8];

        //front left bottom corner
        vertices[0] = new VertexPositionColor(new Vector3(0, 0, 0), color);
        //front left upper corner
        vertices[1] = new VertexPositionColor(new Vector3(0, 5, 0), color);
        //front right upper corner
        vertices[2] = new VertexPositionColor(new Vector3(5, 5, 0), color);
        //front lower right corner
        vertices[3] = new VertexPositionColor(new Vector3(5, 0, 0), color);
        //back left lower corner
        vertices[4] = new VertexPositionColor(new Vector3(0, 0, -5), color);
        //back left upper corner
        vertices[5] = new VertexPositionColor(new Vector3(0, 5, -5), color);
        //back right upper corner
        vertices[6] = new VertexPositionColor(new Vector3(5, 5, -5), color);
        //back right lower corner
        vertices[7] = new VertexPositionColor(new Vector3(5, 0, -5), color);

        collisionBox = new BoundingBox(vertices[0].Position, vertices[6].Position);

        vBuffer = new VertexBuffer(device, typeof(VertexPositionColor), 8, BufferUsage.WriteOnly);
        vBuffer.SetData<VertexPositionColor>(vertices);
    }

And finally the Camera's class. 最后是相机课。

        void UpdateBoundingBox()
    {
        cameraBox = new BoundingBox(cameraPosition + new Vector3(-1, -1, -1), cameraPosition + new Vector3(1,1,1));
    }

If you want anything else just let me know :) I appreciate any help, thanks 如果您还有其他需要,请告诉我:)非常感谢您的帮助,谢谢

The problem is with the min and max coordinates of your collision box. 问题出在碰撞盒的最小和最大坐标上。 The collision boxes min is at [0,0,0] and the max is [5,5,-5]. 碰撞盒的最小值为[0,0,0],最大值为[5,5,-5]。

A bounding boxes max coordinate should always be greater on the x, y and z component than the min, otherwise you can create a bounding box with "negative" thickness on one or more dimensions (or commonly referred to as an inside out box). 边界框的最大坐标在x,y和z分量上应始终大于最小值,否则,您可以在一个或多个维度上创建厚度为“负”的边界框(或通常称为“内向外框”)。

You can get the correct min and max of your bounding box with the following modification. 通过以下修改,您可以获得正确的边界框的最小值和最大值。 The idea here being to simply compare the x,y and z component of each vertex looking for the lowest x value of all of the vertices, the lowest y value and the lowest z value which becomes the new min of your box. 这里的想法是简单地比较每个顶点的x,y和z分量,以寻找所有顶点的最低x值,最低y值和最低z值,从而成为盒子的新最小值。 The same is done to obtain the max coordinate. 进行相同操作以获得最大坐标。 (Possibly not the most efficient code, but as a working example it gets the job done nonetheless). (可能不是最有效的代码,但作为一个工作示例,它仍然可以完成工作)。

Vector3 MinResult(Vector3 u, Vector3 v)
  {
     Vector3 minVec = v;

     if (u.X < v.X)
     {
        minVec.X = u.X;
     }

     if (u.Y < v.Y)
     {
        minVec.Y = u.Y;
     }

     if (u.Z < v.Z)
     {
        minVec.Z = u.Z;
     }

     return minVec;
  }

  Vector3 MaxResult(Vector3 u, Vector3 v)
  {
     Vector3 maxVec = v;

     if (u.X > v.X)
     {
        maxVec.X = u.X;
     }

     if (u.Y > v.Y)
     {
        maxVec.Y = u.Y;
     }

     if (u.Z > v.Z)
     {
        maxVec.Z = u.Z;
     }

     return maxVec;
  }

private void SetUpVertices()
{
     vertices = new VertexPositionColor[8];

     //front left bottom corner
     vertices[0] = new VertexPositionColor(new Vector3(0, 0, 0), color);
     //front left upper corner
     vertices[1] = new VertexPositionColor(new Vector3(0, 5, 0), color);
     //front right upper corner
     vertices[2] = new VertexPositionColor(new Vector3(5, 5, 0), color);
     //front lower right corner
     vertices[3] = new VertexPositionColor(new Vector3(5, 0, 0), color);
     //back left lower corner
     vertices[4] = new VertexPositionColor(new Vector3(0, 0, -5), color);
     //back left upper corner
     vertices[5] = new VertexPositionColor(new Vector3(0, 5, -5), color);
     //back right upper corner
     vertices[6] = new VertexPositionColor(new Vector3(5, 5, -5), color);
     //back right lower corner
     vertices[7] = new VertexPositionColor(new Vector3(5, 0, -5), color);

     Vector3 max = vertices[0].Position;
     Vector3 min = vertices[0].Position;

     foreach(VertexPositionColor vc in vertices)
     {
        min = MinResult(min, vc.Position);
        max = MaxResult(max, vc.Position);
     }

     collisionBox = new BoundingBox(min, max);

     vBuffer = new VertexBuffer(gd, typeof(VertexPositionColor), 8, BufferUsage.WriteOnly);
     vBuffer.SetData<VertexPositionColor>(vertices);
  }

I tried this with a camera position of [0,0,0] and it returned true for a collision between the two boxes. 我尝试使用[0,0,0]的相机位置进行此操作,并且由于两个框之间发生碰撞而返回true。

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

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