public override void ExclusiveUpdate() { if (Helicopter.HeloPilot == null) { return; // Don't fly when there's nobody driving this! } // Collect the helicopter's relative "up" vector BEPUutilities.Vector3 up = BEPUutilities.Quaternion.Transform(BEPUutilities.Vector3.UnitZ, Entity.Orientation); // Apply the amount of force necessary to counteract downward force, within a limit. // POTENTIAL: Adjust according to orientation? double uspeed = Math.Min(Helicopter.LiftStrength, -(Entity.LinearVelocity.Z + Entity.Space.ForceUpdater.Gravity.Z) * Entity.Mass); if (uspeed < 0f) { uspeed += (uspeed - Helicopter.FallStrength) * Helicopter.HeloPilot.SprintOrWalk; } else { uspeed += (Helicopter.LiftStrength - uspeed) * Helicopter.HeloPilot.SprintOrWalk; } BEPUutilities.Vector3 upvel = up * uspeed * Delta; Entity.ApplyLinearImpulse(ref upvel); // Rotate slightly to move in a direction. // At the same time, fight against existing rotation. BEPUutilities.Vector3 VecUp = new BEPUutilities.Vector3(Helicopter.HeloPilot.XMove * 0.2f * Helicopter.HeloTiltMod, Helicopter.HeloPilot.YMove * -0.2f * Helicopter.HeloTiltMod, 1); // TODO: Simplify yawrel calculation. float tyaw = (float)(Utilities.MatrixToAngles(Matrix.CreateFromQuaternion(Entity.Orientation)).Z * Utilities.PI180); BEPUutilities.Quaternion yawrel = BEPUutilities.Quaternion.CreateFromAxisAngle(BEPUutilities.Vector3.UnitZ, tyaw); VecUp = BEPUutilities.Quaternion.Transform(VecUp, yawrel); VecUp.Normalize(); VecUp.Y = -VecUp.Y; BEPUutilities.Vector3 axis = BEPUutilities.Vector3.Cross(VecUp, up); double len = axis.Length(); if (len > 0) { axis /= len; float angle = (float)Math.Asin(len); if (!float.IsNaN(angle)) { double avel = BEPUutilities.Vector3.Dot(Entity.AngularVelocity, axis); BEPUutilities.Vector3 torque = axis * ((-angle) - 0.3f * avel); torque *= Entity.Mass * Delta * 30; Entity.ApplyAngularImpulse(ref torque); } } // Spin in place float rotation = (Helicopter.HeloPilot.ItemRight ? -1f : 0f) + (Helicopter.HeloPilot.ItemLeft ? 1f : 0f); if (rotation * rotation > 0f) { BEPUutilities.Vector3 rot = new BEPUutilities.Vector3(0, 0, rotation * 15f * Delta * Entity.Mass); Entity.ApplyAngularImpulse(ref rot); } // Apply air drag Entity.ModifyLinearDamping(0.3f); // TODO: arbitrary constant Entity.ModifyAngularDamping(0.6f); // TODO: arbitrary constant // Ensure we're active if flying! Entity.ActivityInformation.Activate(); }
public override void ExclusiveUpdate() { if (Plane.PlanePilot == null) { return; // Don't fly when there's nobody driving this! } // TODO: Special case for motion on land: only push forward if W key is pressed? Or maybe apply that rule in general? // Collect the plane's relative vectors BEPUutilities.Vector3 forward = BEPUutilities.Quaternion.Transform(BEPUutilities.Vector3.UnitY, Entity.Orientation); BEPUutilities.Vector3 side = BEPUutilities.Quaternion.Transform(BEPUutilities.Vector3.UnitX, Entity.Orientation); BEPUutilities.Vector3 up = BEPUutilities.Quaternion.Transform(BEPUutilities.Vector3.UnitZ, Entity.Orientation); // Engines! if (Plane.PlanePilot.SprintOrWalk >= 0.0) { BEPUutilities.Vector3 force = forward * (Plane.PlaneRegularStrength + Plane.PlaneFastStrength) * Delta; entity.ApplyLinearImpulse(ref force); } double dotforw = BEPUutilities.Vector3.Dot(entity.LinearVelocity, forward); entity.ApplyImpulse(side * 5 + entity.Position, up * -Plane.PlanePilot.XMove * entity.Mass * dotforw * 0.5 * Delta); entity.ApplyImpulse(forward * 5 + entity.Position, side * ((Plane.PlanePilot.ItemRight ? 1 : 0) + (Plane.PlanePilot.ItemLeft ? -1 : 0)) * entity.Mass * dotforw * 0.5 * Delta); entity.ApplyImpulse(forward * 5 + entity.Position, up * Plane.PlanePilot.YMove * entity.Mass * 0.5 * Delta * dotforw); // Rotate the entity pre-emptively, and re-apply the movement velocity in this new direction! double vellen = entity.LinearVelocity.Length(); BEPUutilities.Vector3 normvel = entity.LinearVelocity / vellen; BEPUutilities.Vector3 norm_vel_transf = BEPUutilities.Quaternion.Transform(normvel, BEPUutilities.Quaternion.Inverse(entity.Orientation)); // Probably just 1,0,0 on whichever axis... can be simplified! BEPUutilities.Vector3 inc = entity.AngularVelocity * Delta * 0.5; BEPUutilities.Quaternion quat = new BEPUutilities.Quaternion(inc.X, inc.Y, inc.Z, 0); quat = quat * entity.Orientation; BEPUutilities.Quaternion orient = entity.Orientation; BEPUutilities.Quaternion.Add(ref orient, ref quat, out orient); orient.Normalize(); entity.Orientation = orient; entity.LinearVelocity = BEPUutilities.Quaternion.Transform(norm_vel_transf, orient) * vellen; entity.AngularVelocity *= 0.1; // Apply air drag Entity.ModifyLinearDamping(Plane.PlanePilot.SprintOrWalk < 0.0 ? 0.6 : 0.1); // TODO: arbitrary constant Entity.ModifyAngularDamping(0.5); // TODO: arbitrary constant // Ensure we're active if flying! Entity.ActivityInformation.Activate(); }
public override void ExclusiveUpdate() { if (Plane.PlanePilot == null) { return; // Don't fly when there's nobody driving this! } // TODO: Special case for motion on land: only push forward if FORWARD key is pressed? Or maybe apply that rule in general? // Collect the plane's relative vectors BEPUutilities.Vector3 forward = BEPUutilities.Quaternion.Transform(BEPUutilities.Vector3.UnitY, Entity.Orientation); BEPUutilities.Vector3 side = BEPUutilities.Quaternion.Transform(BEPUutilities.Vector3.UnitX, Entity.Orientation); BEPUutilities.Vector3 up = BEPUutilities.Quaternion.Transform(BEPUutilities.Vector3.UnitZ, Entity.Orientation); // Engines! if (Plane.PlanePilot.SprintOrWalk >= 0.0) { BEPUutilities.Vector3 force = forward * (Plane.PlaneRegularStrength + Plane.PlaneFastStrength * Plane.PlanePilot.SprintOrWalk) * Delta; entity.ApplyLinearImpulse(ref force); } double dotforw = BEPUutilities.Vector3.Dot(entity.LinearVelocity, forward); double mval = 2.0 * (1.0 / Math.Max(1.0, entity.LinearVelocity.Length())); double rot_x = -Plane.PlanePilot.YMove * 0.5 * Delta * dotforw * mval; double rot_y = Plane.PlanePilot.XMove * dotforw * 0.5 * Delta * mval; double rot_z = -((Plane.PlanePilot.ItemRight ? 1 : 0) + (Plane.PlanePilot.ItemLeft ? -1 : 0)) * dotforw * 0.1 * Delta * mval; entity.AngularVelocity += BEPUutilities.Quaternion.Transform(new BEPUutilities.Vector3(rot_x, rot_y, rot_z), entity.Orientation); double vellen = entity.LinearVelocity.Length(); BEPUutilities.Vector3 newVel = forward * vellen; double forwVel = BEPUutilities.Vector3.Dot(entity.LinearVelocity, forward); double root = Math.Sqrt(Math.Sign(forwVel) * forwVel); entity.LinearVelocity += (newVel - entity.LinearVelocity) * BEPUutilities.MathHelper.Clamp(Delta, 0.01, 0.3) * Math.Min(2.0, root * 0.05); // Apply air drag Entity.ModifyLinearDamping(Plane.PlanePilot.SprintOrWalk < 0.0 ? 0.5 : 0.1); // TODO: arbitrary constants Entity.ModifyAngularDamping(0.995); // TODO: arbitrary constant // Ensure we're active if flying! Entity.ActivityInformation.Activate(); }