public void AddForceAtPosition(UnityEngine.Vector3 force, UnityEngine.Vector3 relativePostion) { if (isInWorld) { m_Brigidbody.ApplyForce(force.ToBullet(), relativePostion.ToBullet()); } }
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); }
protected override void Apply(RigidBody obj, int slice) { Vector3D pos = this.FPosition[slice]; Vector3D force = this.FForce[slice]; obj.ApplyForce(new Vector3((float)force.x, (float)force.y, (float)force.z), new Vector3((float)pos.x, (float)pos.y, (float)pos.z)); }
public void AddForce(Vector3 force) { if (force.LengthSquared > float.Epsilon) { RigidBody.Activate(true); RigidBody.ApplyForce(force.Cast(), BM.Vector3.Zero); } }
public override void Update(GameTime gameTime) { // Basic Movement if (direction != Vector2.Zero) { force = RigidBody.Mass * (acceleration * accelerationScaling); RigidBody.ApplyForce(force * new Vector2(direction.X, -direction.Y)); } base.Update(gameTime); }
public virtual void Shoot() { if (owner != null) { RigidBody.LinearDamping = 0.5f; RigidBody.ApplyForce(-_shootForce * new Vector2((float)Math.Cos(owner.Rotation), (float)Math.Sin(owner.Rotation))); Direction = Vector2.Zero; Release(); Library.Audio.PlaySoundEffect(Library.Audio.SoundEffects.Shoot); _cooldownTimer = 0; } }
public override void ApplyImpulse(Vector3 position, Vector3 force) { body.ApplyForce(force, position); }
public override void FixedUpdate(float timeStep) { //var timeStep = e.TimeStep; if (rigidBody_ == null) { rigidBody_ = node_.GetComponent <RigidBody>(false); } var animCtrl = modelNode_.GetComponent <AnimationController>(); jumpTimer_ += timeStep; crouchTimer_ += timeStep; // Update the in air timer. Reset if grounded if (!onGround_) { inAirTimer_ += timeStep; } else { inAirTimer_ = 0.0f; } // When character has been in air less than 1/10 second, it's still interpreted as being on ground bool softGrounded = (inAirTimer_ < INAIR_THRESHOLD_TIME); // Update movement & animation var rot = Node.Rotation; Vector3 moveDir = Vector3.Zero; var velocity = rigidBody_.LinearVelocity; // Velocity on the XZ plane var planeVelocity = new Vector3(velocity.X, 0, velocity.Z); float speed = planeVelocity.Length; if (controls_.IsDown(CTRL_FORWARD)) { moveDir += Vector3.Forward * 1.0f; } if (controls_.IsDown(CTRL_BACK)) { moveDir += Vector3.Back * 0.6f; } if (controls_.IsDown(CTRL_LEFT)) { moveDir += Vector3.Left * 0.75f; } if (controls_.IsDown(CTRL_RIGHT)) { moveDir += Vector3.Right * 0.75f; } // Normalize move vector so that diagonal strafing is not faster if (moveDir.LengthSquared > 1.0f) { moveDir.Normalize(); } if (controls_.IsDown(CTRL_SHIFT)) { moveDir *= 0.4f; } // Crouching CollisionShape shape = node_.GetComponent <CollisionShape>(); Node dropDetectNode = Node.GetChild("DropDetect", true); CollisionShape dropDetectShape = dropDetectNode.GetComponent <CollisionShape>(); Node standDetectNode = Node.GetChild("StandDetect", true); CollisionShape standDetectShape = standDetectNode.GetComponent <CollisionShape>(); Node groundDetectNode = Node.GetChild("GroundDetect", true); CollisionShape groundDetectShape = groundDetectNode.GetComponent <CollisionShape>(); bool crouch = ((controls_.IsDown(CTRL_CROUCH) && crouchTimer_ >= CROUCH_RECOVER) || !okToStand_);// || (!softGrounded && crouching_)); rigidBody_.DisableMassUpdate(); if (crouch) { if (softGrounded) { moveDir *= 0.3f; } dropDetectShape.Position = Lerp(dropDetectShape.Position, new Vector3(0.0f, 1.0f, 0.0f), timeStep * CROUCH_SPEED); standDetectShape.Position = Lerp(standDetectShape.Position, new Vector3(0.0f, 2.1f, 0.0f), timeStep * CROUCH_SPEED); groundDetectShape.Position = Lerp(groundDetectShape.Position, new Vector3(0.0f, 1.3f, 0.0f), timeStep * CROUCH_SPEED); shape.Position = Lerp(shape.Position, new Vector3(0.0f, 1.5f, 0.0f), timeStep * CROUCH_SPEED); shape.Size = Lerp(shape.Size, new Vector3(0.8f, 1.0f, 0.8f), timeStep * CROUCH_SPEED); crouching_ = true; } else { dropDetectShape.Position = Lerp(dropDetectShape.Position, new Vector3(0.0f, 0.0f, 0.0f), timeStep * CROUCH_SPEED); standDetectShape.Position = Lerp(standDetectShape.Position, new Vector3(0.0f, 1.1f, 0.0f), timeStep * CROUCH_SPEED); groundDetectShape.Position = Lerp(groundDetectShape.Position, new Vector3(0.0f, 0.3f, 0.0f), timeStep * CROUCH_SPEED); Vector3 pos = shape.Position; Vector3 newPos = Lerp(pos, new Vector3(0.0f, 1.0f, 0.0f), timeStep * CROUCH_SPEED); // Adjust rigid body position to prevent penetration into the ground when standing if (softGrounded && !dropDetected_) { rigidBody_.SetPosition((rigidBody_.Position - (newPos - pos) * 2.0f)); } else if (nearGround_) { rigidBody_.SetPosition((rigidBody_.Position - (newPos - pos) * 3.0f)); } shape.Position = (newPos); shape.Size = Lerp(shape.Size, new Vector3(0.8f, 2.0f, 0.8f), timeStep * CROUCH_SPEED); if (crouching_) { crouchTimer_ = 0.0f; } crouching_ = false; } rigidBody_.EnableMassUpdate(); // Prevent bounce bugs when colliding against corners, while still allowing movement up ramps if (highImpulseDetected_ && nearGround_) { Vector3 vel = (rigidBody_.LinearVelocity); rigidBody_.SetLinearVelocity(new Vector3(vel.X, Math.Min(vel.Y, 0.0f), vel.Z)); } // If in air, allow control, but slower than when on ground if (softGrounded && okToJump_) { float moveMag = (moveDir.Length); if (moveMag > 0.0001) { moveMag_ = Lerp(moveMag_, 1.0f, timeStep * MOVE_ACCELERATION); } else { moveMag_ = Lerp(moveMag_, 0.0f, timeStep * MOVE_DECELERATION); } velocity_ = Lerp(velocity_, rot * moveDir * moveMag_ * MOVE_FORCE, timeStep * AGILITY); rigidBody_.ApplyImpulse(velocity_); Vector3 brakeForce = (planeVelocity * -BRAKE_FORCE); rigidBody_.ApplyImpulse(brakeForce); } else { if (!(dropDetected_ && nearGround_)) { velocity_ = Lerp(velocity_, rot * moveDir * moveMag_ * INAIR_MOVE_FORCE, timeStep * INAIR_AGILITY); rigidBody_.ApplyImpulse(velocity_); Vector3 brakeForce = (planeVelocity * -INAIR_BRAKE_FORCE); rigidBody_.ApplyImpulse(brakeForce); } } // Jumping if (!controls_.IsDown(CTRL_JUMP)) { jumpReleased_ = true; } if (!okToJump_ && jumpTimer_ > JUMP_RECOVER && onGround_ && softGrounded && jumpReleased_) { okToJump_ = true; } if (softGrounded && !highImpulseDetected_) { if (controls_.IsDown(CTRL_JUMP) && okToJump_) { Vector3 vel = (rigidBody_.LinearVelocity); //URHO3D_LOGINFO("JUMP " + String(vel.y_)); rigidBody_.SetLinearVelocity(new Vector3(vel.X, Math.Max(vel.Y, 0.0f), vel.Z)); rigidBody_.ApplyImpulse(Vector3.Up * JUMP_FORCE); okToJump_ = false; jumpTimer_ = 0.0f; jumpReleased_ = false; } } // Keep the character close the ground / prevent launching from high speed movement if ((okToJump_ && inAirTimer_ > 0.0f && inAirTimer_ <= INAIR_THRESHOLD_TIME && speed > 0.1f && !dropDetected_) || (inAirTimer_ > INAIR_THRESHOLD_TIME && !dropDetected_)) { rigidBody_.ApplyImpulse(new Vector3(0.0f, -9.81f * speed, 0.0f)); } // Prevent sliding while on slopes rigidBody_.UseGravity = (!onGround_); // Add artificial gravity after some time to pull to ground faster if (inAirTimer_ > 0.1f && velocity_.Length > 0.1f) { rigidBody_.ApplyForce(new Vector3(0.0f, -9.81f * 2.0f, 0.0f)); } // Play walk animation if moving on ground, otherwise fade it out if (softGrounded && !moveDir.Equals(Vector3.Zero)) { animCtrl.PlayExclusive("Models/Jack_Walk.ani", 0, true, 0.2f); } else { animCtrl.Stop("Models/Jack_Walk.ani", 0.2f); } // Set walk animation speed proportional to velocity animCtrl.SetSpeed("Models/Jack_Walk.ani", speed / 9.0f); // Reset grounded flag for next frame onGround_ = false; nearGround_ = false; dropDetected_ = true; okToStand_ = true; highImpulseDetected_ = false; modelNode_.Position = Lerp(modelNode_.Position, rigidBody_.Position + rigidBody_.LinearVelocity * timeStep * 4.0f, timeStep * (10.0f + rigidBody_.LinearVelocity.Length / 8.0f )); modelNode_.Rotation = rot; }
/// <summary> /// Do on frame update. /// </summary> protected override void OnUpdate() { // if dead - disable controls if (IsDead) { return; } // decrease weapon cooldown if (_weaponCooldown > 0f) { _weaponCooldown -= Managers.TimeManager.TimeFactor; } // if hit fire key, fire weapon if (Managers.GameInput.IsKeyDown(GeonBit.Input.GameKeys.Fire)) { // make sure we have enough ammo and weapon is not currently cooling down. if (_weaponCooldown <= 0f && PlayerStatus.Ammo > 0) { // set cooldown and decrease ammo _weaponCooldown = 0.25f; _timeForNextAmmoRegen = TimeToRegenerateAmmo; PlayerStatus.Ammo--; // spawn bullets from player's position for (int i = -1; i <= 1; i += 2) { // create new bullet GameObject bullet = Managers.Prototypes.Spawn("bullet"); // update position and copy transformations from node to physical body bullet.SceneNode.Position = _GameObject.SceneNode.WorldPosition + Vector3.Left * 1.5f * i; bullet.GetComponent <RigidBody>().CopyNodeWorldMatrix(true); bullet.Parent = _GameObject.Parent; } } } // get physical body RigidBody body = _GameObject.GetComponent <RigidBody>(); // set movement speed factor float moveSpeed = 750f; // if currently moving ship, apply force in desired direction if (Managers.GameInput.MovementVector != Vector3.Zero) { body.ApplyForce(Managers.GameInput.MovementVector * moveSpeed); } // tilt spaceship left / right based on speed on X axis float tilt = body.LinearVelocity.X * 0.035f; if (tilt < -0.5f) { tilt = -0.5f; } if (tilt > 0.5f) { tilt = 0.5f; } _GameObject.Find("model", false).SceneNode.RotationZ = tilt; // Make sure player can't escape screen boundaries // get screen boundaries based on camera frustum BoundingFrustum frustum = Managers.ActiveScene.ActiveCamera.ViewFrustum; // get world position Vector3 worldPos = _GameObject.SceneNode.WorldPosition; // if try to escape from left side, apply force to bring the ship back to screen if (worldPos.X < frustum.Left.D) { body.ApplyForce(Vector3.Right * moveSpeed * 2f); } // if try to escape from right side, apply force to bring the ship back to screen if (worldPos.X > -frustum.Right.D) { body.ApplyForce(Vector3.Left * moveSpeed * 2f); } // if try to escape from top side, apply force to bring the ship back to screen if (worldPos.Z < frustum.Top.D) { body.ApplyForce(Vector3.Backward * moveSpeed * 2f); } // if try to escape from bottom side, apply force to bring the ship back to screen if (worldPos.Z > -frustum.Bottom.D) { body.ApplyForce(Vector3.Forward * moveSpeed * 2f); } }
// public Rigid(int mass, float inertia, float restitution){} /// <summary> /// Adds the force. /// </summary> /// <param name="engineForce">Engine force.</param> public void AddForce(int engineForce) { var vec = new Vector3(0, 0, engineForce); rb.ApplyForce(vec, Vector3.Zero); }