public void TestIntersectsBox()
 {
     //   x
     //  /
     // x
     var cylinder = new BoundingCylinder(Vector3.Zero, Vector3.One * 10, 3);
     var doesNotIntersect = new BoundingBox(Vector3.One * 10 + 5, Vector3.One * 10 + 5);
     Assert.IsFalse(cylinder.Intersects(doesNotIntersect));
     var intersects = new BoundingBox(Vector3.Zero, Vector3.One);
     Assert.IsTrue(cylinder.Intersects(intersects));
 }
Exemple #2
0
        public void TestIntersectsBox()
        {
            //   x
            //  /
            // x
            var cylinder         = new BoundingCylinder(Vector3.Zero, Vector3.One * 10, 3);
            var doesNotIntersect = new BoundingBox(Vector3.One * 10f + new Vector3(5, 5, 5), Vector3.One * 10f + new Vector3(5, 5, 5));

            Assert.IsFalse(cylinder.Intersects(doesNotIntersect));
            var intersects = new BoundingBox(Vector3.Zero, Vector3.One);

            Assert.IsTrue(cylinder.Intersects(intersects));
        }
        public bool IntersectsAny(DrawableGameComponent component, Vector3 offset)
        {
            BoundingCylinder cylinder = component.GetBoundingCylinder();

            cylinder.Point += offset;

            return(vegetationList.Any(v => cylinder.Intersects(v.GetBoundingCylinder())));
        }
Exemple #4
0
        private bool TestTerrainCollisionCylinder(Vector3 velocity, out Vector3 adjustedVelocity, List <ColoredBoundingBox> boxes)
        {
            adjustedVelocity = velocity;

            var testBox = GetAABBVelocityBox(velocity);

            var testCylinder = new BoundingCylinder(testBox.Min, testBox.Max,
                                                    Entity.Width / 2f);

            bool collision = false;

            for (int x = (int)(Math.Floor(testBox.Min.X)); x <= (int)(Math.Ceiling(testBox.Max.X)); x++)
            {
                for (int z = (int)(Math.Floor(testBox.Min.Z)); z <= (int)(Math.Ceiling(testBox.Max.Z)); z++)
                {
                    for (int y = (int)(Math.Floor(testBox.Min.Y)); y <= (int)(Math.Ceiling(testBox.Max.Y)); y++)
                    {
                        var blockState = Entity.Level.GetBlockState(x, y, z);
                        if (!blockState.Block.Solid)
                        {
                            continue;
                        }

                        var coords = new Vector3(x, y, z);

                        foreach (var box in blockState.Block.GetBoundingBoxes(coords))
                        {
                            if (testCylinder.Intersects(box))
                            {
                                if (testBox.Intersects(box))
                                {
                                    if (boxes != null)
                                    {
                                        boxes.Add(new ColoredBoundingBox(box, Color.Orange));
                                    }

                                    collision = true;

                                    AdjustVelocityForCollision(ref velocity, box);

                                    testBox      = GetAABBVelocityBox(velocity);
                                    testCylinder = new BoundingCylinder(
                                        testBox.Min, testBox.Max,
                                        Entity.Width / 2f);

                                    adjustedVelocity = velocity;
                                }
                            }
                        }
                    }
                }
            }

            return(collision);
        }
Exemple #5
0
        public bool TestTerrainCollisionCylinder(IAABBEntity entity, out Vector3 collisionPoint)
        {
            collisionPoint = Vector3.Zero;
            var testBox      = GetAABBVelocityBox(entity);
            var testCylinder = new BoundingCylinder(testBox.Min, testBox.Max,
                                                    entity.BoundingBox.Min.DistanceTo(entity.BoundingBox.Max));

            var collision = false;

            for (var x = (int)Math.Floor(testBox.Min.X); x <= (int)Math.Ceiling(testBox.Max.X); x++)
            {
                for (var z = (int)Math.Floor(testBox.Min.Z); z <= (int)Math.Ceiling(testBox.Max.Z); z++)
                {
                    for (var y = (int)Math.Floor(testBox.Min.Y); y <= (int)Math.Ceiling(testBox.Max.Y); y++)
                    {
                        var coords = new Coordinates3D(x, y, z);
                        if (!World.IsValidPosition(coords))
                        {
                            continue;
                        }

                        var _box = BlockPhysicsProvider.GetBoundingBox(World, coords);
                        if (_box == null)
                        {
                            continue;
                        }

                        var box = _box.Value.OffsetBy(coords.AsVector3());
                        if (testCylinder.Intersects(box))
                        {
                            if (testBox.Intersects(box))
                            {
                                collision = true;
                                AdjustVelocityForCollision(entity, box);
                                testBox      = GetAABBVelocityBox(entity);
                                testCylinder = new BoundingCylinder(testBox.Min, testBox.Max,
                                                                    entity.BoundingBox.Min.DistanceTo(entity.BoundingBox.Max));
                                collisionPoint = coords.AsVector3();
                            }
                        }
                    }
                }
            }

            return(collision);
        }
Exemple #6
0
        public bool TestTerrainCollisionCylinder(Entity entity, out Vector3 collisionPoint)
        {
            collisionPoint = Vector3.Zero;
            var testBox      = GetAABBVelocityBox(entity);
            var testCylinder = new BoundingCylinder(testBox.Min, testBox.Max,
                                                    Vector3.Distance(entity.BoundingBox.Min, entity.BoundingBox.Max));

            bool collision = false;

            for (int x = (int)(Math.Floor(testBox.Min.X)); x <= (int)(Math.Ceiling(testBox.Max.X)); x++)
            {
                for (int z = (int)(Math.Floor(testBox.Min.Z)); z <= (int)(Math.Ceiling(testBox.Max.Z)); z++)
                {
                    for (int y = (int)(Math.Floor(testBox.Min.Y)); y <= (int)(Math.Ceiling(testBox.Max.Y)); y++)
                    {
                        var blockState = entity.Level.GetBlockState(x, y, z);
                        if (blockState?.Model == null || !blockState.Block.Solid)
                        {
                            continue;
                        }

                        var coords = new Vector3(x, y, z);

                        foreach (var box in blockState.Model.GetBoundingBoxes(coords))
                        {
                            if (testCylinder.Intersects(box))
                            {
                                if (testBox.Intersects(box))
                                {
                                    collision = true;
                                    AdjustVelocityForCollision(entity, box);
                                    testBox = GetAABBVelocityBox(entity);

                                    testCylinder = new BoundingCylinder(
                                        testBox.Min, testBox.Max,
                                        Vector3.Distance(entity.BoundingBox.Min, entity.BoundingBox.Max));

                                    collisionPoint = coords;
                                }
                            }
                        }
                    }
                }
            }
            return(collision);
        }
 public void TestIntersectsPoint()
 {
     //   x
     //  /
     // x
     var cylinder = new BoundingCylinder(Vector3.Zero, Vector3.One, 1);
     Assert.IsTrue(cylinder.Intersects(cylinder.Min));
     Assert.IsTrue(cylinder.Intersects(cylinder.Max));
     Assert.IsTrue(cylinder.Intersects(cylinder.Min + (Vector3.One / 2)));
     Assert.IsTrue(cylinder.Intersects(cylinder.Max - (Vector3.One / 2)));
     Assert.IsTrue(cylinder.Intersects(new Vector3(0.25, 0, 0)));
     Assert.IsFalse(cylinder.Intersects(new Vector3(5, 5, 5)));
 }
Exemple #8
0
        public void TestIntersectsPoint()
        {
            //   x
            //  /
            // x
            var cylinder = new BoundingCylinder(Vector3.Zero, Vector3.One, 1);

            Assert.IsTrue(cylinder.Intersects(cylinder.Min));
            Assert.IsTrue(cylinder.Intersects(cylinder.Max));
            Assert.IsTrue(cylinder.Intersects(cylinder.Min + Vector3.One / 2));
            Assert.IsTrue(cylinder.Intersects(cylinder.Max - Vector3.One / 2));
            Assert.IsTrue(cylinder.Intersects(new Vector3(0.25f, 0, 0)));
            Assert.IsFalse(cylinder.Intersects(new Vector3(5, 5, 5)));
        }
Exemple #9
0
        public bool TestTerrainCollisionCylinder(IAABBEntity entity, out Vector3 collisionPoint)
        {
            collisionPoint = Vector3.Zero;
            var testBox = GetAABBVelocityBox(entity);
            var testCylinder = new BoundingCylinder(testBox.Min, testBox.Max,
                entity.BoundingBox.Min.DistanceTo(entity.BoundingBox.Max));

            bool collision = false;
            for (int x = (int)(Math.Floor(testBox.Min.X)); x <= (int)(Math.Ceiling(testBox.Max.X)); x++)
            {
                for (int z = (int)(Math.Floor(testBox.Min.Z)); z <= (int)(Math.Ceiling(testBox.Max.Z)); z++)
                {
                    for (int y = (int)(Math.Floor(testBox.Min.Y)); y <= (int)(Math.Ceiling(testBox.Max.Y)); y++)
                    {
                        var coords = new Coordinates3D(x, y, z);
                        if (!World.IsValidPosition(coords))
                            continue;

                        var _box = BlockPhysicsProvider.GetBoundingBox(World, coords);
                        if (_box == null)
                            continue;

                        var box = _box.Value.OffsetBy(coords);
                        if (testCylinder.Intersects(box))
                        {
                            if (testBox.Intersects(box))
                            {
                                collision = true;
                                AdjustVelocityForCollision(entity, box);
                                testBox = GetAABBVelocityBox(entity);
                                testCylinder = new BoundingCylinder(testBox.Min, testBox.Max,
                                    entity.BoundingBox.Min.DistanceTo(entity.BoundingBox.Max));
                                collisionPoint = coords;
                            }
                        }
                    }
                }
            }
            return collision;
        }