From fae3b852132eb4e5537528e1376237c0650c24c0 Mon Sep 17 00:00:00 2001 From: Reinier Vens Date: Wed, 3 Mar 2021 01:53:23 +0000 Subject: [PATCH] Torque Compensation for Strafe Input Adjusted thruster firing to compensate for torque introduced by uniform application of strafe input to lateral thrusters. --- .../Prefabs/Weapons/Micromissile.prefab | 85 +++++++++++++++++- .../ServerShared/Behaviors/Thruster.cs | 1 + Assets/Scripts/ServerShared/Entity.cs | 27 +++--- Assets/Scripts/ServerShared/Ship.cs | 86 ++++++++++++++----- 4 files changed, 165 insertions(+), 34 deletions(-) diff --git a/Assets/Resources/Prefabs/Weapons/Micromissile.prefab b/Assets/Resources/Prefabs/Weapons/Micromissile.prefab index aaa28ae99..db858d9a9 100644 --- a/Assets/Resources/Prefabs/Weapons/Micromissile.prefab +++ b/Assets/Resources/Prefabs/Weapons/Micromissile.prefab @@ -57,10 +57,18 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 56c973b8f3781c848b6530c2e7efce05, type: 3} m_Name: m_EditorClassIdentifier: + HitEffect: {fileID: 332779026898239585} ChildProjectile: {fileID: 0} + AirburstProjectile: {fileID: 0} + AirburstCount: 0 + AirburstDirectionality: 0 + AirburstDistance: 0 + AirburstRange: 0 + AirburstVelocity: 0 Children: 0 SplitTime: 0 SplitSeparationForwardness: 0 + SplitSeparationVelocity: 0 Particles: {fileID: 2200213684891960502} FadeOutTime: 1 ThrustCurve: @@ -175,7 +183,6 @@ MonoBehaviour: Thrust: 500 TopSpeed: 250 Source: {fileID: 0} - Target: {fileID: 0} --- !u!1 &2200213684891960497 GameObject: m_ObjectHideFlags: 0 @@ -5058,6 +5065,7 @@ Transform: m_LocalScale: {x: 1, y: 1, z: 1} m_Children: - {fileID: 2200213683735813172} + - {fileID: 7703202934816564928} m_Father: {fileID: 0} m_RootOrder: 0 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} @@ -5074,3 +5082,78 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: ProjectilePrototype: {fileID: 2698694499776308069} +--- !u!1001 &7703470150373816180 +PrefabInstance: + m_ObjectHideFlags: 0 + serializedVersion: 2 + m_Modification: + m_TransformParent: {fileID: 5148235482537864796} + m_Modifications: + - target: {fileID: 1828176872806910, guid: 64891a0eb28db934898420d80ca07cb3, type: 3} + propertyPath: m_Name + value: SmallExplosion + objectReference: {fileID: 0} + - target: {fileID: 4361801158873524, guid: 64891a0eb28db934898420d80ca07cb3, type: 3} + propertyPath: m_RootOrder + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 4361801158873524, guid: 64891a0eb28db934898420d80ca07cb3, type: 3} + propertyPath: m_LocalPosition.x + value: -0.50997305 + objectReference: {fileID: 0} + - target: {fileID: 4361801158873524, guid: 64891a0eb28db934898420d80ca07cb3, type: 3} + propertyPath: m_LocalPosition.y + value: 2.57 + objectReference: {fileID: 0} + - target: {fileID: 4361801158873524, guid: 64891a0eb28db934898420d80ca07cb3, type: 3} + propertyPath: m_LocalPosition.z + value: -20.12803 + objectReference: {fileID: 0} + - target: {fileID: 4361801158873524, guid: 64891a0eb28db934898420d80ca07cb3, type: 3} + propertyPath: m_LocalRotation.w + value: 0.7071068 + objectReference: {fileID: 0} + - target: {fileID: 4361801158873524, guid: 64891a0eb28db934898420d80ca07cb3, type: 3} + propertyPath: m_LocalRotation.x + value: -0.7071068 + objectReference: {fileID: 0} + - target: {fileID: 4361801158873524, guid: 64891a0eb28db934898420d80ca07cb3, type: 3} + propertyPath: m_LocalRotation.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 4361801158873524, guid: 64891a0eb28db934898420d80ca07cb3, type: 3} + propertyPath: m_LocalRotation.z + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 4361801158873524, guid: 64891a0eb28db934898420d80ca07cb3, type: 3} + propertyPath: m_LocalEulerAnglesHint.x + value: -90 + objectReference: {fileID: 0} + - target: {fileID: 4361801158873524, guid: 64891a0eb28db934898420d80ca07cb3, type: 3} + propertyPath: m_LocalEulerAnglesHint.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 4361801158873524, guid: 64891a0eb28db934898420d80ca07cb3, type: 3} + propertyPath: m_LocalEulerAnglesHint.z + value: 0 + objectReference: {fileID: 0} + m_RemovedComponents: [] + m_SourcePrefab: {fileID: 100100000, guid: 64891a0eb28db934898420d80ca07cb3, type: 3} +--- !u!4 &7703202934816564928 stripped +Transform: + m_CorrespondingSourceObject: {fileID: 4361801158873524, guid: 64891a0eb28db934898420d80ca07cb3, + type: 3} + m_PrefabInstance: {fileID: 7703470150373816180} + m_PrefabAsset: {fileID: 0} +--- !u!114 &332779026898239585 stripped +MonoBehaviour: + m_CorrespondingSourceObject: {fileID: 7959687425165588245, guid: 64891a0eb28db934898420d80ca07cb3, + type: 3} + m_PrefabInstance: {fileID: 7703470150373816180} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 1281fe44325f9b548aa42dc331b17266, type: 3} + m_Name: + m_EditorClassIdentifier: diff --git a/Assets/Scripts/ServerShared/Behaviors/Thruster.cs b/Assets/Scripts/ServerShared/Behaviors/Thruster.cs index 2f3e0e93d..361ffabcb 100644 --- a/Assets/Scripts/ServerShared/Behaviors/Thruster.cs +++ b/Assets/Scripts/ServerShared/Behaviors/Thruster.cs @@ -61,6 +61,7 @@ public class Thruster : IAnalogBehavior var itemCenter = hullData.Shape.Inset(itemData.Shape, item.Position, item.EquippableItem.Rotation).CenterOfMass; var toCenter = hullCenter - itemCenter; Torque = -dot(normalize(toCenter), float2(1, 0).Rotate(item.EquippableItem.Rotation)); + Thrust = Context.Evaluate(_data.Thrust, Item.EquippableItem, Entity); } public bool Execute(float delta) diff --git a/Assets/Scripts/ServerShared/Entity.cs b/Assets/Scripts/ServerShared/Entity.cs index c3babebc3..a3c6e6d96 100644 --- a/Assets/Scripts/ServerShared/Entity.cs +++ b/Assets/Scripts/ServerShared/Entity.cs @@ -90,6 +90,8 @@ public abstract class Entity public Subject<(int2 pos, float damage)> ArmorDamage = new Subject<(int2, float)>(); public Subject<(EquippedItem item, float damage)> ItemDamage = new Subject<(EquippedItem, float)>(); + public Subject ItemOffline = new Subject(); + public Subject ItemOnline = new Subject(); public Subject HullDamage = new Subject(); public Subject Docked = new Subject(); @@ -474,25 +476,26 @@ public abstract class Entity { equippedItem = new EquippedItem(ItemManager, item, hullCoord, this); Equipment.Add(equippedItem); - foreach (var b in equippedItem.Behaviors) - { - if(b is Capacitor capacitor) - _capacitors.Add(capacitor); - } } } else { equippedItem = new EquippedItem(ItemManager, item, hullCoord, this); Equipment.Add(equippedItem); - foreach (var b in equippedItem.Behaviors) - { - if (b is Weapon weapon) - _weapons.Add(weapon); - if(b is Reactor reactor) - _reactors.Add(reactor); - } } + + foreach (var b in equippedItem.Behaviors) + { + if (b is Weapon weapon) + _weapons.Add(weapon); + if(b is Capacitor capacitor) + _capacitors.Add(capacitor); + if(b is Reactor reactor) + _reactors.Add(reactor); + } + + equippedItem.OnEnable += () => ItemOnline.OnNext(equippedItem); + equippedItem.OnDisable += () => ItemOffline.OnNext(equippedItem); foreach (var i in itemData.Shape.Coordinates) { diff --git a/Assets/Scripts/ServerShared/Ship.cs b/Assets/Scripts/ServerShared/Ship.cs index 77c34f3f8..9369a5a74 100644 --- a/Assets/Scripts/ServerShared/Ship.cs +++ b/Assets/Scripts/ServerShared/Ship.cs @@ -25,7 +25,8 @@ public class Ship : Entity // [IgnoreMember] public Targetable Target; public Entity HomeEntity; public float2 MovementDirection; - + + private HashSet _thrusterItems; private Thruster[] _allThrusters; private HashSet _forwardThrusters; private HashSet _reverseThrusters; @@ -40,6 +41,10 @@ public class Ship : Entity public float RightStrafeThrust { get; private set; } public float ClockwiseTorque { get; private set; } public float CounterClockwiseTorque { get; private set; } + public float LeftStrafeTotalTorque { get; private set; } + private Thruster[] LeftStrafeTorqueThrusters; + public float RightStrafeTotalTorque { get; private set; } + private Thruster[] RightStrafeTorqueThrusters; public float TurnTime(float2 direction) { @@ -55,6 +60,7 @@ public class Ship : Entity base.Activate(); _allThrusters = GetBehaviors().ToArray(); + _thrusterItems = new HashSet(_allThrusters.Select(x=>x.Item)); _forwardThrusters = new HashSet(_allThrusters .Where(x => x.Item.EquippableItem.Rotation == ItemRotation.Reversed)); @@ -83,26 +89,28 @@ public class Ship : Entity public Ship(ItemManager itemManager, Zone zone, EquippableItem hull) : base(itemManager, zone, hull) { - ItemDamage.Subscribe(x => + void CheckForThruster(EquippedItem item) { - if (Hardpoints[x.item.Position.x, x.item.Position.y]?.Type == HardpointType.Thruster) + if (_thrusterItems.Contains(item)) { - foreach (var behavior in x.item.Behaviors) + foreach (var behavior in item.Behaviors) { if (behavior is Thruster thruster) { - if(_forwardThrusters.Contains(thruster)) RecalculateForwardThrust(); - if(_reverseThrusters.Contains(thruster)) RecalculateReverseThrust(); - if(_rightThrusters.Contains(thruster)) RecalculateRightStrafeThrust(); - if(_leftThrusters.Contains(thruster)) RecalculateLeftStrafeThrust(); - if(_clockwiseThrusters.Contains(thruster)) RecalculateClockwiseTorque(); - if(_counterClockwiseThrusters.Contains(thruster)) RecalculateCounterClockwiseTorque(); + if (_forwardThrusters.Contains(thruster)) RecalculateForwardThrust(); + if (_reverseThrusters.Contains(thruster)) RecalculateReverseThrust(); + if (_rightThrusters.Contains(thruster)) RecalculateRightStrafeThrust(); + if (_leftThrusters.Contains(thruster)) RecalculateLeftStrafeThrust(); + if (_clockwiseThrusters.Contains(thruster)) RecalculateClockwiseTorque(); + if (_counterClockwiseThrusters.Contains(thruster)) RecalculateCounterClockwiseTorque(); } } - if(x.item.EquippableItem.Rotation == ItemRotation.Reversed) - RecalculateForwardThrust(); } - }); + } + + ItemDamage.Select(x => x.item).Subscribe(CheckForThruster); + ItemOffline.Subscribe(CheckForThruster); + ItemOnline.Subscribe(CheckForThruster); } private void RecalculateForwardThrust() @@ -110,7 +118,8 @@ public class Ship : Entity ForwardThrust = 0; foreach (var thruster in _forwardThrusters) { - ForwardThrust += thruster.Thrust; + if(thruster.Item.Online) + ForwardThrust += thruster.Thrust; } } @@ -119,26 +128,43 @@ public class Ship : Entity ReverseThrust = 0; foreach (var thruster in _reverseThrusters) { - ReverseThrust += thruster.Thrust; + if(thruster.Item.Online) + ReverseThrust += thruster.Thrust; } } private void RecalculateLeftStrafeThrust() { LeftStrafeThrust = 0; + LeftStrafeTotalTorque = 0; foreach (var thruster in _leftThrusters) { - LeftStrafeThrust += thruster.Thrust; + if(thruster.Item.Online) + { + LeftStrafeThrust += thruster.Thrust; + LeftStrafeTotalTorque += thruster.Torque * thruster.Thrust; + } } + LeftStrafeTorqueThrusters = _leftThrusters + .Where(x => abs(sign(x.Torque) - sign(LeftStrafeTotalTorque)) < .01f) + .ToArray(); } private void RecalculateRightStrafeThrust() { RightStrafeThrust = 0; + RightStrafeTotalTorque = 0; foreach (var thruster in _rightThrusters) { - RightStrafeThrust += thruster.Thrust; + if(thruster.Item.Online) + { + RightStrafeThrust += thruster.Thrust; + RightStrafeTotalTorque += thruster.Torque * thruster.Thrust; + } } + RightStrafeTorqueThrusters = _rightThrusters + .Where(x => abs(sign(x.Torque) - sign(RightStrafeTotalTorque)) < .01f) + .ToArray(); } private void RecalculateClockwiseTorque() @@ -146,7 +172,8 @@ public class Ship : Entity ClockwiseTorque = 0; foreach (var thruster in _clockwiseThrusters) { - ClockwiseTorque += thruster.Torque; + if(thruster.Item.Online) + ClockwiseTorque += thruster.Torque; } } @@ -155,7 +182,8 @@ public class Ship : Entity CounterClockwiseTorque = 0; foreach (var thruster in _counterClockwiseThrusters) { - CounterClockwiseTorque -= thruster.Torque; + if(thruster.Item.Online) + CounterClockwiseTorque -= thruster.Torque; } } @@ -165,8 +193,24 @@ public class Ship : Entity if (_active) { foreach (var thruster in _allThrusters) thruster.Axis = 0; - foreach (var thruster in _rightThrusters) thruster.Axis += MovementDirection.x; - foreach (var thruster in _leftThrusters) thruster.Axis += -MovementDirection.x; + var rightThrusterTorqueCompensation = abs(RightStrafeTotalTorque) / RightStrafeTorqueThrusters.Length; + foreach (var thruster in _rightThrusters) + { + var thrust = 0f; + thrust += MovementDirection.x; + if (RightStrafeTorqueThrusters.Contains(thruster)) + thrust -= MovementDirection.x * (rightThrusterTorqueCompensation / (abs(thruster.Torque) * thruster.Thrust)); + thruster.Axis = thrust; + } + var leftThrusterTorqueCompensation = abs(LeftStrafeTotalTorque) / LeftStrafeTorqueThrusters.Length; + foreach (var thruster in _leftThrusters) + { + var thrust = 0f; + thrust += -MovementDirection.x; + if (LeftStrafeTorqueThrusters.Contains(thruster)) + thrust += MovementDirection.x * (leftThrusterTorqueCompensation / (abs(thruster.Torque) * thruster.Thrust)); + thruster.Axis = thrust; + } foreach (var thruster in _forwardThrusters) thruster.Axis += MovementDirection.y; foreach (var thruster in _reverseThrusters) thruster.Axis += -MovementDirection.y;