Piloting and thruster tuning (#5594)

Co-authored-by: metalgearsloth <metalgearsloth@gmail.com>
This commit is contained in:
metalgearsloth
2021-12-03 13:03:06 +11:00
committed by GitHub
parent f4c10295dc
commit 56845da37b
6 changed files with 218 additions and 208 deletions

View File

@@ -36,6 +36,7 @@ namespace Content.Server.Physics.Controllers
private float _shuttleDockSpeedCap; private float _shuttleDockSpeedCap;
private HashSet<EntityUid> _excludedMobs = new(); private HashSet<EntityUid> _excludedMobs = new();
private Dictionary<ShuttleComponent, List<(PilotComponent, IMoverComponent)>> _shuttlePilots = new();
public override void Initialize() public override void Initialize()
{ {
@@ -56,12 +57,7 @@ namespace Content.Server.Physics.Controllers
HandleMobMovement(mover, physics, mobMover); HandleMobMovement(mover, physics, mobMover);
} }
foreach (var (pilot, mover) in EntityManager.EntityQuery<PilotComponent, SharedPlayerInputMoverComponent>()) HandleShuttleMovement(frameTime);
{
if (pilot.Console == null) continue;
_excludedMobs.Add(mover.Owner.Uid);
HandleShuttleMovement(mover);
}
foreach (var (mover, physics) in EntityManager.EntityQuery<IMoverComponent, PhysicsComponent>(true)) foreach (var (mover, physics) in EntityManager.EntityQuery<IMoverComponent, PhysicsComponent>(true))
{ {
@@ -71,43 +67,120 @@ namespace Content.Server.Physics.Controllers
} }
} }
/* private void HandleShuttleMovement(float frameTime)
* Some thoughts:
* Unreal actually doesn't predict vehicle movement at all, it's purely server-side which I thought was interesting
* The reason for this is that vehicles change direction very slowly compared to players so you don't really have the requirement for quick movement anyway
* As such could probably just look at applying a force / impulse to the shuttle server-side only so it controls like the titanic.
*/
private void HandleShuttleMovement(SharedPlayerInputMoverComponent mover)
{ {
var gridId = mover.Owner.Transform.GridID; var newPilots = new Dictionary<ShuttleComponent, List<(PilotComponent, IMoverComponent)>>();
if (!_mapManager.TryGetGrid(gridId, out var grid) || !EntityManager.TryGetEntity(grid.GridEntityId, out var gridEntity)) return; // We just mark off their movement and the shuttle itself does its own movement
foreach (var (pilot, mover, xform) in EntityManager.EntityQuery<PilotComponent, SharedPlayerInputMoverComponent, TransformComponent>())
if (!gridEntity.TryGetComponent(out ShuttleComponent? shuttleComponent) ||
!gridEntity.TryGetComponent(out PhysicsComponent? physicsComponent))
{ {
return; if (pilot.Console == null) continue;
_excludedMobs.Add(mover.Owner.Uid);
var gridId = xform.GridID;
if (!_mapManager.TryGetGrid(gridId, out var grid) ||
!EntityManager.TryGetComponent(grid.GridEntityId, out ShuttleComponent? shuttleComponent)) continue;
if (!newPilots.TryGetValue(shuttleComponent, out var pilots))
{
pilots = new List<(PilotComponent, IMoverComponent)>();
newPilots[shuttleComponent] = pilots;
}
pilots.Add((pilot, mover));
} }
// ShuttleSystem has already worked out the ratio so we'll just multiply it back by the mass. var shuttleSystem = EntitySystem.Get<ShuttleSystem>();
var movement = mover.VelocityDir.walking + mover.VelocityDir.sprinting; var thrusterSystem = EntitySystem.Get<ThrusterSystem>();
var system = EntitySystem.Get<ThrusterSystem>();
if (movement.Length.Equals(0f)) // Reset inputs for non-piloted shuttles.
foreach (var (shuttle, _) in _shuttlePilots)
{ {
// TODO: This visualization doesn't work with multiple pilots so need to address that somehow. if (newPilots.ContainsKey(shuttle)) continue;
system.DisableAllThrustDirections(shuttleComponent);
return; thrusterSystem.DisableLinearThrusters(shuttle);
} }
var speedCap = 0f; _shuttlePilots = newPilots;
switch (shuttleComponent.Mode) // Collate all of the linear / angular velocites for a shuttle
// then do the movement input once for it.
foreach (var (shuttle, pilots) in _shuttlePilots)
{ {
case ShuttleMode.Docking: if (shuttle.Paused || !EntityManager.TryGetComponent(shuttle.OwnerUid, out PhysicsComponent? body)) continue;
system.DisableAllThrustDirections(shuttleComponent);
var dockDirection = movement.ToWorldAngle().GetDir(); // Collate movement linear and angular inputs together
var dockFlag = dockDirection.AsFlag(); var linearInput = Vector2.Zero;
var angularInput = 0f;
switch (shuttle.Mode)
{
case ShuttleMode.Cruise:
foreach (var (pilot, mover) in pilots)
{
var console = pilot.Console;
if (console == null)
{
DebugTools.Assert(false);
continue;
}
var sprint = mover.VelocityDir.sprinting;
if (sprint.Equals(Vector2.Zero)) continue;
var offsetRotation = EntityManager.GetComponent<TransformComponent>(console.OwnerUid).LocalRotation;
linearInput += offsetRotation.RotateVec(new Vector2(0f, sprint.Y));
angularInput += sprint.X;
}
break;
case ShuttleMode.Docking:
// No angular input possible
foreach (var (pilot, mover) in pilots)
{
var console = pilot.Console;
if (console == null)
{
DebugTools.Assert(false);
continue;
}
var sprint = mover.VelocityDir.sprinting;
if (sprint.Equals(Vector2.Zero)) continue;
var offsetRotation = EntityManager.GetComponent<TransformComponent>(console.OwnerUid).LocalRotation;
sprint = offsetRotation.RotateVec(sprint);
linearInput += sprint;
}
break;
default:
throw new ArgumentOutOfRangeException();
}
var count = pilots.Count;
linearInput /= count;
angularInput /= count;
// Handle shuttle movement
if (linearInput.Length.Equals(0f))
{
thrusterSystem.DisableLinearThrusters(shuttle);
body.LinearDamping = shuttleSystem.ShuttleIdleLinearDamping;
}
else
{
body.LinearDamping = shuttleSystem.ShuttleMovingLinearDamping;
var angle = linearInput.ToWorldAngle();
var linearDir = angle.GetDir();
var dockFlag = linearDir.AsFlag();
var shuttleNorth = EntityManager.GetComponent<TransformComponent>(body.OwnerUid).WorldRotation.ToWorldVec();
// Won't just do cardinal directions. // Won't just do cardinal directions.
foreach (DirectionFlag dir in Enum.GetValues(typeof(DirectionFlag))) foreach (DirectionFlag dir in Enum.GetValues(typeof(DirectionFlag)))
@@ -124,108 +197,74 @@ namespace Content.Server.Physics.Controllers
continue; continue;
} }
if ((dir & dockFlag) == 0x0) continue; if ((dir & dockFlag) == 0x0)
{
thrusterSystem.DisableLinearThrustDirection(shuttle, dir);
continue;
}
system.EnableThrustDirection(shuttleComponent, dir); float length;
switch (dir)
{
case DirectionFlag.North:
length = linearInput.Y;
break;
case DirectionFlag.South:
length = -linearInput.Y;
break;
case DirectionFlag.East:
length = linearInput.X;
break;
case DirectionFlag.West:
length = -linearInput.X;
break;
default:
throw new ArgumentOutOfRangeException();
}
thrusterSystem.EnableLinearThrustDirection(shuttle, dir);
var index = (int) Math.Log2((int) dir); var index = (int) Math.Log2((int) dir);
var speed = shuttle.LinearThrusterImpulse[index] * length;
var dockSpeed = shuttleComponent.LinearThrusterImpulse[index]; if (body.LinearVelocity.LengthSquared < 0.5f)
// TODO: Cvar for the speed drop
dockSpeed /= 5f;
if (physicsComponent.LinearVelocity.LengthSquared == 0f)
{ {
dockSpeed *= 5f; speed *= 5f;
} }
var moveAngle = movement.ToWorldAngle(); body.ApplyLinearImpulse(
angle.RotateVec(shuttleNorth) *
physicsComponent.ApplyLinearImpulse(moveAngle.RotateVec(physicsComponent.Owner.Transform.WorldRotation.ToWorldVec()) * speed *
dockSpeed); frameTime);
} }
}
speedCap = _shuttleDockSpeedCap; if (MathHelper.CloseTo(angularInput, 0f))
break; {
case ShuttleMode.Cruise: thrusterSystem.SetAngularThrust(shuttle, false);
if (movement.Y == 0f) body.AngularDamping = shuttleSystem.ShuttleIdleAngularDamping;
}
else
{
body.AngularDamping = shuttleSystem.ShuttleMovingAngularDamping;
var angularSpeed = shuttle.AngularThrust;
if (body.AngularVelocity < 0.5f)
{ {
system.DisableThrustDirection(shuttleComponent, DirectionFlag.South); angularSpeed *= 5f;
system.DisableThrustDirection(shuttleComponent, DirectionFlag.North);
}
else
{
var direction = movement.Y > 0f ? Direction.North : Direction.South;
var linearSpeed = shuttleComponent.LinearThrusterImpulse[(int) direction / 2];
if (physicsComponent.LinearVelocity.LengthSquared == 0f)
{
linearSpeed *= 5f;
}
// Currently this is slow BUT we'd have a separate multiplier for docking and cruising or whatever.
physicsComponent.ApplyLinearImpulse(physicsComponent.Owner.Transform.WorldRotation.Opposite().ToWorldVec() *
linearSpeed *
movement.Y);
switch (direction)
{
case Direction.North:
system.DisableThrustDirection(shuttleComponent, DirectionFlag.South);
system.EnableThrustDirection(shuttleComponent, DirectionFlag.North);
break;
case Direction.South:
system.DisableThrustDirection(shuttleComponent, DirectionFlag.North);
system.EnableThrustDirection(shuttleComponent, DirectionFlag.South);
break;
}
} }
var angularSpeed = shuttleComponent.AngularThrust; // Scale rotation by mass just to make rotating larger things a bit more bearable.
body.ApplyAngularImpulse(
-angularInput *
angularSpeed *
frameTime *
body.Mass / 100f);
if (movement.X == 0f) thrusterSystem.SetAngularThrust(shuttle, true);
{ }
system.DisableThrustDirection(shuttleComponent, DirectionFlag.West);
system.DisableThrustDirection(shuttleComponent, DirectionFlag.East);
}
else if (movement.X != 0f)
{
physicsComponent.ApplyAngularImpulse(-movement.X * angularSpeed);
if (movement.X < 0f)
{
system.EnableThrustDirection(shuttleComponent, DirectionFlag.West);
system.DisableThrustDirection(shuttleComponent, DirectionFlag.East);
}
else
{
system.EnableThrustDirection(shuttleComponent, DirectionFlag.East);
system.DisableThrustDirection(shuttleComponent, DirectionFlag.West);
}
}
// TODO WHEN THIS ACTUALLY WORKS
speedCap = _shuttleDockSpeedCap * 10;
break;
default:
throw new ArgumentOutOfRangeException();
} }
var velocity = physicsComponent.LinearVelocity;
var angVelocity = physicsComponent.AngularVelocity;
if (velocity.Length > speedCap)
{
physicsComponent.LinearVelocity = velocity.Normalized * speedCap;
}
/* TODO: Need to suss something out but this PR already BEEG
if (angVelocity > speedCap)
{
physicsComponent.AngularVelocity = speedCap;
}
*/
} }
protected override void HandleFootsteps(IMoverComponent mover, IMobMoverComponent mobMover) protected override void HandleFootsteps(IMoverComponent mover, IMobMoverComponent mobMover)

View File

@@ -51,7 +51,7 @@ namespace Content.Server.Shuttles.Components
[ViewVariables] [ViewVariables]
[DataField("impulse")] [DataField("impulse")]
public float Impulse = 5f; public float Impulse = 450f;
[ViewVariables] [ViewVariables]
[DataField("thrusterType")] [DataField("thrusterType")]

View File

@@ -11,6 +11,12 @@ namespace Content.Server.Shuttles.EntitySystems
{ {
private const float TileMassMultiplier = 4f; private const float TileMassMultiplier = 4f;
public float ShuttleIdleLinearDamping = 0.1f;
public float ShuttleIdleAngularDamping = 0.2f;
public float ShuttleMovingLinearDamping = 0.05f;
public float ShuttleMovingAngularDamping = 0.05f;
public override void Initialize() public override void Initialize()
{ {
base.Initialize(); base.Initialize();
@@ -88,8 +94,8 @@ namespace Content.Server.Shuttles.EntitySystems
component.BodyStatus = BodyStatus.InAir; component.BodyStatus = BodyStatus.InAir;
//component.FixedRotation = false; TODO WHEN ROTATING SHUTTLES FIXED. //component.FixedRotation = false; TODO WHEN ROTATING SHUTTLES FIXED.
component.FixedRotation = false; component.FixedRotation = false;
component.LinearDamping = 0.2f; component.LinearDamping = ShuttleIdleLinearDamping;
component.AngularDamping = 0.3f; component.AngularDamping = ShuttleIdleAngularDamping;
} }
private void Disable(PhysicsComponent component) private void Disable(PhysicsComponent component)

View File

@@ -404,46 +404,60 @@ namespace Content.Server.Shuttles.EntitySystems
/// <summary> /// <summary>
/// Considers a thrust direction as being active. /// Considers a thrust direction as being active.
/// </summary> /// </summary>
public void EnableThrustDirection(ShuttleComponent component, DirectionFlag direction) public void EnableLinearThrustDirection(ShuttleComponent component, DirectionFlag direction)
{ {
if ((component.ThrustDirections & direction) != 0x0) return; if ((component.ThrustDirections & direction) != 0x0) return;
component.ThrustDirections |= direction; component.ThrustDirections |= direction;
if ((direction & (DirectionFlag.East | DirectionFlag.West)) != 0x0) var index = GetFlagIndex(direction);
foreach (var comp in component.LinearThrusters[index])
{ {
switch (component.Mode) if (!EntityManager.TryGetComponent(comp.OwnerUid, out AppearanceComponent? appearanceComponent))
{ continue;
case ShuttleMode.Cruise:
foreach (var comp in component.AngularThrusters)
{
if (!EntityManager.TryGetComponent(comp.OwnerUid, out AppearanceComponent? appearanceComponent))
continue;
comp.Firing = true; comp.Firing = true;
appearanceComponent.SetData(ThrusterVisualState.Thrusting, true); appearanceComponent.SetData(ThrusterVisualState.Thrusting, true);
}
break;
case ShuttleMode.Docking:
var index = GetFlagIndex(direction);
foreach (var comp in component.LinearThrusters[index])
{
if (!EntityManager.TryGetComponent(comp.OwnerUid, out AppearanceComponent? appearanceComponent))
continue;
comp.Firing = true;
appearanceComponent.SetData(ThrusterVisualState.Thrusting, true);
}
break;
}
} }
else }
{
var index = GetFlagIndex(direction);
foreach (var comp in component.LinearThrusters[index]) /// <summary>
/// Disables a thrust direction.
/// </summary>
public void DisableLinearThrustDirection(ShuttleComponent component, DirectionFlag direction)
{
if ((component.ThrustDirections & direction) == 0x0) return;
component.ThrustDirections &= ~direction;
var index = GetFlagIndex(direction);
foreach (var comp in component.LinearThrusters[index])
{
if (!EntityManager.TryGetComponent(comp.OwnerUid, out AppearanceComponent? appearanceComponent))
continue;
comp.Firing = false;
appearanceComponent.SetData(ThrusterVisualState.Thrusting, false);
}
}
public void DisableLinearThrusters(ShuttleComponent component)
{
foreach (DirectionFlag dir in Enum.GetValues(typeof(DirectionFlag)))
{
DisableLinearThrustDirection(component, dir);
}
DebugTools.Assert(component.ThrustDirections == DirectionFlag.None);
}
public void SetAngularThrust(ShuttleComponent component, bool on)
{
if (on)
{
foreach (var comp in component.AngularThrusters)
{ {
if (!EntityManager.TryGetComponent(comp.OwnerUid, out AppearanceComponent? appearanceComponent)) if (!EntityManager.TryGetComponent(comp.OwnerUid, out AppearanceComponent? appearanceComponent))
continue; continue;
@@ -452,51 +466,9 @@ namespace Content.Server.Shuttles.EntitySystems
appearanceComponent.SetData(ThrusterVisualState.Thrusting, true); appearanceComponent.SetData(ThrusterVisualState.Thrusting, true);
} }
} }
}
/// <summary>
/// Disables a thrust direction.
/// </summary>
public void DisableThrustDirection(ShuttleComponent component, DirectionFlag direction)
{
if ((component.ThrustDirections & direction) == 0x0) return;
component.ThrustDirections &= ~direction;
if ((direction & (DirectionFlag.East | DirectionFlag.West)) != 0x0)
{
switch (component.Mode)
{
case ShuttleMode.Cruise:
foreach (var comp in component.AngularThrusters)
{
if (!EntityManager.TryGetComponent(comp.OwnerUid, out AppearanceComponent? appearanceComponent))
continue;
comp.Firing = false;
appearanceComponent.SetData(ThrusterVisualState.Thrusting, false);
}
break;
case ShuttleMode.Docking:
var index = GetFlagIndex(direction);
foreach (var comp in component.LinearThrusters[index])
{
if (!EntityManager.TryGetComponent(comp.OwnerUid, out AppearanceComponent? appearanceComponent))
continue;
comp.Firing = false;
appearanceComponent.SetData(ThrusterVisualState.Thrusting, false);
}
break;
}
}
else else
{ {
var index = GetFlagIndex(direction); foreach (var comp in component.AngularThrusters)
foreach (var comp in component.LinearThrusters[index])
{ {
if (!EntityManager.TryGetComponent(comp.OwnerUid, out AppearanceComponent? appearanceComponent)) if (!EntityManager.TryGetComponent(comp.OwnerUid, out AppearanceComponent? appearanceComponent))
continue; continue;
@@ -507,16 +479,6 @@ namespace Content.Server.Shuttles.EntitySystems
} }
} }
public void DisableAllThrustDirections(ShuttleComponent component)
{
foreach (DirectionFlag dir in Enum.GetValues(typeof(DirectionFlag)))
{
DisableThrustDirection(component, dir);
}
DebugTools.Assert(component.ThrustDirections == DirectionFlag.None);
}
#endregion #endregion
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]

View File

@@ -25,6 +25,8 @@
radius: 1.5 radius: 1.5
energy: 1.6 energy: 1.6
color: "#43ccb5" color: "#43ccb5"
- type: Rotatable
rotateWhileAnchored: true
- type: entity - type: entity
parent: ComputerShuttleBase parent: ComputerShuttleBase

View File

@@ -47,6 +47,7 @@
name: thruster name: thruster
description: It goes nyooooooom. description: It goes nyooooooom.
components: components:
- type: Thruster
- type: Sprite - type: Sprite
sprite: Structures/Shuttles/thruster.rsi sprite: Structures/Shuttles/thruster.rsi
layers: layers:
@@ -68,7 +69,7 @@
components: components:
- type: Thruster - type: Thruster
thrusterType: Angular thrusterType: Angular
impulse: 2 impulse: 200
- type: Sprite - type: Sprite
# Listen I'm not the biggest fan of the sprite but it was the most appropriate thing I could find. # Listen I'm not the biggest fan of the sprite but it was the most appropriate thing I could find.
sprite: Structures/Shuttles/gyroscope.rsi sprite: Structures/Shuttles/gyroscope.rsi