Improve throwing precision (#29726)
* improve throwing precision * remove debugging logs * minor fixes * f * Update Content.Shared/Throwing/LandAtCursorComponent.cs --------- Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
This commit is contained in:
@@ -1,13 +1,12 @@
|
||||
using System.Numerics;
|
||||
using Content.Shared.Administration.Logs;
|
||||
using Content.Shared.Camera;
|
||||
using Content.Shared.CCVar;
|
||||
using Content.Shared.Database;
|
||||
using Content.Shared.Friction;
|
||||
using Content.Shared.Gravity;
|
||||
using Content.Shared.Hands.Components;
|
||||
using Content.Shared.Hands.EntitySystems;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Projectiles;
|
||||
using Content.Shared.Tag;
|
||||
using Robust.Shared.Configuration;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Physics;
|
||||
using Robust.Shared.Physics.Components;
|
||||
@@ -31,7 +30,10 @@ public sealed class ThrowingSystem : EntitySystem
|
||||
/// The minimum amount of time an entity needs to be thrown before the timer can be run.
|
||||
/// Anything below this threshold never enters the air.
|
||||
/// </summary>
|
||||
public const float FlyTime = 0.15f;
|
||||
public const float MinFlyTime = 0.15f;
|
||||
public const float FlyTimePercentage = 0.8f;
|
||||
|
||||
private float _frictionModifier;
|
||||
|
||||
[Dependency] private readonly IGameTiming _gameTiming = default!;
|
||||
[Dependency] private readonly SharedGravitySystem _gravity = default!;
|
||||
@@ -40,13 +42,23 @@ public sealed class ThrowingSystem : EntitySystem
|
||||
[Dependency] private readonly ThrownItemSystem _thrownSystem = default!;
|
||||
[Dependency] private readonly SharedCameraRecoilSystem _recoil = default!;
|
||||
[Dependency] private readonly ISharedAdminLogManager _adminLogger = default!;
|
||||
[Dependency] private readonly IConfigurationManager _configManager = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
Subs.CVar(_configManager, CCVars.TileFrictionModifier, value => _frictionModifier = value, true);
|
||||
}
|
||||
|
||||
public void TryThrow(
|
||||
EntityUid uid,
|
||||
EntityCoordinates coordinates,
|
||||
float strength = 1.0f,
|
||||
float baseThrowSpeed = 10.0f,
|
||||
EntityUid? user = null,
|
||||
float pushbackRatio = PushbackDefault,
|
||||
float? friction = null,
|
||||
bool compensateFriction = false,
|
||||
bool recoil = true,
|
||||
bool animated = true,
|
||||
bool playSound = true,
|
||||
@@ -58,7 +70,7 @@ public sealed class ThrowingSystem : EntitySystem
|
||||
if (mapPos.MapId != thrownPos.MapId)
|
||||
return;
|
||||
|
||||
TryThrow(uid, mapPos.Position - thrownPos.Position, strength, user, pushbackRatio, recoil: recoil, animated: animated, playSound: playSound, doSpin: doSpin);
|
||||
TryThrow(uid, mapPos.Position - thrownPos.Position, baseThrowSpeed, user, pushbackRatio, friction, compensateFriction: compensateFriction, recoil: recoil, animated: animated, playSound: playSound, doSpin: doSpin);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -66,14 +78,18 @@ public sealed class ThrowingSystem : EntitySystem
|
||||
/// </summary>
|
||||
/// <param name="uid">The entity being thrown.</param>
|
||||
/// <param name="direction">A vector pointing from the entity to its destination.</param>
|
||||
/// <param name="strength">How much the direction vector should be multiplied for velocity.</param>
|
||||
/// <param name="baseThrowSpeed">Throw velocity. Gets modified if compensateFriction is true.</param>
|
||||
/// <param name="pushbackRatio">The ratio of impulse applied to the thrower - defaults to 10 because otherwise it's not enough to properly recover from getting spaced</param>
|
||||
/// <param name="friction">friction value used for the distance calculation. If set to null this defaults to the standard tile values</param>
|
||||
/// <param name="compensateFriction">True will adjust the throw so the item stops at the target coordinates. False means it will land at the target and keep sliding.</param>
|
||||
/// <param name="doSpin">Whether spin will be applied to the thrown entity.</param>
|
||||
public void TryThrow(EntityUid uid,
|
||||
Vector2 direction,
|
||||
float strength = 1.0f,
|
||||
float baseThrowSpeed = 10.0f,
|
||||
EntityUid? user = null,
|
||||
float pushbackRatio = PushbackDefault,
|
||||
float? friction = null,
|
||||
bool compensateFriction = false,
|
||||
bool recoil = true,
|
||||
bool animated = true,
|
||||
bool playSound = true,
|
||||
@@ -91,9 +107,10 @@ public sealed class ThrowingSystem : EntitySystem
|
||||
physics,
|
||||
Transform(uid),
|
||||
projectileQuery,
|
||||
strength,
|
||||
baseThrowSpeed,
|
||||
user,
|
||||
pushbackRatio, recoil: recoil, animated: animated, playSound: playSound, doSpin: doSpin);
|
||||
pushbackRatio,
|
||||
friction, compensateFriction: compensateFriction, recoil: recoil, animated: animated, playSound: playSound, doSpin: doSpin);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -101,23 +118,27 @@ public sealed class ThrowingSystem : EntitySystem
|
||||
/// </summary>
|
||||
/// <param name="uid">The entity being thrown.</param>
|
||||
/// <param name="direction">A vector pointing from the entity to its destination.</param>
|
||||
/// <param name="strength">How much the direction vector should be multiplied for velocity.</param>
|
||||
/// <param name="baseThrowSpeed">Throw velocity. Gets modified if compensateFriction is true.</param>
|
||||
/// <param name="pushbackRatio">The ratio of impulse applied to the thrower - defaults to 10 because otherwise it's not enough to properly recover from getting spaced</param>
|
||||
/// <param name="friction">friction value used for the distance calculation. If set to null this defaults to the standard tile values</param>
|
||||
/// <param name="compensateFriction">True will adjust the throw so the item stops at the target coordinates. False means it will land at the target and keep sliding.</param>
|
||||
/// <param name="doSpin">Whether spin will be applied to the thrown entity.</param>
|
||||
public void TryThrow(EntityUid uid,
|
||||
Vector2 direction,
|
||||
PhysicsComponent physics,
|
||||
TransformComponent transform,
|
||||
EntityQuery<ProjectileComponent> projectileQuery,
|
||||
float strength = 1.0f,
|
||||
float baseThrowSpeed = 10.0f,
|
||||
EntityUid? user = null,
|
||||
float pushbackRatio = PushbackDefault,
|
||||
float? friction = null,
|
||||
bool compensateFriction = false,
|
||||
bool recoil = true,
|
||||
bool animated = true,
|
||||
bool playSound = true,
|
||||
bool doSpin = true)
|
||||
{
|
||||
if (strength <= 0 || direction == Vector2Helpers.Infinity || direction == Vector2Helpers.NaN || direction == Vector2.Zero)
|
||||
if (baseThrowSpeed <= 0 || direction == Vector2Helpers.Infinity || direction == Vector2Helpers.NaN || direction == Vector2.Zero || friction < 0)
|
||||
return;
|
||||
|
||||
if ((physics.BodyType & (BodyType.Dynamic | BodyType.KinematicController)) == 0x0)
|
||||
@@ -136,16 +157,22 @@ public sealed class ThrowingSystem : EntitySystem
|
||||
Animate = animated,
|
||||
};
|
||||
|
||||
// Estimate time to arrival so we can apply OnGround status and slow it much faster.
|
||||
var time = direction.Length() / strength;
|
||||
// if not given, get the default friction value for distance calculation
|
||||
var tileFriction = friction ?? _frictionModifier * TileFrictionController.DefaultFriction;
|
||||
|
||||
if (tileFriction == 0f)
|
||||
compensateFriction = false; // cannot calculate this if there is no friction
|
||||
|
||||
// Set the time the item is supposed to be in the air so we can apply OnGround status.
|
||||
// This is a free parameter, but we should set it to something reasonable.
|
||||
var flyTime = direction.Length() / baseThrowSpeed;
|
||||
if (compensateFriction)
|
||||
flyTime *= FlyTimePercentage;
|
||||
|
||||
if (flyTime < MinFlyTime)
|
||||
flyTime = 0f;
|
||||
comp.ThrownTime = _gameTiming.CurTime;
|
||||
// TODO: This is a bandaid, don't do this.
|
||||
// if you want to force landtime have the caller handle it or add a new method.
|
||||
// did we launch this with something stronger than our hands?
|
||||
if (TryComp<HandsComponent>(comp.Thrower, out var hands) && strength > hands.ThrowForceMultiplier)
|
||||
comp.LandTime = comp.ThrownTime + TimeSpan.FromSeconds(time);
|
||||
else
|
||||
comp.LandTime = time < FlyTime ? default : comp.ThrownTime + TimeSpan.FromSeconds(time - FlyTime);
|
||||
comp.LandTime = comp.ThrownTime + TimeSpan.FromSeconds(flyTime);
|
||||
comp.PlayLandSound = playSound;
|
||||
AddComp(uid, comp, true);
|
||||
|
||||
@@ -173,7 +200,12 @@ public sealed class ThrowingSystem : EntitySystem
|
||||
if (user != null)
|
||||
_adminLogger.Add(LogType.Throw, LogImpact.Low, $"{ToPrettyString(user.Value):user} threw {ToPrettyString(uid):entity}");
|
||||
|
||||
var impulseVector = direction.Normalized() * strength * physics.Mass;
|
||||
// if compensateFriction==true compensate for the distance the item will slide over the floor after landing by reducing the throw speed accordingly.
|
||||
// else let the item land on the cursor and from where it slides a little further.
|
||||
// This is an exact formula we get from exponentially decaying velocity after landing.
|
||||
// If someone changes how tile friction works at some point, this will have to be adjusted.
|
||||
var throwSpeed = compensateFriction ? direction.Length() / (flyTime + 1 / tileFriction) : baseThrowSpeed;
|
||||
var impulseVector = direction.Normalized() * throwSpeed * physics.Mass;
|
||||
_physics.ApplyLinearImpulse(uid, impulseVector, body: physics);
|
||||
|
||||
if (comp.LandTime == null || comp.LandTime <= TimeSpan.Zero)
|
||||
|
||||
Reference in New Issue
Block a user