Defer physics transforms to ensure thread safety
Refactored multiple components to defer Farseer physics transform operations using PhysicsBodyQueue, preventing unsafe calls from parallel contexts. This change addresses thread safety issues with Farseer's DynamicTree and ensures transforms are executed in a safe context. Also increased the spawn amount limit in DebugConsole from 100 to 100000.
This commit is contained in:
@@ -314,13 +314,21 @@ namespace Barotrauma.Items.Components
|
||||
|
||||
public override void Move(Vector2 amount, bool ignoreContacts = false)
|
||||
{
|
||||
if (ignoreContacts)
|
||||
// Defer physics operation if in parallel context (Farseer is not thread-safe)
|
||||
if (Body != null)
|
||||
{
|
||||
Body?.SetTransformIgnoreContacts(Body.SimPosition + ConvertUnits.ToSimUnits(amount), 0.0f);
|
||||
}
|
||||
else
|
||||
{
|
||||
Body?.SetTransform(Body.SimPosition + ConvertUnits.ToSimUnits(amount), 0.0f);
|
||||
var capturedBody = Body;
|
||||
var capturedNewPos = Body.SimPosition + ConvertUnits.ToSimUnits(amount);
|
||||
if (ignoreContacts)
|
||||
{
|
||||
PhysicsBodyQueue.ExecuteOrDefer(() =>
|
||||
capturedBody.SetTransformIgnoreContacts(capturedNewPos, 0.0f));
|
||||
}
|
||||
else
|
||||
{
|
||||
PhysicsBodyQueue.ExecuteOrDefer(() =>
|
||||
capturedBody.SetTransform(capturedNewPos, 0.0f));
|
||||
}
|
||||
}
|
||||
#if CLIENT
|
||||
UpdateConvexHulls();
|
||||
@@ -786,13 +794,19 @@ namespace Barotrauma.Items.Components
|
||||
//immediately teleport it to the correct side
|
||||
if (Math.Sign(diff) != dir)
|
||||
{
|
||||
// Defer physics operation if in parallel context (Farseer is not thread-safe)
|
||||
var capturedBody = body;
|
||||
if (IsHorizontal)
|
||||
{
|
||||
body.SetTransformIgnoreContacts(new Vector2(body.SimPosition.X, item.SimPosition.Y + dir * doorRectSimSize.Y * 2.0f), body.Rotation);
|
||||
Vector2 newPos = new Vector2(body.SimPosition.X, item.SimPosition.Y + dir * doorRectSimSize.Y * 2.0f);
|
||||
float rotation = body.Rotation;
|
||||
PhysicsBodyQueue.ExecuteOrDefer(() => capturedBody.SetTransformIgnoreContacts(newPos, rotation));
|
||||
}
|
||||
else
|
||||
{
|
||||
body.SetTransformIgnoreContacts(new Vector2(item.SimPosition.X + dir * doorRectSimSize.X * 1.2f, body.SimPosition.Y), body.Rotation);
|
||||
Vector2 newPos = new Vector2(item.SimPosition.X + dir * doorRectSimSize.X * 1.2f, body.SimPosition.Y);
|
||||
float rotation = body.Rotation;
|
||||
PhysicsBodyQueue.ExecuteOrDefer(() => capturedBody.SetTransformIgnoreContacts(newPos, rotation));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -92,13 +92,16 @@ namespace Barotrauma.Items.Components
|
||||
{
|
||||
if (trigger != null && amount.LengthSquared() > 0.00001f)
|
||||
{
|
||||
// Defer physics operation if in parallel context (Farseer is not thread-safe)
|
||||
var capturedTrigger = trigger;
|
||||
var capturedPos = item.SimPosition;
|
||||
if (ignoreContacts)
|
||||
{
|
||||
trigger.SetTransformIgnoreContacts(item.SimPosition, 0.0f);
|
||||
PhysicsBodyQueue.ExecuteOrDefer(() => capturedTrigger.SetTransformIgnoreContacts(capturedPos, 0.0f));
|
||||
}
|
||||
else
|
||||
{
|
||||
trigger.SetTransform(item.SimPosition, 0.0f);
|
||||
PhysicsBodyQueue.ExecuteOrDefer(() => capturedTrigger.SetTransform(capturedPos, 0.0f));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -134,7 +137,10 @@ namespace Barotrauma.Items.Components
|
||||
}
|
||||
if (trigger != null && Vector2.DistanceSquared(item.SimPosition, trigger.SimPosition) > 0.01f)
|
||||
{
|
||||
trigger.SetTransform(item.SimPosition, 0.0f);
|
||||
// Defer physics operation if in parallel context (Farseer is not thread-safe)
|
||||
var capturedTrigger = trigger;
|
||||
var capturedPos = item.SimPosition;
|
||||
PhysicsBodyQueue.ExecuteOrDefer(() => capturedTrigger.SetTransform(capturedPos, 0.0f));
|
||||
}
|
||||
IsActive = false;
|
||||
}
|
||||
|
||||
@@ -1011,7 +1011,11 @@ namespace Barotrauma.Items.Components
|
||||
if (flippedX ^ flippedY) { rotation = -rotation; }
|
||||
rotation += -item.RotationRad;
|
||||
}
|
||||
contained.Item.body.FarseerBody.SetTransformIgnoreContacts(ref simPos, rotation);
|
||||
// Defer physics operation if in parallel context (Farseer is not thread-safe)
|
||||
var capturedBody = contained.Item.body.FarseerBody;
|
||||
var capturedSimPos = simPos;
|
||||
var capturedRotation = rotation;
|
||||
PhysicsBodyQueue.ExecuteOrDefer(() => capturedBody.SetTransformIgnoreContacts(ref capturedSimPos, capturedRotation));
|
||||
contained.Item.body.UpdateDrawPosition(interpolate: false);
|
||||
}
|
||||
catch (Exception e)
|
||||
|
||||
@@ -312,13 +312,18 @@ namespace Barotrauma.Items.Components
|
||||
Matrix transform = Matrix.CreateRotationZ(-item.RotationRad);
|
||||
offset = Vector2.Transform(offset, transform);
|
||||
}
|
||||
|
||||
// Defer physics operations if in parallel context (Farseer is not thread-safe)
|
||||
var capturedBody = PhysicsBody;
|
||||
var capturedPos = item.SimPosition + offset;
|
||||
var capturedRot = -item.RotationRad;
|
||||
if (ignoreContacts)
|
||||
{
|
||||
PhysicsBody.SetTransformIgnoreContacts(item.SimPosition + offset, -item.RotationRad);
|
||||
PhysicsBodyQueue.ExecuteOrDefer(() => capturedBody.SetTransformIgnoreContacts(capturedPos, capturedRot));
|
||||
}
|
||||
else
|
||||
{
|
||||
PhysicsBody.SetTransform(item.SimPosition + offset, -item.RotationRad);
|
||||
PhysicsBodyQueue.ExecuteOrDefer(() => capturedBody.SetTransform(capturedPos, capturedRot));
|
||||
}
|
||||
PhysicsBody.UpdateDrawPosition();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user