Ejemplo n.º 1
0
        private void AddWheels()
        {
            PhysicsGameBase pg = Game.Instance as PhysicsGameBase;

            if (pg == null)
            {
                throw new InvalidOperationException("Cannot have a tank in non-physics game");
            }

            const int wheelCount = 6;

            double r    = this.Width / (2 * wheelCount);
            double left = this.X - this.Width / 2 + r;

            double[] wheelYPositions = new double[wheelCount];
            for (int i = 0; i < wheelYPositions.Length; i++)
            {
                wheelYPositions[i] = this.Y - this.Height / 2;
            }
            wheelYPositions[0] = wheelYPositions[wheelCount - 1] = this.Position.Y - (this.Height * 3 / 8);

            for (int i = 0; i < wheelCount; i++)
            {
                PhysicsObject wheel = new PhysicsObject(2 * r, 2 * r, Shape.Circle);
                wheel.Color            = Color.Gray;
                wheel.CollisionIgnorer = this.CollisionIgnorer;
                wheels.Add(wheel);
                pg.Add(wheel);
                Vector axlePos = new Vector(left + i * (this.Width / wheelCount), wheelYPositions[i]);
                wheel.Position        = axlePos;
                wheel.Mass            = this.Mass / 20;
                wheel.KineticFriction = 1.0;
                wheel.AngularDamping  = 0.8;

                IMotorJoint joint = (IMotorJoint)pg.Engine.CreateJoint(this, wheel, JointTypes.WheelJoint);

                joint.Softness       = Mass * 10;
                joint.MaxMotorTorque = Mass * 50;
                joint.MotorEnabled   = false;
                joint.Axis           = Vector.UnitY;

                joints.Add(joint);
                pg.Add(joint);
            }
        }
Ejemplo n.º 2
0
    private void AddCollisionHelpers()
    {
        PhysicsGameBase physicsGame = Game.Instance as PhysicsGameBase;

        if (physicsGame == null)
        {
            throw new InvalidOperationException("Cannot have a platform character in non-physics game");
        }

        for (int i = 0; i < collisionHelpers.Length; i++)
        {
            physicsGame.Add(collisionHelpers[i].Object);
        }

        physicsGame.AddProtectedCollisionHandler <PhysicsObject, PhysicsObject>(this, OnCollision);

        for (int i = 0; i < collisionHelpers.Length; i++)
        {
            physicsGame.AddProtectedCollisionHandler <PhysicsObject, PhysicsObject>(collisionHelpers[i].Object, collisionHelpers[i].SetObjectBeingHit);
        }
    }