Esempio n. 1
0
        public ContainmentType Contains(BoundingPolygon polygon)
        {
            ContainmentType result;

            Contains(ref polygon, out result);
            return(result);
        }
        public bool Intersects(BoundingPolygon polygon)
        {
            bool result;

            polygon.Intersects(ref this, out result);
            return(result);
        }
Esempio n. 3
0
        public bool Intersects(BoundingPolygon polygon)
        {
            bool result;

            Intersects(ref polygon, out result);
            return(result);
        }
Esempio n. 4
0
 public bool Intersects(BoundingPolygon polygon)
 {
     if (polygon == null) { throw new ArgumentNullException("polygon"); }
     bool result;
     polygon.Intersects(ref this, out result);
     return result;
 }
Esempio n. 5
0
        public void Intersects(ref BoundingPolygon polygon, out bool result)
        {
            if (polygon == null)
            {
                throw new ArgumentNullException("polygon");
            }
            Vector2D[] vertexes = polygon.Vertexes;
            Scalar     distance;

            GetDistance(ref vertexes[0], out distance);

            int sign = Math.Sign(distance);

            result = false;
            for (int index = 1; index < vertexes.Length; ++index)
            {
                GetDistance(ref vertexes[index], out distance);

                if (Math.Sign(distance) != sign)
                {
                    result = true;
                    break;
                }
            }
        }
Esempio n. 6
0
 public void Intersects(ref BoundingPolygon polygon, out bool result)
 {
     if (polygon == null)
     {
         throw new ArgumentNullException("polygon");
     }
     polygon.Intersects(ref this, out result);
 }
Esempio n. 7
0
 public void Contains(ref BoundingPolygon polygon, out ContainmentType result)
 {
     if (polygon == null)
     {
         throw new ArgumentNullException("polygon");
     }
     Contains(polygon.vertexes, out result);
 }
 public static void FromVectors(Vector2D[] vertexes, out BoundingCircle result)
 {
     BoundingPolygon.GetCentroid(vertexes, out result.Position);
     result.Radius = -1;
     for (int index = 0; index < vertexes.Length; ++index)
     {
         Scalar distSq;
         Vector2D.DistanceSq(ref result.Position, ref vertexes[index], out distSq);
         if (result.Radius == -1 || (distSq < result.Radius))
         {
             result.Radius = distSq;
         }
     }
     result.Radius = MathHelper.Sqrt(result.Radius);
 }
Esempio n. 9
0
 public void Contains(ref BoundingPolygon polygon, out ContainmentType result)
 {
     if (polygon == null) { throw new ArgumentNullException("polygon"); }
     Vector2D[] vertexes = polygon.Vertexes;
     result = ContainmentType.Unknown;
     for (int index = 0; index < vertexes.Length && result != ContainmentType.Intersects; ++index)
     {
         ContainmentType con;
         Contains(ref vertexes[index], out con);
         result |= con;
     }
     if (result == ContainmentType.Disjoint)
     {
         bool test;
         polygon.Intersects(ref this, out test);
         if (test)
         {
             result = ContainmentType.Intersects;
         }
     }
 }
Esempio n. 10
0
 public void Contains(ref BoundingPolygon polygon, out ContainmentType result)
 {
     if (polygon == null) { throw new ArgumentNullException("polygon"); }
     Contains(polygon.vertexes, out result);
 }
Esempio n. 11
0
 public ContainmentType Contains(BoundingPolygon polygon)
 {
     ContainmentType result;
     Contains(ref polygon, out result);
     return result;
 }
Esempio n. 12
0
        void CreateAvatar()
        {
            Lifespan avatarLifespan = new Lifespan();

            Sprite sprite = GetSprite("tank.png");
            Vector2D[][] polygons = sprite.Polygons;
            MultiPolygonShape shape = new MultiPolygonShape(polygons, 4);
            shape.Tag = sprite;

            ObjectIgnorer ignorer = new ObjectIgnorer();
            Body a = new Body(new PhysicsState(new ALVector2D(0, 0, 0)),
                shape,
                300,//new MassInfo(40, Scalar.PositiveInfinity),
                coefficients.Duplicate(),
                avatarLifespan);
            a.Updated += new EventHandler<UpdatedEventArgs>(avatar_Updated);
            avatarBodies = new List<Body>();
            avatarOffsets = new List<Vector2D>();
            avatarJoints = new List<Joint>();
            avatarBodies.Add(a);
            a.CollisionIgnorer = ignorer;




            Scalar wheelSize = 18;
            Scalar wheelSpacing = -9;
            Scalar lenghtPercent = .84f;
            Matrix2x3 ident = Matrix2x3.Identity;
            BoundingRectangle rect;
            shape.CalcBoundingRectangle(ref ident, out rect);
            Scalar y = (rect.Max.Y +4)  ;
            Body lastWheel = null ;
            BoundingPolygon polygon = new BoundingPolygon(polygons[0]);

            Ray ray2 = new Ray(new Vector2D(rect.Max.X, y), -Vector2D.YAxis);
            Scalar y3 = y - polygon.Intersects(ray2);
            avatarBarrelOffset = new Vector2D(rect.Max.X, y3);
            
            for (Scalar x = rect.Min.X + wheelSize ; x < (rect.Max.X - wheelSize ) * lenghtPercent; x += (wheelSize*2 + wheelSpacing))
            {

                Ray ray = new Ray(new Vector2D(x, y), -Vector2D.YAxis);
                Scalar y2 = y-  polygon.Intersects(ray);



                Vector2D offset = new Vector2D(x, y2);

                Body wheel = new Body(
                    new PhysicsState(new ALVector2D(0, offset)),
                    new CircleShape(wheelSize, 30),
                    10,
                    new Coefficients(0,3),//  coefficients.Duplicate(),
                    avatarLifespan);
                HingeJoint joint = new HingeJoint(a, wheel, offset, avatarLifespan);
                joint.Softness = .1f;
                wheel.CollisionIgnorer = ignorer;

                if (lastWheel != null)
                {
                    AngleJoint joint2 = new AngleJoint(lastWheel, wheel, avatarLifespan);
                    avatarJoints.Add(joint2);
                }

                avatarJoints.Add(joint);
                avatarOffsets.Add(offset);
                avatarBodies.Add(wheel);
                lastWheel = wheel;
            }
        }
 bool IRaySegmentsCollidable.TryGetRayCollision(Body thisBody, Body raysBody, RaySegmentsShape raySegments, out RaySegmentIntersectionInfo info)
 {
     bool intersects = false;
     RaySegment[] segments = raySegments.Segments;
     Scalar[] result = new Scalar[segments.Length];
     Scalar temp;
     Vector2D[][] polygons = this.Polygons;
     for (int index = 0; index < segments.Length; ++index)
     {
         result[index] = -1;
     }
     Matrix2x3 matrix = raysBody.Matrices.ToBody * thisBody.Matrices.ToWorld;
     for (int polyIndex = 0; polyIndex < polygons.Length; ++polyIndex)
     {
         Vector2D[] unTrans = polygons[polyIndex];
         Vector2D[] polygon = new Vector2D[unTrans.Length];
         for (int index = 0; index < unTrans.Length; ++index)
         {
             Vector2D.Transform(ref matrix, ref unTrans[index], out polygon[index]);
         }
         BoundingRectangle rect;
         BoundingRectangle.FromVectors(polygon, out rect);
         BoundingPolygon poly = new BoundingPolygon(polygon);
         for (int index = 0; index < segments.Length; ++index)
         {
             RaySegment segment = segments[index];
             rect.Intersects(ref segment.RayInstance, out temp);
             if (temp >= 0 && temp <= segment.Length)
             {
                 poly.Intersects(ref segment.RayInstance, out temp);
                 if (temp >= 0 && temp <= segment.Length)
                 {
                     if (result[index] == -1 || temp < result[index])
                     {
                         result[index] = temp;
                     }
                     intersects = true;
                 }
             }
         }
     }
     if (intersects)
     {
         info = new RaySegmentIntersectionInfo(result);
     }
     else
     {
         info = null;
     }
     return intersects;
 }
Esempio n. 14
0
 public void Intersects(ref BoundingPolygon polygon, out bool result)
 {
     polygon.Intersects(ref this, out result);
 }
Esempio n. 15
0
 public bool Intersects(BoundingPolygon polygon)
 {
     bool result;
     Intersects(ref polygon, out result);
     return result;
 }
Esempio n. 16
0
 public void Intersects(ref BoundingPolygon polygon, out bool result)
 {
     if (polygon == null) { throw new ArgumentNullException("polygon"); }
     Intersects(this.vertexes, polygon.vertexes, out result);
 }
Esempio n. 17
0
        public static DisposeCallback CreateTank(DemoOpenInfo info, Vector2D position,List<Body> result)
        {
            Lifespan avatarLifespan = new Lifespan();

            IShape shape = ShapeFactory.CreateSprite(Cache<SurfacePolygons>.GetItem("tank.png"), 4, 18, 2);

            ObjectIgnorer ignorer = new ObjectIgnorer();
            Body tankBody = new Body(new PhysicsState(new ALVector2D(0, 0, 0)),
                shape,
                300,//new MassInfo(40, Scalar.PositiveInfinity),
                new Coefficients(0, 1),
                avatarLifespan);
            result.Add(tankBody);
            tankBody.State.Position.Linear += position;
            tankBody.ApplyPosition();

            tankBody.CollisionIgnorer = ignorer;
            BodyGraphic graphic = CreateGraphic(tankBody);
            graphic.ZOrder = 2;
            info.Scene.AddGraphic(graphic);

            Scalar wheelSize = 18;
            Scalar wheelSpacing = -9;
            Scalar lenghtPercent = .84f;
            Matrix2x3 ident = Matrix2x3.Identity;
            BoundingRectangle rect;
            shape.CalcBoundingRectangle(ref ident, out rect);
            Scalar y = (rect.Max.Y + 4);
            Body lastWheel = null;
            BoundingPolygon polygon = new BoundingPolygon(shape.Vertexes);

            Ray ray2 = new Ray(new Vector2D(rect.Max.X, y), -Vector2D.YAxis);
            Scalar y3 = y - polygon.Intersects(ray2);
            Vector2D avatarBarrelOffset = new Vector2D(rect.Max.X + 10, y3);

            CircleShape wheelShape = ShapeFactory.CreateColoredCircle(wheelSize, 30);
            Scalar force = 0;

            for (Scalar x = rect.Min.X + wheelSize; x < (rect.Max.X - wheelSize) * lenghtPercent; x += (wheelSize * 2 + wheelSpacing))
            {

                Ray ray = new Ray(new Vector2D(x, y), -Vector2D.YAxis);
                Scalar y2 = y - polygon.Intersects(ray);



                Vector2D offset = new Vector2D(x, y2);

                Body wheel = new Body(
                    new PhysicsState(new ALVector2D(0, offset + position)),
                    wheelShape,
                    10,
                    new Coefficients(0, 3),//  coefficients.Duplicate(),
                    avatarLifespan);
                result.Add(wheel);

                wheel.CollisionIgnorer = ignorer;
                wheel.AngularDamping = .9f;
                wheel.Updated += delegate(object sender, UpdatedEventArgs e)
                {
                    wheel.State.ForceAccumulator.Angular += force;
                };
                info.Scene.AddGraphic(CreateGraphic(wheel));

                HingeJoint joint = new HingeJoint(tankBody, wheel, offset + position, avatarLifespan);
                joint.Softness = .1f;
                info.Scene.Engine.AddJoint(joint);

                if (lastWheel != null)
                {
                    AngleJoint joint2 = new AngleJoint(lastWheel, wheel, avatarLifespan);
                    info.Scene.Engine.AddJoint(joint2);
                }
                lastWheel = wheel;
            }


            CircleShape weaponShape = ShapeFactory.CreateColoredCircle(5, 8);

            //now begins the abuse of anominous delegates (BIG TIME)

            EventHandler<KeyboardEventArgs> keyDownHandler = delegate(object sender, KeyboardEventArgs e)
            {
                switch (e.Key)
                {
                    case Key.LeftArrow:
                        force = -1500000;
                        break;
                    case Key.RightArrow:
                        force = 1500000;
                        break;
                    case Key.Space:

                        Scalar velocity = 2000;

                        Matrix2x3 toWorld = tankBody.Matrices.ToWorld;
                        Matrix2x2 toWorldNormal = tankBody.Matrices.ToWorldNormal;

                        //  Matrix2D mat = avatarBodies[0].Matrices.ToWorld;
                        Vector2D direction = toWorldNormal * Vector2D.XAxis;
                        PhysicsState state = new PhysicsState();
                        state.Position.Linear = toWorld * (avatarBarrelOffset);
                        state.Velocity.Linear = velocity * direction + tankBody.State.Velocity.Linear;

                        Body weapon = new Body(state,
                            weaponShape,
                            5,
                            new Coefficients(1, 1),
                            new Lifespan(10));
                        //weapon.CollisionIgnorer = tankBody.CollisionIgnorer;

                        weapon.Collided += delegate(object sender2, CollisionEventArgs e2)
                        {
                            if (!weapon.Lifetime.IsExpired)
                            {
                                weapon.Lifetime.IsExpired = true;
                                AddParticles(info, weapon.State.Position.Linear, weapon.State.Velocity.Linear * .5f, 50);
                            }
                        };

                        //  weapon.Collided += weapon_Collided;
                        tankBody.State.Velocity.Linear -= (velocity * weapon.Mass.Mass * tankBody.Mass.MassInv) * direction;
                        info.Scene.AddGraphic(CreateGraphic(weapon));
                        break;
                }
            };
            EventHandler<KeyboardEventArgs> keyUpHandler = delegate(object sender, KeyboardEventArgs e)
            {
                switch (e.Key)
                {
                    case Key.LeftArrow:
                        force = 0;
                        break;
                    case Key.RightArrow:
                        force = 0;
                        break;
                }
            };
            Events.KeyboardDown += keyDownHandler;
            Events.KeyboardUp += keyUpHandler;

            return delegate()
            {
                Events.KeyboardDown -= keyDownHandler;
                Events.KeyboardUp -= keyUpHandler;
            };
        }