Skip to content

Commit

Permalink
offgrid friction
Browse files Browse the repository at this point in the history
  • Loading branch information
EmoGarbage404 committed Jun 23, 2024
1 parent 2fa8318 commit bab95d5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
6 changes: 6 additions & 0 deletions Robust.Shared/Physics/Components/PhysicsComponent.Physics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,12 @@ public sealed partial class PhysicsComponent : Component
Other = AccessPermissions.Read)]
public float LinearDamping = 0.2f;

/// <summary>
/// This is a set amount that the body's linear velocity is reduced by every tick when the body is off-grid.
/// </summary>
[DataField, Access(typeof(SharedPhysicsSystem), Friend = AccessPermissions.ReadWriteExecute, Other = AccessPermissions.Read)]
public float OffGridLinearDamping = 0.05f;

/// <summary>
/// This is a set amount that the body's angular velocity is reduced every tick.
/// Combined with the tile friction.
Expand Down
12 changes: 10 additions & 2 deletions Robust.Shared/Physics/Systems/SharedPhysicsSystem.Island.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Threading.Tasks;
using Microsoft.Extensions.ObjectPool;
using Robust.Shared.GameObjects;
using Robust.Shared.Map.Components;
using Robust.Shared.Maths;
using Robust.Shared.Physics.Components;
using Robust.Shared.Physics.Dynamics;
Expand Down Expand Up @@ -716,12 +717,14 @@ private void SolveIsland(
var angles = ArrayPool<float>.Shared.Rent(bodyCount);
var offset = island.Offset;
var xformQuery = GetEntityQuery<TransformComponent>();
var mapGridQuery = GetEntityQuery<MapGridComponent>();

for (var i = 0; i < island.Bodies.Count; i++)
{
var body = island.Bodies[i];
var xform = xformQuery.GetComponent(body.Owner);
var (worldPos, worldRot) =
_transform.GetWorldPositionRotation(xformQuery.GetComponent(body.Owner), xformQuery);
_transform.GetWorldPositionRotation(xform, xformQuery);

var transform = new Transform(worldPos, worldRot);
var position = Physics.Transform.Mul(transform, body.LocalCenter);
Expand All @@ -744,7 +747,12 @@ private void SolveIsland(

angularVelocity += body.InvI * body.Torque * data.FrameTime;

linearVelocity *= Math.Clamp(1.0f - data.FrameTime * body.LinearDamping, 0.0f, 1.0f);
var onGrid = xform.GridUid != null || mapGridQuery.HasComp(xform.MapUid);
var linearDamping = onGrid
? body.LinearDamping
: body.OffGridLinearDamping;

linearVelocity *= Math.Clamp(1.0f - data.FrameTime * linearDamping, 0.0f, 1.0f);
angularVelocity *= Math.Clamp(1.0f - data.FrameTime * body.AngularDamping, 0.0f, 1.0f);
}

Expand Down

0 comments on commit bab95d5

Please sign in to comment.