Inertia (#3709)
* Some stuff * Fix NaN angular velocity * Optimise a bit * Give throwing a bit of a spin * Reality can be whatever I want * Biffing it * Cleanup * Update submodule Co-authored-by: Metal Gear Sloth <metalgearsloth@gmail.com>
This commit is contained in:
@@ -48,43 +48,80 @@ namespace Content.Shared.Physics.Controllers
|
||||
|
||||
foreach (var body in map.AwakeBodies)
|
||||
{
|
||||
if (prediction && !body.Predict) continue;
|
||||
|
||||
var speed = body.LinearVelocity.Length;
|
||||
|
||||
if (speed <= 0.0f || body.BodyStatus == BodyStatus.InAir) continue;
|
||||
|
||||
// This is the *actual* amount that speed will drop by, we just do some multiplication around it to be easier.
|
||||
var drop = 0.0f;
|
||||
float control;
|
||||
|
||||
// Only apply friction when it's not a mob (or the mob doesn't have control).
|
||||
if (SharedMoverController.UseMobMovement(_broadPhaseSystem, body, _physicsManager)) continue;
|
||||
// Only apply friction when it's not a mob (or the mob doesn't have control)
|
||||
if (prediction && !body.Predict ||
|
||||
body.BodyStatus == BodyStatus.InAir ||
|
||||
SharedMoverController.UseMobMovement(_broadPhaseSystem, body, _physicsManager)) continue;
|
||||
|
||||
var surfaceFriction = GetTileFriction(body);
|
||||
var bodyModifier = body.Owner.GetComponentOrNull<SharedTileFrictionModifier>()?.Modifier ?? 1.0f;
|
||||
var friction = _frictionModifier * surfaceFriction * bodyModifier;
|
||||
|
||||
if (friction > 0.0f)
|
||||
{
|
||||
// TBH I can't really tell if this makes a difference.
|
||||
if (!prediction)
|
||||
{
|
||||
control = speed < _stopSpeed ? _stopSpeed : speed;
|
||||
}
|
||||
else
|
||||
{
|
||||
control = speed;
|
||||
}
|
||||
ReduceLinearVelocity(prediction, body, friction, frameTime);
|
||||
ReduceAngularVelocity(prediction, body, friction, frameTime);
|
||||
}
|
||||
}
|
||||
|
||||
drop += control * friction * frameTime;
|
||||
private void ReduceLinearVelocity(bool prediction, PhysicsComponent body, float friction, float frameTime)
|
||||
{
|
||||
var speed = body.LinearVelocity.Length;
|
||||
|
||||
if (speed <= 0.0f) return;
|
||||
|
||||
// This is the *actual* amount that speed will drop by, we just do some multiplication around it to be easier.
|
||||
var drop = 0.0f;
|
||||
float control;
|
||||
|
||||
if (friction > 0.0f)
|
||||
{
|
||||
// TBH I can't really tell if this makes a difference.
|
||||
if (!prediction)
|
||||
{
|
||||
control = speed < _stopSpeed ? _stopSpeed : speed;
|
||||
}
|
||||
else
|
||||
{
|
||||
control = speed;
|
||||
}
|
||||
|
||||
var newSpeed = MathF.Max(0.0f, speed - drop);
|
||||
|
||||
newSpeed /= speed;
|
||||
body.LinearVelocity *= newSpeed;
|
||||
drop += control * friction * frameTime;
|
||||
}
|
||||
|
||||
var newSpeed = MathF.Max(0.0f, speed - drop);
|
||||
|
||||
newSpeed /= speed;
|
||||
body.LinearVelocity *= newSpeed;
|
||||
}
|
||||
|
||||
private void ReduceAngularVelocity(bool prediction, PhysicsComponent body, float friction, float frameTime)
|
||||
{
|
||||
var speed = MathF.Abs(body.AngularVelocity);
|
||||
|
||||
if (speed <= 0.0f) return;
|
||||
|
||||
// This is the *actual* amount that speed will drop by, we just do some multiplication around it to be easier.
|
||||
var drop = 0.0f;
|
||||
float control;
|
||||
|
||||
if (friction > 0.0f)
|
||||
{
|
||||
// TBH I can't really tell if this makes a difference.
|
||||
if (!prediction)
|
||||
{
|
||||
control = speed < _stopSpeed ? _stopSpeed : speed;
|
||||
}
|
||||
else
|
||||
{
|
||||
control = speed;
|
||||
}
|
||||
|
||||
drop += control * friction * frameTime;
|
||||
}
|
||||
|
||||
var newSpeed = MathF.Max(0.0f, speed - drop);
|
||||
|
||||
newSpeed /= speed;
|
||||
body.AngularVelocity *= newSpeed;
|
||||
}
|
||||
|
||||
[Pure]
|
||||
|
||||
Reference in New Issue
Block a user