Ejemplo n.º 1
0
        private void DoCollisionDetectionWithTerrain(IPhysicsObject physicsObject)
        {
            Vector3 position = physicsObject.GetPosition();

            if (game.terrain.heightMapInfo.IsOnHeightmap(position))
            {
                float objectBottom = physicsObject.GetBoundingBox().Min.Y;

                //Utility.BoundingBoxRenderer.Render(game, physicsObject.GetBoundingBox(), game.GraphicsDevice, game.worldCamera.ViewMatrix, game.worldCamera.ProjectionMatrix, Color.White);

                Vector3 v = physicsObject.GetVelocity();

                // In the bounding box, the bottom corners have indices 2,3,6,7. We're
                // interested in checking just the collisions of the bottom corners of
                // the object with the terrain.

                Vector3[] corners = physicsObject.GetBoundingBox().GetCorners();
                int[] bottomCorners = { 2, 3, 6, 7 };
                foreach (int i in bottomCorners)
                {
                    float terrainElevation;
                    Vector3 terrainNormal;

                    if (game.terrain.heightMapInfo.IsOnHeightmap(corners[i]))
                    {
                        game.terrain.heightMapInfo.GetHeightAndNormal(corners[i], out terrainElevation, out terrainNormal);
                        if (corners[i].Y <= terrainElevation)
                        {
                            HandleCollisionWithSurface(physicsObject, terrainNormal, 0.7f);

                            // Notify the physics object that a collision with the terrain had occurred.
                            physicsObject.HandleCollisionWithTerrain();

                            return;
                        }
                    }
                }
            }
        }