예제 #1
0
        public static FixtureCoord[] GetFixtureCircleIntersections(World world, Vector2 point, float radius)
        {
            List<Fixture> potentials = new List<Fixture>();
            var box = new FarseerPhysics.Collision.AABB(new Xna.Vector2(point.X, point.Y), radius * 2, radius * 2);
            world.QueryAABB(delegate (Fixture fixture)
            {
                potentials.Add(fixture);
                return false;
            }, ref box);

            List<FixtureCoord> collisions = new List<FixtureCoord>();
            foreach (Fixture f in potentials)
            {
                Xna.Vector2 relativePoint = f.Body.GetLocalPoint(new Xna.Vector2(point.X, point.Y));
                switch (f.Shape.ShapeType)
                {
                    case ShapeType.Polygon:
                        PolygonShape polygon = (PolygonShape)f.Shape;
                        for (int i = 0; i < polygon.Vertices.Count; i++)
                        {
                            int iNext = (i + 1) % polygon.Vertices.Count;
                            LineF edge = new LineF(polygon.Vertices[i], polygon.Vertices[iNext]);
                            IntersectCoord[] intersects = MathExt.LineCircleIntersect(new Vector2(relativePoint.X, relativePoint.Y), radius, edge, true);
                            for (int j = 0; i < intersects.Length; i++)
                            {
                                collisions.Add(new FixtureCoord(f, i, (float)intersects[j].TFirst));
                            }
                            /*if (intersects.Length > 0)
                            {
                                fixtureCollisions.Add(f);
                                break;
                            }*/
                        }
                        break;

                    default:
                        throw new NotImplementedException();
                }
            }
            return collisions.ToArray();
        }
예제 #2
0
        private void FindTarget(World world)
        {
            Vector2 currentPosition = Owner.Position;
            closestTarget = Owner.PlayWindow.GoodCellList.First();
            closestDistance = (currentPosition - closestTarget.Position).LengthSquared();
            float boxSideStep = 2;
            targetFound = false;
            while (boxSideStep < 64 && targetFound == false)
            {
                AABB boundingBox = new AABB(ConvertUnits.ToSimUnits(currentPosition), boxSideStep, boxSideStep);

                world.QueryAABB(AABBCollision, ref boundingBox);

                boxSideStep *= 4;
            }
            _target = closestTarget;
        }