Example #1
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;
            }
        }