コード例 #1
0
ファイル: Weapon.cs プロジェクト: Jypeli-JYU/Jypeli
        /// <summary>
        /// Lisää törmäyksenkäsittelijän ammukselle
        /// </summary>
        /// <param name="projectile">Ammus</param>
        /// <param name="handler">Käsittelijä</param>
        protected void SetCollisionHandler(PhysicsObject projectile, CollisionHandler <PhysicsObject, PhysicsObject> handler)
        {
            if (handler == null)
            {
                return;
            }

            if (Game.Instance is PhysicsGameBase)
            {
                PhysicsGameBase pg = (PhysicsGameBase)Game.Instance;
                pg.AddCollisionHandler(projectile, handler);
            }
            else
            {
                throw new InvalidOperationException("Cannot set a collision handler to non-physics game!");
            }
        }
コード例 #2
0
ファイル: Tank.cs プロジェクト: Jypeli-JYU/Jypeli
        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);
            }
        }
コード例 #3
0
    private void updateGravity()
    {
        PhysicsGameBase physGame = Game.Instance as PhysicsGameBase;

        if (physGame == null)
        {
            return;
        }

        if (physGame.Gravity != _cachedGravity)
        {
            _cachedGravity          = physGame.Gravity;
            _cachedGravityMagnitude = _cachedGravity.Magnitude;
            _cachedGravityNormal    = _cachedGravity / _cachedGravityMagnitude;

#if VISUALIZE
            ssurface.Angle = gravityNormal.LeftNormal.Angle;
#endif
        }
    }
コード例 #4
0
    private void RemoveCollisionHelpers()
    {
        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.Remove(collisionHelpers[i].Object);
        }

        physicsGame.RemoveCollisionHandlers(this, null, null, null);

        for (int i = 0; i < collisionHelpers.Length; i++)
        {
            physicsGame.RemoveProtectedCollisionHandlers(collisionHelpers[i].Object, null, null, null);
        }
    }
コード例 #5
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);
        }
    }
コード例 #6
0
    private void AddCollisionHandler()
    {
        PhysicsGameBase physicsGame = Game.Instance as PhysicsGameBase;

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

        this.Body.Colliding += delegate(IPhysicsBody o1, IPhysicsBody o2, Collision c)
        {
            PhysicsObject other  = (PhysicsObject)o2.Owner;
            Vector        normal = c.Contacts.First().Normal;
            OnColliding(this, other, normal);
        };

        #if VISUALIZE
        ssurface.Width = 2 * Math.Sqrt(Game.Level.Width * Game.Level.Width + Game.Level.Height * Game.Level.Height);
        Game.Instance.Add(ssurface);
        #endif
    }