Skip to content

Commit 518e006

Browse files
Spajker7xen2
authored andcommitted
Avoid calculating inertia for StaticComponent
1 parent 3e52f84 commit 518e006

6 files changed

Lines changed: 42 additions & 8 deletions

File tree

‎sources/engine/Stride.BepuPhysics/Stride.BepuPhysics/CollidableComponent.cs‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,17 @@ public IContactHandler? ContactEventHandler
253253

254254
protected internal abstract CollidableReference? CollidableReference { get; }
255255

256+
/// <summary>
257+
/// Whether this collidable needs its collider's inertia when attaching, see
258+
/// <see cref="AttachInner"/>'s <c>shapeInertia</c> parameter.
259+
/// </summary>
260+
/// <remarks>
261+
/// Inertia only affects bodies; statics discard it, and computing it is not free -
262+
/// <see cref="Definitions.Colliders.MeshCollider"/>'s inertia iterates over every
263+
/// triangle of the mesh. Colliders skip that work when this is false.
264+
/// </remarks>
265+
internal virtual bool ShouldCalculateInertia => true;
266+
256267
public CollidableComponent()
257268
{
258269
_collider = new CompoundCollider();
@@ -277,7 +288,7 @@ internal void ReAttach(BepuSimulation onSimulation)
277288

278289
Debug.Assert(Processor is not null);
279290

280-
if (false == Collider.TryAttach(onSimulation.Simulation.Shapes, onSimulation.BufferPool, Processor.ShapeCache, out var index, out var centerOfMass, out var shapeInertia))
291+
if (false == Collider.TryAttach(onSimulation.Simulation.Shapes, onSimulation.BufferPool, Processor.ShapeCache, ShouldCalculateInertia, out var index, out var centerOfMass, out var shapeInertia))
281292
{
282293
return;
283294
}

‎sources/engine/Stride.BepuPhysics/Stride.BepuPhysics/Definitions/Colliders/CompoundCollider.cs‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public void GetLocalTransforms(CollidableComponent collidable, Span<ShapeTransfo
6565
}
6666
}
6767

68-
bool ICollider.TryAttach(Shapes shapes, BufferPool pool, ShapeCacheSystem shapeCache, out TypedIndex index, out Vector3 centerOfMass, out BodyInertia inertia)
68+
bool ICollider.TryAttach(Shapes shapes, BufferPool pool, ShapeCacheSystem shapeCache, bool shouldCalculateInertia, out TypedIndex index, out Vector3 centerOfMass, out BodyInertia inertia)
6969
{
7070
if (_colliders.Count == 0)
7171
{
@@ -89,7 +89,18 @@ bool ICollider.TryAttach(Shapes shapes, BufferPool pool, ShapeCacheSystem shapeC
8989

9090
Buffer<CompoundChild> compoundChildren;
9191
System.Numerics.Vector3 shapeCenter;
92-
compoundBuilder.BuildDynamicCompound(out compoundChildren, out inertia, out shapeCenter);
92+
93+
if (shouldCalculateInertia)
94+
{
95+
compoundBuilder.BuildDynamicCompound(out compoundChildren, out inertia, out shapeCenter);
96+
}
97+
else
98+
{
99+
// Same children and same weight-weighted center as BuildDynamicCompound, minus
100+
// combining the children's inertia (it never reads their LocalInverseInertia)
101+
compoundBuilder.BuildKinematicCompound(out compoundChildren, out shapeCenter);
102+
inertia = default;
103+
}
93104

94105
index = IsBig ? shapes.Add(new BigCompound(compoundChildren, shapes, pool)) : shapes.Add(new Compound(compoundChildren));
95106
centerOfMass = shapeCenter.ToStride();

‎sources/engine/Stride.BepuPhysics/Stride.BepuPhysics/Definitions/Colliders/EmptyCollider.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ void ICollider.GetLocalTransforms(CollidableComponent collidable, Span<ShapeTran
3636
transforms[0].Scale = new Vector3(0.1f);
3737
}
3838

39-
bool ICollider.TryAttach(Shapes shapes, BufferPool pool, ShapeCacheSystem shapeCache, out TypedIndex index, out Vector3 centerOfMass, out BodyInertia inertia)
39+
bool ICollider.TryAttach(Shapes shapes, BufferPool pool, ShapeCacheSystem shapeCache, bool shouldCalculateInertia, out TypedIndex index, out Vector3 centerOfMass, out BodyInertia inertia)
4040
{
4141
index = new();
4242
centerOfMass = new();
43-
inertia = new Sphere(1).ComputeInertia(1);
43+
inertia = shouldCalculateInertia ? new Sphere(1).ComputeInertia(1) : default;
4444
return true;
4545
}
4646

‎sources/engine/Stride.BepuPhysics/Stride.BepuPhysics/Definitions/Colliders/ICollider.cs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ public interface ICollider
2323
/// You must still transform this further into worldspace by using the world position and rotation the collidable's entity.
2424
/// </remarks>
2525
public void GetLocalTransforms(CollidableComponent collidable, Span<ShapeTransform> transforms);
26-
internal bool TryAttach(Shapes shapes, BufferPool pool, ShapeCacheSystem shapeCache, out TypedIndex index, out Vector3 centerOfMass, out BodyInertia inertia);
26+
/// <remarks>
27+
/// When <paramref name="shouldCalculateInertia"/> is false the caller does not read
28+
/// <paramref name="inertia"/> - implementations skip computing it and may leave it default.
29+
/// </remarks>
30+
internal bool TryAttach(Shapes shapes, BufferPool pool, ShapeCacheSystem shapeCache, bool shouldCalculateInertia, out TypedIndex index, out Vector3 centerOfMass, out BodyInertia inertia);
2731
internal void Detach(Shapes shapes, BufferPool pool, TypedIndex index);
2832
internal void AppendModel(List<BasicMeshBuffers> buffer, ShapeCacheSystem shapeCache, out object? cache);
2933

‎sources/engine/Stride.BepuPhysics/Stride.BepuPhysics/Definitions/Colliders/MeshCollider.cs‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,19 @@ public static Vector3 ComputeMeshScale(CollidableComponent collidable)
8282
return ShapeCacheSystem.GetClosestToDecomposableScale(collidable.Entity.Transform.WorldMatrix);
8383
}
8484

85-
bool ICollider.TryAttach(Shapes shapes, BufferPool pool, ShapeCacheSystem shapeCache, out TypedIndex index, out Vector3 centerOfMass, out BodyInertia inertia)
85+
bool ICollider.TryAttach(Shapes shapes, BufferPool pool, ShapeCacheSystem shapeCache, bool shouldCalculateInertia, out TypedIndex index, out Vector3 centerOfMass, out BodyInertia inertia)
8686
{
8787
Debug.Assert(_component is not null);
8888

8989
shapeCache.GetModelCache(Model, out _cache);
9090
var mesh = _cache.GetBepuMesh(ComputeMeshScale(_component));
9191

9292
index = shapes.Add(mesh);
93-
inertia = Closed ? mesh.ComputeClosedInertia(Mass) : mesh.ComputeOpenInertia(Mass);
93+
// Computing a mesh's inertia iterates over every one of its triangles, avoid that
94+
// whenever the collidable doesn't read the result (statics)
95+
inertia = shouldCalculateInertia
96+
? Closed ? mesh.ComputeClosedInertia(Mass) : mesh.ComputeOpenInertia(Mass)
97+
: default;
9498
centerOfMass = Vector3.Zero;
9599
//if (_containerComponent is BodyMeshContainerComponent _b)
96100
//{

‎sources/engine/Stride.BepuPhysics/Stride.BepuPhysics/StaticComponent.cs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ protected internal override CollidableReference? CollidableReference
8484
}
8585
}
8686

87+
/// <inheritdoc/>
88+
/// <remarks> Statics are not affected by forces, <see cref="AttachInner"/> discards the inertia </remarks>
89+
internal override bool ShouldCalculateInertia => false;
90+
8791
protected override void AttachInner(NRigidPose pose, BodyInertia shapeInertia, TypedIndex shapeIndex)
8892
{
8993
Debug.Assert(Processor is not null);

0 commit comments

Comments
 (0)