Пример #1
0
 public void AddTorque(UnityEngine.Vector3 torque)
 {
     if (isInWorld)
     {
         m_Brigidbody.ApplyTorque(torque.ToBullet());
     }
 }
Пример #2
0
        public void PhysicsPreStep(float timeStep)
        {
            float newSteering = 0.0f;
            float accelerator = 0.0f;

            // Read controls
            if (Controls.IsDown(CtrlLeft))
            {
                newSteering = -1.0f;
            }
            if (Controls.IsDown(CtrlRight))
            {
                newSteering = 1.0f;
            }
            if (Controls.IsDown(CtrlForward))
            {
                accelerator = 1.0f;
            }
            if (Controls.IsDown(CtrlBack))
            {
                accelerator = -0.5f;
            }

            // When steering, wake up the wheel rigidbodies so that their orientation is updated
            if (newSteering != 0.0f)
            {
                frontLeftBody.Activate();
                frontRightBody.Activate();
                steering = steering * 0.95f + newSteering * 0.05f;
            }
            else
            {
                steering = steering * 0.8f + newSteering * 0.2f;
            }

            // Set front wheel angles
            Quaternion steeringRot = new Quaternion(0, steering * MaxWheelAngle, 0);

            frontLeftAxis.SetOtherAxis(steeringRot * new Vector3(-1f, 0f, 0f));
            frontRightAxis.SetOtherAxis(steeringRot * Vector3.UnitX);

            Quaternion hullRot = hullBody.Rotation;

            if (accelerator != 0.0f)
            {
                // Torques are applied in world space, so need to take the vehicle & wheel rotation into account
                Vector3 torqueVec = new Vector3(EnginePower * accelerator, 0.0f, 0.0f);

                frontLeftBody.ApplyTorque(hullRot * steeringRot * torqueVec);
                frontRightBody.ApplyTorque(hullRot * steeringRot * torqueVec);
                rearLeftBody.ApplyTorque(hullRot * torqueVec);
                rearRightBody.ApplyTorque(hullRot * torqueVec);
            }

            // Apply downforce proportional to velocity
            Vector3 localVelocity = Quaternion.Invert(hullRot) * hullBody.LinearVelocity;

            hullBody.ApplyForce(hullRot * new Vector3(0f, -1f, 0f) * Math.Abs(localVelocity.Z) * DownForce);
        }
Пример #3
0
 public void AddTorque(Vector3 torque)
 {
     if (torque.LengthSquared > float.Epsilon)
     {
         RigidBody.Activate(true);
         RigidBody.ApplyTorque(torque.Cast());
     }
 }
Пример #4
0
        protected override void Apply(RigidBody obj, int slice)
        {
            Vector3D torque = this.FTorque[slice];

            obj.ApplyTorque(new Vector3((float)torque.x, (float)torque.y, (float)torque.z));
        }
Пример #5
0
        /// <summary>
        /// update override
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Update(GameTime gameTime)
        {
            if (Active)
            {
                var rotationDegrees = RigidBody.GetAngle() * 180 / System.Math.PI;

                if (Keyboard.GetState().IsKeyDown(Keys.OemOpenBrackets))
                {
                    if (Vec2.Distance(Vec2.Zero, RigidBody.GetLinearVelocity()) < GameData.PlayerMaxSpeed)
                    {
                        //apply impulse to push the player to left
                        var impulseVec = GameUtils.RotationToVec2((float)rotationDegrees - 90);
                        RigidBody.ApplyImpulse(impulseVec * GameData.PlayerLateralImpulse
                                               , RigidBody.GetPosition());
                    }
                }

                if (Keyboard.GetState().IsKeyDown(Keys.OemCloseBrackets))
                {
                    if (Vec2.Distance(Vec2.Zero, RigidBody.GetLinearVelocity()) < GameData.PlayerMaxSpeed)
                    {
                        //apply impulse to push the player to right
                        var impulseVec = GameUtils.RotationToVec2((float)rotationDegrees + 90);

                        RigidBody.ApplyImpulse(impulseVec * GameData.PlayerLateralImpulse
                                               , RigidBody.GetPosition());
                    }
                }

                if (FilteredInputListener.WasKeyPressed(Keys.Left))
                {
                    WeaponInventory.SelectPreviousWeapon();
                    FilteredInputListener.ResetKey(Keys.Left);
                }

                if (FilteredInputListener.WasKeyPressed(Keys.Right))
                {
                    WeaponInventory.SelectNextWeapon();
                    FilteredInputListener.ResetKey(Keys.Right);
                }

                if (Keyboard.GetState().IsKeyDown(Keys.W))
                {
                    if (Vec2.Distance(Vec2.Zero, RigidBody.GetLinearVelocity()) < GameData.PlayerMaxSpeed)
                    {
                        var impulseVec = GameUtils.RotationToVec2((float)rotationDegrees);
                        if (Vec2.Dot(impulseVec, RigidBody.GetLinearVelocity()) == 0)
                        {
                            RigidBody.SetLinearVelocity(Vec2.Zero);
                        }
                        RigidBody.ApplyImpulse(impulseVec * GameData.PlayerImpulse
                                               , RigidBody.GetPosition());
                    }
                }
                if (Keyboard.GetState().IsKeyDown(Keys.S))
                {
                    if (Vec2.Distance(Vec2.Zero, RigidBody.GetLinearVelocity()) < GameData.PlayerMaxSpeed)
                    {
                        var impulseVec = GameUtils.RotationToVec2((float)rotationDegrees);
                        RigidBody.ApplyImpulse(impulseVec * -GameData.PlayerImpulse
                                               , RigidBody.GetPosition());
                    }
                }
                if (Keyboard.GetState().IsKeyDown(Keys.D))
                {
                    //DecreaseLinearVelocity(GameData.PlayerTurnVelocityDecrement, 1);
                    RigidBody.ApplyTorque(GameData.PlayerTurnTorque);
                }
                if (Keyboard.GetState().IsKeyDown(Keys.A))
                {
                    //DecreaseLinearVelocity(GameData.PlayerTurnVelocityDecrement, 1);
                    RigidBody.ApplyTorque(-GameData.PlayerTurnTorque);
                }
                if (Keyboard.GetState().IsKeyDown(Keys.Space))
                {
                    var weapon = WeaponInventory.GetSelectedWeapon();
                    if (weapon != null && weapon.RemainingAmmo > 0)
                    {
                        if (DateTime.Now - _lastProjectileTime > TimeSpan.FromMilliseconds(100))
                        {
                            ShootingEffect.Play();

                            _lastProjectileTime = DateTime.Now;
                            WeaponInventory.DecreaseAmmo(1);
                            SpawnProjectile(weapon.ProjectileName, ProjectileSource.Player);
                        }
                    }
                }

                if (Keyboard.GetState().IsKeyUp(Keys.W) &&
                    Keyboard.GetState().IsKeyUp(Keys.A) &&
                    Keyboard.GetState().IsKeyUp(Keys.S) &&
                    Keyboard.GetState().IsKeyUp(Keys.D))
                {
                    DecreaseLinearVelocity(GameData.PlayerTurnVelocityDecrement, 0);
                }

                if (Keyboard.GetState().IsKeyUp(Keys.A) && Keyboard.GetState().IsKeyUp(Keys.D))
                {
                    RigidBody.SetAngularVelocity(0);
                }
            }
        }
Пример #6
0
        /// <summary>
        /// Adds the torque.
        /// </summary>
        /// <param name="torque">Torque.</param>
        public void AddTorque(int torque)
        {
            var vec = new Vector3(0, torque, 0);

            rb.ApplyTorque(vec);
        }