protected override void OnTick() { base.OnTick(); if (firstWay == true) { ActivateWayMovement(); } UpdateGeneralTask(); CalculateThings(); chassisBody = PhysicsModel.GetBody("main"); if (chassisBody == null) { Log.Error("Emrah Helli: \"main\" chassisBody dose not exists."); return; } float diff = Position.Z - flyHeight; float force = -diff - chassisBody.LinearVelocity.Z; MathFunctions.Clamp(ref force, -10, 10); chassisBody.AddForce(ForceType.GlobalAtLocalPos, TickDelta, new Vec3(0, 0, force * 3) * chassisBody.Mass, new Vec3(0, 0, 1)); chassisBody.AngularDamping = 1; this.flyHeight = TaskPosition.Z; float ZA = ZADiffAngle * 4; if (ZA < 20 && ZA > -20) { chassisBody.AngularVelocity = new Vec3 (chassisBody.AngularVelocity.X, chassisBody.AngularVelocity.Y, ZA); } else if (ZA > 20) { chassisBody.AngularVelocity = new Vec3 (chassisBody.AngularVelocity.X, chassisBody.AngularVelocity.Y, -2); } else if (ZA < -20) { chassisBody.AngularVelocity = new Vec3 (chassisBody.AngularVelocity.X, chassisBody.AngularVelocity.Y, 2); } chassisBody.AddForce(ForceType.LocalAtLocalPos, TickDelta, new Vec3(Velocity * chassisBody.Mass, 0, 0), new Vec3(-1, 0, 0)); chassisBody.LinearDamping = 1; //check outside Map position Bounds checkBounds = Map.Instance.InitialCollisionBounds; checkBounds.Expand(new Vec3(300, 300, 10000)); if (!checkBounds.IsContainsPoint(Position)) { SetDeleted(); } }
private void DoForce(Body otherbody, Body thisbody) { float velocity = 0; //if(MaxVelocity <= Velocity) //if(MaxVelocity <= Velocity) //)this.GroundRelativeVelocity.Length()) //{ velocity = this.GroundRelativeVelocity.Length() + 1000f; //(speedpersecpersec) //} //else //{ // if (velocity >= MaxVelocity) // velocity = MaxVelocity; // else if (Velocity < 1) // { // velocity = 0f; // } //} Vec3 vector = otherbody.Position - thisbody.Position; //vector.Z += 3f; vector.Normalize(); otherbody.AddForce(ForceType.GlobalAtLocalPos, TickDelta, vector * (velocity * 2), otherbody.Position); //thisbody.AddForce(ForceType.GlobalAtGlobalPos, TickDelta, -vector * velocity, thisbody.Position); }
void Update() { Body body = Utilities.GetBodyFromPosition(Input.mousePosition); if (body != null || selectedBody != null) { if (selectedBody) { body = selectedBody; } spriteRenderer.enabled = true; transform.position = body.position; transform.rotation = Quaternion.AngleAxis(Time.time * 90, Vector3.forward); transform.localScale = Vector2.one * body.shape.size * 1.2f; } else { spriteRenderer.enabled = false; } if (selectedBody) { Vector2 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition); if (selectedBody.type == Body.eType.Static) { selectedBody.position = pos; } if (selectedBody.type == Body.eType.Kinematic || selectedBody.type == Body.eType.Dynamic) { Vector2 force = Utilities.SpringForce(pos, selectedBody.position, 0, 5); selectedBody.AddForce(force, Body.eForceMode.Velocity); } } }
public void Move() { x = Mathf.Cos(Time.deltaTime); y = Mathf.Sin(Time.deltaTime); Body.AddForce(Vector2.up * new Vector2(x, y) * Time.deltaTime); }
public static void Resolve(List <Contact> contacts) { foreach (var contact in contacts) { Body bodyA = contact.bodyA; Body bodyB = contact.bodyB; //separation float TIM = bodyA.inverseMass + bodyB.inverseMass; //total inverse mass Vector2 separation = (contact.normal * contact.depth) / TIM; contact.bodyA.position += separation * bodyA.inverseMass; contact.bodyB.position -= separation * bodyB.inverseMass; //Collision impulse Vector2 relativeVelocity = bodyA.velocity - bodyB.velocity; float normalVelocity = Vector2.Dot(relativeVelocity, contact.normal); if (normalVelocity > 0) { continue; } float restitution = (bodyA.restitution + bodyB.restitution) * 0.5f; float impulseMagnitude = -(1.0f + restitution) * normalVelocity / TIM; Vector2 impulse = contact.normal * impulseMagnitude; bodyA.AddForce(bodyA.velocity + (impulse * bodyA.inverseMass), Body.eForceMode.Velocity); bodyB.AddForce(bodyB.velocity - (impulse * bodyB.inverseMass), Body.eForceMode.Velocity); } }
private void FixedUpdate() { if (Grounded && Input.GetAxisRaw("Jump") > 0) { Body.AddForce(transform.up * JumpStrength, ForceMode.VelocityChange); } }
// Update is called once per frame void Update() { if (Input.GetKey(KeyCode.Z)) { transform.Translate(Vector3.forward * LinearVelocity * Time.deltaTime); } if (Input.GetKey(KeyCode.S)) { transform.Translate(Vector3.back * LinearVelocity * Time.deltaTime); } if (Input.GetKey(KeyCode.Q)) { transform.Rotate(Vector3.up, -AngularVelocity * Time.deltaTime); } if (Input.GetKey(KeyCode.D)) { transform.Rotate(Vector3.up, AngularVelocity * Time.deltaTime); } if (Input.GetKey(KeyCode.Space) && canJump) { canJump = false; Invoke("ReuseJump", 2.0f); Body.AddForce(Vector3.up * 10.0f, ForceMode.Impulse); } if (HbScript.Health <= 0.0f) { SceneManager.LoadScene(0); } }
public void Jump() { if (!IsJumping && IsGrounded) { Body.AddForce(Vector2.up * jumpForce, ForceMode2D.Force); } }
/// <summary>Overridden from <see cref="Engine.EntitySystem.Entity.OnTick()"/>.</summary> protected override void OnTick() { CalculateRelativeVelocity(); base.OnTick(); TickContusionTime(); //if (Damager_Ball_Player != null && Damager_Ball_Player.LastStepLinearVelocity != null) // RelativeVelocity += Damager_Ball_Player.LastStepLinearVelocity / skilllevel; //else // RelativeVelocity = currentvelocity + (forward(strength)) + (right(strength)); float speed = GroundRelativeVelocity.Length(); if (speed >= 1) { speed = 1f; } if (Damager_Ball_Player != null) { Damager_Ball_Player.AddForce(ForceType.Global, TickDelta, new Vec3(0, 0, speed *= PhysicsGravity), Position); } TickMovement(); if (Intellect != null) { TickIntellect(Intellect); } //UpdateRotation(); //TickJump(false); //if (IsOnGround()) // onGroundTime += TickDelta; //else // onGroundTime = 0; //if (forceMoveVectorTimer != 0) // forceMoveVectorTimer--; //if (turret != null) // turretTimer += TickDelta; //can add remove after 10 seconds }
protected override void MovementPattern() { Body.velocity = new Vector2(1 * Direction, Body.velocity.y); if (Body.velocity.y == 0) { Body.AddForce(new Vector2(0, 300)); } }
protected override void FixedUpdate() { base.FixedUpdate(); Body.AddForce(m_Input.y * transform.up * m_ThrusterSpeed); Body.angularVelocity = m_Input.x * m_RotationSpeed; // Body.AddTorque(m_Input.x * m_RotationSpeed); }
private void OnDamaged() { Body.AddForce(Vector2.up * (JumpForce * 2)); if (GameoverOverlay == null) { GameoverOverlay = Instantiate(GameoverOverlayPrefab); } }
private void FixedUpdate() { if (!Player.Status.CanInteract) { return; } Body.AddForce(GetMovementInput() * (movementForce * MovementMultiplicator)); }
private void Move(Vector3 direction, bool doNormalize) { if (doNormalize == true) { NormalizeDirection(ref direction); } direction *= MaxImpulse; Body.AddForce(direction, ForceMode.VelocityChange); }
public static void ApplyForce(List <Body> bodies, float G) { for (int i = 0; i < bodies.Count; i++) { for (int j = i + 1; j < bodies.Count; j++) { Body bodyA = bodies[i]; Body bodyB = bodies[j]; Vector2 direction = bodyA.Position - bodyB.Position; float sqrDistance = Mathf.Max(direction.magnitude * direction.magnitude, 1); float force = G * (bodyA.Mass * bodyB.Mass) / sqrDistance; bodyA.AddForce(-direction.normalized, Body.eForceMode.Force); bodyA.AddForce(direction.normalized, Body.eForceMode.Force); } } }
public void Update() { //老版 物理碰撞 if (isColliderStay == true && isColliderMethodEnter == false) { isColliderStay = false; OnColliderExit?.Invoke(null); } //Log.Trace("物理碰撞 持续数量" + contactActors.Count); //新版物理碰撞 if (contactActors.Count > 0) { foreach (var i in contactActors) { //持续伤害 OnColliderStay?.Invoke(i); //Log.Trace("物理碰撞 持续" + i); } } //附上值 angleVelocity_copy = m_body.AngularVelocity; LinearVelocity_copy = m_body.LinearVelocity; Torque_copy = m_body.GetTorque(); Damping_copy = m_body.LinearDamping; isColliderMethodEnter = false; //Force_copy = m_body.GetForce(); //if(Force_copy.LengthSquared() > 1000000) //{ // Force_copy.Normalize(); // Force_copy = Force_copy * 31.62277660168379f; //} Force_copy_copy = Force_copy; m_body.AddForce(Force_copy); //Log.Trace("已经添加力量:" + m_body.GetForce()+" "+Force_copy); //Log.Trace("Update Mass" + m_body.Mass); Force_copy = Vector2.Zero; TickFlag(); }
void Jump(float _power)//Прыжок { Body.AddForce((transform.forward * 0.5f + Vector3.up) * _power, ForceMode.Impulse); Power_jump = 0; Interface_player.Instance.Progress_bar_jump(0); Anim.SetBool("Jump", true); Anim.SetFloat("Jump_power", 0); Sound_control_.Sound_play_1(); }
protected override void MovementPattern() { if (TweakOut) { float randomX = UnityEngine.Random.Range(-29f, 29f); float randomY = UnityEngine.Random.Range(-120f, 120f); Body.AddForce(new Vector2(randomX * Speed, randomY * Speed)); TweakOut = false; } }
/// <summary> /// This will whack the body by the explosion. /// NOTE: Only call from within the Body.ApplyForceAndTorque callback /// </summary> public void ApplyForceToBody(Body body) { //TODO: This needs to add some rotation. Otherwise it just looks odd (everything is too even) Vector3D explosiveForce = GetForceAtPoint(body.Position); if (explosiveForce.X != 0d || explosiveForce.Y != 0d || explosiveForce.Z != 0d) { body.AddForce(explosiveForce); } }
protected override void MovementPattern() { if (Body.velocity.y < 0) { Body.AddTorque((float)Speed / 2 * Direction); } else if (Body.velocity.y == 0) { Body.AddForce(new Vector2(0, 100)); } }
/// <summary> /// 追向目标 /// </summary> /// <param name="body"></param> /// <param name="targetPoint"></param> /// <returns></returns> public static bool FollowTarget(this Body body, Vector2 targetPoint, float force, float angularVelocityProc) { var cos = FowardToTarget(body, targetPoint, angularVelocityProc); if (cos > 0.9f) { body.AddForce(force * body.GetForward()); } return(cos > 0.9f); }
/// <summary>Overridden from <see cref="Engine.EntitySystem.Entity.OnTick()"/>.</summary> protected override void OnTick() { base.OnTick(); //!!!!!!this class not implemented //!!!!! this is a fake float mass = 0; foreach (Body b in PhysicsModel.Bodies) { mass += b.Mass; } Body body = PhysicsModel.Bodies[0]; body.AngularVelocity = Vec3.Zero; body.LinearVelocity = Rotation * new Vec3(Type.Velocity, 0, body.LinearVelocity.Z); //anti gravity body.AddForce(ForceType.GlobalAtLocalPos, TickDelta, -PhysicsWorld.Instance.Gravity * mass, Vec3.Zero); float diff = Position.Z - flyHeight; float force = -diff - body.LinearVelocity.Z; MathFunctions.Clamp(ref force, -10, 10); body.AddForce(ForceType.GlobalAtLocalPos, TickDelta, new Vec3(0, 0, force) * mass, Vec3.Zero); //check outside Map position Bounds checkBounds = Map.Instance.InitialCollisionBounds; checkBounds.Expand(new Vec3(300, 300, 10000)); if (!checkBounds.IsContainsPoint(Position)) { SetDeleted(); } }
public void FixedUpdate() { if (Jump) { Body.bodyType = RigidbodyType2D.Dynamic; Body.AddForce(new Vector2(0f, Vector2.up.y * ySpeed)); } else if (!Jump && IsGrounded()) { Body.bodyType = RigidbodyType2D.Kinematic; } }
public void Update() { if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow)) { Body.AddForce(LeftDirection * xSpeed * Multiplyer * Time.deltaTime); } else if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow)) { Body.AddForce(RightDirection * xSpeed * Multiplyer * Time.deltaTime); } Jump = Input.GetKey(KeyCode.Space) ? true : false; }
protected void startJumping() { if (!_isJumping && isGrounded()) { _isJumping = true; Velocity = new Vector3(Velocity.x, jumpStrength, 0); } if (_isJumping) { Body.AddForce(Vector2.up * jumpStrength * Time.deltaTime * 50); } }
public void ApplyForce(Body body, float TickDelta) { Vec3 originalBoneDerivedPosition = bone.Parent.Position + bone.Parent.Rotation * originalBonePosition; Quat originalBoneDerivedRotation = bone.Parent.Rotation * originalBoneRotation; Vec3 pos = body.Position + body.Rotation * originalBoneDerivedPosition; Vec3 dir = body.Rotation * originalBoneDerivedRotation * new Vec3(size, 0, 0); Ray ray = new Ray(pos, dir); RayCastResult res = PhysicsWorld.Instance.RayCast(ray, (int)ContactGroup.CastOnlyContact); if (res.Distance != 0) { // Stiffness float forceStiffness = 0, forceDamping = 0; forceStiffness = (size - res.Distance) * stiffness; // Damping if (lastPos != -1.0f) { float speed = res.Distance - lastPos; forceDamping = -speed * damping; } lastPos = res.Distance; //EngineConsole.Instance.Print("force(" + bone.Name + ")=" + forceStiffness.ToString() + ", " + forceDamping.ToString()); body.AddForce(ForceType.LocalAtLocalPos, TickDelta, new Vec3(0, 0, forceStiffness + forceDamping), originalBoneDerivedPosition); // Lateral Friction Vec3 speedAtWheel = body.LinearVelocity + Vec3.Cross(body.AngularVelocity, body.Rotation * bone.GetDerivedPosition()); float lateralForce = -Vec3.Dot(speedAtWheel, body.Rotation * Vec3.XAxis) * 0.3f; body.AddForce(ForceType.LocalAtLocalPos, TickDelta, new Vec3(lateralForce, 0, 0), bone.GetDerivedPosition()); // Longitudinal Friction float longitudinalForce = -Vec3.Dot(speedAtWheel, body.Rotation * Vec3.YAxis) * 0.001f; body.AddForce(ForceType.LocalAtLocalPos, TickDelta, new Vec3(0, longitudinalForce, 0), bone.GetDerivedPosition()); bone.Position = originalBonePosition - new Vec3(0, 0, res.Distance); float wheelRadius = 0.0439f; mesh.PositionOffset = originalBoneDerivedPosition - new Vec3(0, 0, res.Distance - wheelRadius); } }
protected override void MovementPattern() { Body.AddForce(new Vector2(horizontalMotion * Speed, Body.velocity.y), ForceMode2D.Force); if (Body.velocity.x < Speed / 2 * -1) { Body.velocity = new Vector2(Speed / 2 * -1, Body.velocity.y); } if (Body.velocity.x > Speed / 2) { Body.velocity = new Vector2(Speed / 2, Body.velocity.y); } }
public void ForceCallback(Body body, float timestep, int threadindex) { var data = body.UserData as PhysicsControlData; if (data != null) { body.AddForce(data.Force * body.Mass); data.Force = Vector3.ZERO; body.AddTorque(data.Torque * body.Mass); data.Torque = Vector3.ZERO; } if (body.Position.y < -10) body.AddBouyancyForce(1030, 0.0020F, 0.0020F, Vector3.NEGATIVE_UNIT_Y * 9.8f, Plane ); }
void HandleWallJump() { if (playerController.PlayerSkillController.SkillDict[SkillType.WallJump].activeness) { if (IsFacingRight && HorizontalAxis < 0f) { Body.AddForce(Vector2.one * wallJumpForce, ForceMode2D.Impulse); } else if (!IsFacingRight && HorizontalAxis > 0f) { Body.AddForce(Vector2.one * wallJumpForce, ForceMode2D.Impulse); } } }
private void GetInputs() { if (Input.GetKey(KeyCode.D)) { XForce += Acceleration * Body.mass * Time.deltaTime; } if (Input.GetKey(KeyCode.A)) { XForce -= Acceleration * Body.mass * Time.deltaTime; } if (Input.GetKeyDown(KeyCode.W) && IsGrounded()) { Body.AddForce(new Vector2(0, JumpAcceleration * Body.mass), ForceMode2D.Impulse); } }
public void HandleJump() { if (IsGrounded) { playerController.PlayerAnimationController.IsJumping = true; Body.AddForce(jumpVector); playerController.PlayerAttackController.ResetToDefaults(); AudioManager.Instance.Play(MyAudioType.Player_Jump); } if (IsWallSliding) { AudioManager.Instance.Play(MyAudioType.Player_Jump); HandleWallJump(); } }
private void FixedUpdate() { // Set kinematic state. bool kinematic = Override; if (kinematic != Body.isKinematic) { Body.isKinematic = kinematic; } // Go no further if kinematic (animation / cutscene control) if (kinematic) { FixedAnimationUpdate(); return; } // Add custom gravity. Body.AddForce(Physics.gravity * GravityScale * Body.mass); // Add flat input. Vector3 localFlat = new Vector3(flatInput.x, 0f, flatInput.y); Vector3 worldFlat = transform.TransformVector(localFlat); // Add flat (WASD) movement force. Body.AddForce(worldFlat * (IsRunning ? AccelerateForceRun : AccelerateForce)); // Counteract movement to reduce sliding. Vector3 dragVel = -Body.velocity; dragVel.y = 0f; Body.AddForce(dragVel * DecelerateCoefficient); // Jump! if (jump) { jump = false; Body.AddForce(-Physics.gravity.normalized * JumpVel, ForceMode.VelocityChange); if (OnJump != null) { OnJump.Invoke(); } } IsGrounded = false; }
public void TickAndApplyForces(Body body, float TickDelta) { //mainBody.AddForce(ForceType.LocalAtLocalPos, TickDelta, new Vec3(0, 0, intellect.controlThrust.Value), new Vec3(0, 0, 0)); //PhysicsWorld.Instance.CreateFixedJoint(body,) /*body.LinearVelocity = new Vec3(0, 10, -4); body.AngularVelocity = Vec3.Zero; body.Rotation = Quat.Identity;*/ Vec3 speed = body.LinearVelocity; Vec3 omega = body.AngularVelocity; // Apply Thrust //EngineConsole.Instance.Print("speed = " + speed.ToString()); EngineConsole.Instance.Print("thrustControl = " + awesomeAircraft.controlThrust.Value.ToString()); float airspeed_scale = 1.0f - speed.Length() / handling.speedMax; float thrustPower = awesomeAircraft.controlThrust.Value * handling.accelMax * airspeed_scale; body.AddForce(ForceType.LocalAtLocalPos, TickDelta, thrustPower * Vec3.YAxis, new Vec3(0, 5, 0)); if (Utils.LogOn) totalTime += 0.02f; //Utils.Write(totalTime.ToString() + "," + thrustPower.ToString() + ",,"); //Utils.Write(Utils.VectToString(body.Position-Utils.LogPosInitial) + ",," + body.Rotation.ToString() + ",,"); //Utils.Write(Utils.VectToString(speed) + ",," + Utils.VectToString(omega) + ",,"); int i = 0; foreach (Aerofoil aerofoil in aerofoils) { Vec3 speedAtAerofoil = speed + Vec3.Cross(omega, body.Rotation * aerofoil.position); float density = 5; Vec3 liftL, dragL, forceappL, pitch_forceL, pitch_forceappL; Mat3 M3Inv = body.Rotation.ToMat3(); M3Inv.Inverse(); aerofoil.get_lift_and_drag( this, -M3Inv * speedAtAerofoil, out liftL, out dragL, out forceappL, out pitch_forceL, out pitch_forceappL, density, i, awesomeAircraft.axisAileron.Value, awesomeAircraft.axisElevator.Value, awesomeAircraft.axisRudder.Value ); float seuil = 100; if (liftL.Length() > param.CapForce) liftL *= param.CapForce / liftL.Length(); if (dragL.Length() > param.CapForce) dragL *= param.CapForce / dragL.Length(); if (pitch_forceL.Length() > param.CapForce) pitch_forceL *= param.CapForce / pitch_forceL.Length(); body.AddForce(ForceType.LocalAtLocalPos, TickDelta, liftL, forceappL); body.AddForce(ForceType.LocalAtLocalPos, TickDelta, dragL, forceappL); body.AddForce(ForceType.LocalAtLocalPos, TickDelta, pitch_forceL, pitch_forceappL); /* Utils.Write( Utils.VectToString(forceappL) + ",," + Utils.VectToString(liftL) + ",," + Utils.VectToString(dragL) + ",," + Utils.VectToString(pitch_forceappL) + ",," + Utils.VectToString(pitch_forceL) + ",," );*/ Utils.Write(liftL.Length().ToString() + ',' + dragL.Length().ToString() + ',' + pitch_forceL.Length().ToString() + ",,"); i++; } Utils.WriteLine(""); }
private void FixPositionsAndVelocities_ORIG(Body body) { const double RETURNVELOCITY = 0d; double ZACCEL = 10; // Get the center of mass in world coords Point3D position = body.Position; if (_boundryMin != null) // if min is non null, _boundryMax is also non null. I don't want to waste the processor checking that each frame { #region Stay inside bounding box // Set the velocity going away to zero, apply a force //NOTE: Position is the center of mass, so if the position is past the boundry, that means the body was travelling too fast for newton to bounce //it back (there is a wall at the boundry position). So this logic is an extra helper Vector3D velocity = body.Velocity; // already in world coords bool modifiedVelocity = false; #region X if (position.X < _boundryMin.Value.X) { if (velocity.X < 0) { velocity.X = RETURNVELOCITY; modifiedVelocity = true; } body.AddForce(new Vector3D(body.Mass * ZACCEL, 0, 0)); // Apply a constant acceleration until it hits zero } else if (position.X > _boundryMax.Value.X) { if (velocity.X > 0) { velocity.X = -RETURNVELOCITY; modifiedVelocity = true; } body.AddForce(new Vector3D(body.Mass * ZACCEL * -1, 0, 0)); // Apply a constant acceleration until it hits zero } #endregion #region Y if (position.Y < _boundryMin.Value.Y) { if (velocity.Y < 0) { velocity.Y = RETURNVELOCITY; modifiedVelocity = true; } body.AddForce(new Vector3D(0, body.Mass * ZACCEL, 0)); // Apply a constant acceleration until it hits zero } else if (position.Y > _boundryMax.Value.Y) { if (velocity.Y > 0) { velocity.Y = -RETURNVELOCITY; modifiedVelocity = true; } body.AddForce(new Vector3D(0, body.Mass * ZACCEL * -1, 0)); // Apply a constant acceleration until it hits zero } #endregion #region Z if (position.Z < _boundryMin.Value.Z) { if (velocity.Z < 0) { velocity.Z = RETURNVELOCITY; modifiedVelocity = true; } body.AddForce(new Vector3D(0, 0, body.Mass * ZACCEL)); // Apply a constant acceleration until it hits zero } else if (position.Z > _boundryMax.Value.Z) { if (velocity.Z > 0) { velocity.Z = -RETURNVELOCITY; modifiedVelocity = true; } body.AddForce(new Vector3D(0, 0, body.Mass * ZACCEL * -1)); // Apply a constant acceleration until it hits zero } #endregion if (modifiedVelocity) { body.Velocity = velocity; // already in world coords } #endregion } #region Force2D //if (_shouldForce2D) //{ // if (!body.Override2DEnforcement_Rotation) // { // #region Angular Pos/Vel // //body.NewtonBody.AddTorque(new Vector3D(.01, 0, 0)); // pulls back (front comes up, rear goes down) // //body.NewtonBody.AddTorque(new Vector3D(0, .1, 0)); // creates a roll to the right (left side goes up, right side goes down) // Vector3D angularVelocity = body.Omega; // Omega seems to be angular velocity // bool modifiedAngularVelocity = false; // //const double RESTORETORQUE = 15d; // TODO: look at the inertia tensor for the axis I want to apply torque to, and do and calculate to make a constant accel // const double RESTORETORQUE = .25d; // TODO: look at the inertia tensor for the axis I want to apply torque to, and do and calculate to make a constant accel // double massMatrixLength = body.MassMatrix.m_I.Length; // double restoreTorqueX = RESTORETORQUE * body.MassMatrix.m_Mass * (1 / (body.MassMatrix.m_I.X / massMatrixLength)); // double restoreTorqueY = RESTORETORQUE * body.MassMatrix.m_Mass * (1 / (body.MassMatrix.m_I.Y / massMatrixLength)); // //double restoreTorqueX = RESTORETORQUE; // pulling values from the mass matrix seemed to cause the engine to corrupt. See if there's a problem casting that structure // //double restoreTorqueY = RESTORETORQUE; // //TODO: Dampen the angluar velocidy if the object is very close to zero and the angular speed is small. Currently, the object has // // a very slight wobble indefinately // #region X // Vector3D fromVect = new Vector3D(1, 0, 0); // Vector3D toVect = body.DirectionToWorld(fromVect); // Vector3D axis; // double radians; // Math3D.GetRotation(out axis, out radians, fromVect, toVect); // if ((axis.Y > 0 && radians > 0) || (axis.Y < 0 && radians < 0)) // { // if (angularVelocity.Y > 0) // { // angularVelocity.Y = 0; // modifiedAngularVelocity = true; // } // //body.NewtonBody.AddTorque(new Vector3D(0, -RESTORETORQUE, 0)); // //body.NewtonBody.AddTorque(body.DirectionToWorld(new Vector3D(0, -RESTORETORQUE, 0))); // apply torque seems to want world coords // body.NewtonBody.AddTorque(body.DirectionToWorld(new Vector3D(0, -restoreTorqueY * Math.Abs(radians), 0))); // } // else if ((axis.Y > 0 && radians < 0) || (axis.Y < 0 && radians > 0)) // { // if (angularVelocity.Y < 0) // { // angularVelocity.Y = 0; // modifiedAngularVelocity = true; // } // //body.NewtonBody.AddTorque(new Vector3D(0, RESTORETORQUE, 0)); // body.NewtonBody.AddTorque(body.DirectionToWorld(new Vector3D(0, restoreTorqueY * Math.Abs(radians), 0))); // } // #endregion // #region Y // fromVect = new Vector3D(0, 1, 0); // toVect = body.DirectionToWorld(fromVect); // Math3D.GetRotation(out axis, out radians, fromVect, toVect); // if ((axis.X > 0 && radians > 0) || (axis.X < 0 && radians < 0)) // { // if (angularVelocity.X > 0) // { // angularVelocity.X = 0; // modifiedAngularVelocity = true; // } // //body.NewtonBody.AddTorque(new Vector3D(-RESTORETORQUE, 0, 0)); // body.NewtonBody.AddTorque(body.DirectionToWorld(new Vector3D(-restoreTorqueX * Math.Abs(radians), 0, 0))); // } // else if ((axis.X > 0 && radians < 0) || (axis.X < 0 && radians > 0)) // { // if (angularVelocity.X < 0) // { // angularVelocity.X = 0; // modifiedAngularVelocity = true; // } // //body.NewtonBody.AddTorque(new Vector3D(RESTORETORQUE, 0, 0)); // body.NewtonBody.AddTorque(body.DirectionToWorld(new Vector3D(restoreTorqueX * Math.Abs(radians), 0, 0))); // } // #endregion // if (modifiedAngularVelocity) // { // body.Omega = angularVelocity; // } // #endregion // } // if (!body.Override2DEnforcement_Translation) // { // #region Z Pos/Vel // //Vector3D velocityWorld = body.DirectionToWorld(body.Velocity); // Vector3D velocityWorld = body.Velocity; // already in world coords // bool modifiedVelocity = false; // if (position.Z < 0) // { // if (velocityWorld.Z < 0) // { // velocityWorld.Z = 0; // modifiedVelocity = true; // } // body.NewtonBody.AddForce(new Vector3D(0, 0, body.Mass * ZACCEL)); // Apply a constant acceleration until it hits zero // } // else if (position.Z > 0) // { // if (velocityWorld.Z > 0) // { // velocityWorld.Z = 0; // modifiedVelocity = true; // } // body.NewtonBody.AddForce(new Vector3D(0, 0, body.Mass * ZACCEL * -1d)); // Apply a constant acceleration until it hits zero // } // if (modifiedVelocity) // { // //body.Velocity = body.DirectionFromWorld(velocityWorld); // body.Velocity = velocityWorld; // } // #endregion // } //} #endregion }
protected override void OnTick() { base.OnTick(); if (firstWay == true) { ActivateWayMovement(); } UpdateGeneralTask(); CalculateThings(); chassisBody = PhysicsModel.GetBody("main"); if (chassisBody == null) { Log.Error("Emrah Helli: \"main\" chassisBody dose not exists."); return; } float diff = Position.Z - flyHeight; float force = -diff - chassisBody.LinearVelocity.Z; MathFunctions.Clamp(ref force, -10, 10); chassisBody.AddForce(ForceType.GlobalAtLocalPos, TickDelta, new Vec3(0, 0, force * 3) * chassisBody.Mass, new Vec3(0, 0, 1)); chassisBody.AngularDamping = 1; this.flyHeight = TaskPosition.Z; float ZA = ZADiffAngle * 4; if (ZA < 20 && ZA > -20) { chassisBody.AngularVelocity = new Vec3 (chassisBody.AngularVelocity.X, chassisBody.AngularVelocity.Y, ZA); } else if (ZA > 20) { chassisBody.AngularVelocity = new Vec3 (chassisBody.AngularVelocity.X, chassisBody.AngularVelocity.Y, -2); } else if (ZA < -20) { chassisBody.AngularVelocity = new Vec3 (chassisBody.AngularVelocity.X, chassisBody.AngularVelocity.Y, 2); } chassisBody.AddForce(ForceType.LocalAtLocalPos, TickDelta, new Vec3(Velocity * chassisBody.Mass, 0, 0), new Vec3(-1, 0, 0)); chassisBody.LinearDamping = 1; //check outside Map position Bounds checkBounds = Map.Instance.InitialCollisionBounds; checkBounds.Expand(new Vec3(300, 300, 10000)); if (!checkBounds.IsContainsPoint(Position)) SetDeleted(); }