Skip to content

Commit

Permalink
fix: avoid Reattach to run before Entity properly attaches in setup (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
dloe authored Sep 22, 2024
1 parent b6f16b8 commit 763a529
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
2 changes: 2 additions & 0 deletions sources/engine/Stride.Physics.Tests/ColliderShapesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ public void VerifyColliderShapeSetup()

var body = cube.GetOrCreate<RigidbodyComponent>();

await game.Script.NextFrame();

//verify values not properly set up
Assert.Null(body.ColliderShape);
Assert.Equal(RigidBodyTypes.Static, body.RigidBodyType);
Expand Down
8 changes: 6 additions & 2 deletions sources/engine/Stride.Physics/Elements/RigidbodyComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,12 @@ public override ColliderShape ColliderShape

if (InternalRigidBody == null)
{
//When setting ColliderShape, setup could have been previously skipped (eg when PhysicsComponent is created using GetOrCreate)
ReAttach();
if (!attachInProgress)
{
//When setting ColliderShape, setup could have been previously skipped (eg when PhysicsComponent is created using GetOrCreate)
ReAttach();
}

return;
}

Expand Down
9 changes: 9 additions & 0 deletions sources/engine/Stride.Physics/Engine/PhysicsComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,9 @@ public ChannelMicroThreadAwaiter<Collision> CollisionEnded()
[DataMemberIgnore]
protected ColliderShape colliderShape;

[DataMemberIgnore]
protected bool attachInProgress = false;

[DataMemberIgnore]
public virtual ColliderShape ColliderShape
{
Expand Down Expand Up @@ -644,16 +647,19 @@ public virtual void ComposeShape()
internal void Attach(PhysicsProcessor.AssociatedData data)
{
Data = data;
attachInProgress = true;

if (ColliderShapes.Count == 0 && ColliderShape == null)
{
logger.Error($"Entity {{Entity.Name}} has a PhysicsComponent without any collider shape.");
attachInProgress = false;
return; //no shape no purpose
}

//this is mostly required for the game studio gizmos
if (Simulation.DisableSimulation)
{
attachInProgress = false;
return;
}

Expand All @@ -666,6 +672,7 @@ internal void Attach(PhysicsProcessor.AssociatedData data)
if (ColliderShape == null)
{
logger.Error($"Entity {Entity.Name}'s PhysicsComponent failed to compose its collider shape.");
attachInProgress = false;
return; //no shape no purpose
}
}
Expand All @@ -682,6 +689,8 @@ internal void Attach(PhysicsProcessor.AssociatedData data)
}
ignoreCollisionBuffer = null;
}

attachInProgress = false;
}

/// <summary>
Expand Down

0 comments on commit 763a529

Please sign in to comment.