Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Combine Fixtures into Physics #5566

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions Robust.Benchmarks/Physics/BoxStackBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,15 @@ public void BoxStack()
private void SetupTumbler(IEntityManager entManager, MapId mapId)
{
var physics = entManager.System<SharedPhysicsSystem>();
var fixtures = entManager.System<FixtureSystem>();

var groundUid = entManager.SpawnEntity(null, new MapCoordinates(0, 0, mapId));
var ground = entManager.AddComponent<PhysicsComponent>(groundUid);

var horizontal = new EdgeShape(new Vector2(-40, 0), new Vector2(40, 0));
fixtures.CreateFixture(groundUid, "fix1", new Fixture(horizontal, 2, 2, true), body: ground);
physics.CreateFixture(groundUid, "fix1", new Fixture(horizontal, 2, 2, true), body: ground);

var vertical = new EdgeShape(new Vector2(10, 0), new Vector2(10, 10));
fixtures.CreateFixture(groundUid, "fix2", new Fixture(vertical, 2, 2, true), body: ground);
physics.CreateFixture(groundUid, "fix2", new Fixture(vertical, 2, 2, true), body: ground);

var xs = new[]
{
Expand All @@ -84,7 +83,7 @@ private void SetupTumbler(IEntityManager entManager, MapId mapId)
shape = new PolygonShape();
shape.SetAsBox(0.5f, 0.5f);
physics.SetFixedRotation(boxUid, false, body: box);
fixtures.CreateFixture(boxUid, "fix1", new Fixture(shape, 2, 2, true), body: box);
physics.CreateFixture(boxUid, "fix1", new Fixture(shape, 2, 2, true), body: box);

physics.WakeBody(boxUid, body: box);
physics.SetSleepingAllowed(boxUid, box, false);
Expand Down
7 changes: 3 additions & 4 deletions Robust.Benchmarks/Physics/CircleStackBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,15 @@ public void CircleStack()
private void SetupTumbler(IEntityManager entManager, MapId mapId)
{
var physics = entManager.System<SharedPhysicsSystem>();
var fixtures = entManager.System<FixtureSystem>();

var groundUid = entManager.SpawnEntity(null, new MapCoordinates(0, 0, mapId));
var ground = entManager.AddComponent<PhysicsComponent>(groundUid);

var horizontal = new EdgeShape(new Vector2(-40, 0), new Vector2(40, 0));
fixtures.CreateFixture(groundUid, "fix1", new Fixture(horizontal, 2, 2, true), body: ground);
physics.CreateFixture(groundUid, "fix1", new Fixture(horizontal, 2, 2, true), body: ground);

var vertical = new EdgeShape(new Vector2(20, 0), new Vector2(20, 20));
fixtures.CreateFixture(groundUid, "fix2", new Fixture(vertical, 2, 2, true), body: ground);
physics.CreateFixture(groundUid, "fix2", new Fixture(vertical, 2, 2, true), body: ground);

var xs = new[]
{
Expand All @@ -81,7 +80,7 @@ private void SetupTumbler(IEntityManager entManager, MapId mapId)
physics.SetFixedRotation(boxUid, false, body: box);
// TODO: Need to detect shape and work out if we need to use fixedrotation

fixtures.CreateFixture(boxUid, "fix1", new Fixture(shape, 2, 2, true, 5f));
physics.CreateFixture(boxUid, "fix1", new Fixture(shape, 2, 2, true, 5f));
physics.WakeBody(boxUid, body: box);
physics.SetSleepingAllowed(boxUid, box, false);
}
Expand Down
5 changes: 2 additions & 3 deletions Robust.Benchmarks/Physics/PyramidBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,11 @@ private void SetupTumbler(IEntityManager entManager, MapId mapId)

// Setup ground
var physics = entManager.System<SharedPhysicsSystem>();
var fixtures = entManager.System<FixtureSystem>();
var groundUid = entManager.SpawnEntity(null, new MapCoordinates(0, 0, mapId));
var ground = entManager.AddComponent<PhysicsComponent>(groundUid);

var horizontal = new EdgeShape(new Vector2(40, 0), new Vector2(-40, 0));
fixtures.CreateFixture(groundUid, "fix1", new Fixture(horizontal, 2, 2, true), body: ground);
physics.CreateFixture(groundUid, "fix1", new Fixture(horizontal, 2, 2, true), body: ground);
physics.WakeBody(groundUid, body: ground);

// Setup boxes
Expand All @@ -78,7 +77,7 @@ private void SetupTumbler(IEntityManager entManager, MapId mapId)
var box = entManager.AddComponent<PhysicsComponent>(boxUid);
physics.SetBodyType(boxUid, BodyType.Dynamic, body: box);

fixtures.CreateFixture(boxUid, "fix1", new Fixture(shape, 2, 2, true, 5f), body: box);
physics.CreateFixture(boxUid, "fix1", new Fixture(shape, 2, 2, true, 5f), body: box);
y += deltaY;

physics.WakeBody(boxUid, body: box);
Expand Down
14 changes: 6 additions & 8 deletions Robust.Benchmarks/Physics/TumblerBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public void Setup()

var entManager = _sim.Resolve<IEntityManager>();
var physics = entManager.System<SharedPhysicsSystem>();
var fixtures = entManager.System<FixtureSystem>();
entManager.System<SharedMapSystem>().CreateMap(out var mapId);
SetupTumbler(entManager, mapId);

Expand All @@ -38,7 +37,7 @@ public void Setup()
physics.SetFixedRotation(boxUid, false, body: box);
var shape = new PolygonShape();
shape.SetAsBox(0.125f, 0.125f);
fixtures.CreateFixture(boxUid, "fix1", new Fixture(shape, 2, 2, true, 0.0625f), body: box);
physics.CreateFixture(boxUid, "fix1", new Fixture(shape, 2, 2, true, 0.0625f), body: box);
physics.WakeBody(boxUid, body: box);
physics.SetSleepingAllowed(boxUid, box, false);
}
Expand All @@ -58,14 +57,13 @@ public void Tumbler()
private void SetupTumbler(IEntityManager entManager, MapId mapId)
{
var physics = entManager.System<SharedPhysicsSystem>();
var fixtures = entManager.System<FixtureSystem>();
var joints = entManager.System<SharedJointSystem>();

var groundUid = entManager.SpawnEntity(null, new MapCoordinates(0f, 0f, mapId));
var ground = entManager.AddComponent<PhysicsComponent>(groundUid);
// Due to lookup changes fixtureless bodies are invalid, so
var cShape = new PhysShapeCircle(1f);
fixtures.CreateFixture(groundUid, "fix1", new Fixture(cShape, 0, 0, false));
physics.CreateFixture(groundUid, "fix1", new Fixture(cShape, 0, 0, false));

var bodyUid = entManager.SpawnEntity(null, new MapCoordinates(0f, 10f, mapId));
var body = entManager.AddComponent<PhysicsComponent>(bodyUid);
Expand All @@ -78,19 +76,19 @@ private void SetupTumbler(IEntityManager entManager, MapId mapId)
// TODO: Box2D just deref, bleh shape structs someday
var shape1 = new PolygonShape();
shape1.SetAsBox(0.5f, 10.0f, new Vector2(10.0f, 0.0f), 0.0f);
fixtures.CreateFixture(bodyUid, "fix1", new Fixture(shape1, 2, 0, true, 20f));
physics.CreateFixture(bodyUid, "fix1", new Fixture(shape1, 2, 0, true, 20f));

var shape2 = new PolygonShape();
shape2.SetAsBox(0.5f, 10.0f, new Vector2(-10.0f, 0.0f), 0f);
fixtures.CreateFixture(bodyUid, "fix2", new Fixture(shape2, 2, 0, true, 20f));
physics.CreateFixture(bodyUid, "fix2", new Fixture(shape2, 2, 0, true, 20f));

var shape3 = new PolygonShape();
shape3.SetAsBox(10.0f, 0.5f, new Vector2(0.0f, 10.0f), 0f);
fixtures.CreateFixture(bodyUid, "fix3", new Fixture(shape3, 2, 0, true, 20f));
physics.CreateFixture(bodyUid, "fix3", new Fixture(shape3, 2, 0, true, 20f));

var shape4 = new PolygonShape();
shape4.SetAsBox(10.0f, 0.5f, new Vector2(0.0f, -10.0f), 0f);
fixtures.CreateFixture(bodyUid, "fix4", new Fixture(shape4, 2, 0, true, 20f));
physics.CreateFixture(bodyUid, "fix4", new Fixture(shape4, 2, 0, true, 20f));

physics.WakeBody(groundUid, body: ground);
physics.WakeBody(bodyUid, body: body);
Expand Down
8 changes: 4 additions & 4 deletions Robust.Client/Debugging/DebugPhysicsSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ private void DrawWorld(DrawingHandleWorld worldHandle, OverlayDrawArgs args)

const float AlphaModifier = 0.2f;

foreach (var fixture in _entityManager.GetComponent<FixturesComponent>(physBody).Fixtures.Values)
foreach (var fixture in _entityManager.GetComponent<PhysicsComponent>(physBody).Fixtures.Values)
{
// Invalid shape - Box2D doesn't check for IsSensor but we will for sanity.
if (comp.BodyType == BodyType.Dynamic && fixture.Density == 0f && fixture.Hard)
Expand Down Expand Up @@ -315,7 +315,7 @@ private void DrawWorld(DrawingHandleWorld worldHandle, OverlayDrawArgs args)
const float AlphaModifier = 0.2f;
Box2? aabb = null;

foreach (var fixture in _entityManager.GetComponent<FixturesComponent>(physBody).Fixtures.Values)
foreach (var fixture in _entityManager.GetComponent<PhysicsComponent>(physBody).Fixtures.Values)
{
for (var i = 0; i < fixture.Shape.ChildCount; i++)
{
Expand Down Expand Up @@ -443,10 +443,10 @@ private void DrawScreen(DrawingHandleScreen screenHandle, OverlayDrawArgs args)

foreach (var ent in _lookup.GetEntitiesIntersecting(mapPos, flags))
{
if (!_entityManager.TryGetComponent<FixturesComponent>(ent, out var managerB))
if (!_entityManager.TryGetComponent<PhysicsComponent>(ent, out var managerB))
continue;

if (_physicsSystem.TryGetDistance(player.Value, ent, out var distance, managerB: managerB))
if (_physicsSystem.TryGetDistance(player.Value, ent, out var distance, bodyB: managerB))
{
screenHandle.DrawString(_font, mousePos.Position, $"Ent: {_entityManager.ToPrettyString(ent)}\nDistance: {distance:0.00}");
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Robust.Shared.Maths;
using Robust.Shared.Physics;
using Robust.Shared.Physics.Collision.Shapes;
using Robust.Shared.Physics.Components;
using Robust.Shared.Utility;

namespace Robust.Client.GameObjects
Expand Down Expand Up @@ -82,15 +83,15 @@ protected internal override void Draw(in OverlayDrawArgs args)
var viewport = args.WorldBounds;
var worldHandle = args.WorldHandle;

var fixturesQuery = _entityManager.GetEntityQuery<FixturesComponent>();
var physicsQuery = _entityManager.GetEntityQuery<PhysicsComponent>();
_grids.Clear();
_mapManager.FindGridsIntersecting(currentMap, viewport, ref _grids);
foreach (var grid in _grids)
{
var worldMatrix = _transformSystem.GetWorldMatrix(grid);
worldHandle.SetTransform(worldMatrix);
var transform = new Transform(Vector2.Zero, Angle.Zero);
var fixtures = fixturesQuery.Comp(grid.Owner);
var fixtures = physicsQuery.Comp(grid.Owner);

var chunkEnumerator = _mapSystem.GetMapChunks(grid.Owner, grid.Comp, viewport);

Expand Down
5 changes: 4 additions & 1 deletion Robust.Client/GameStates/NetGraphOverlay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ private void HandleGameStateApplied(GameStateAppliedArgs args)
if (entStates.HasContents)
{
var sb = new StringBuilder();

sb.Append($"\nSize: {args.AppliedState.PayloadSize}");

foreach (var entState in entStates.Span)
{
var uid = _entManager.GetEntity(entState.NetEntity);
Expand All @@ -102,7 +105,7 @@ private void HandleGameStateApplied(GameStateAppliedArgs args)
foreach (var compChange in entState.ComponentChanges.Span)
{
var registration = _componentFactory.GetRegistration(compChange.NetID);
sb.Append($"\n [{compChange.NetID}:{registration.Name}");
sb.Append($"\n n[{compChange.NetID}:{registration.Name}");

if (compChange.State is not null)
sb.Append($"\n STATE:{compChange.State.GetType().Name}");
Expand Down
11 changes: 6 additions & 5 deletions Robust.Server/Console/Commands/ScaleCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Robust.Shared.Maths;
using Robust.Shared.Physics;
using Robust.Shared.Physics.Collision.Shapes;
using Robust.Shared.Physics.Components;
using Robust.Shared.Physics.Systems;

namespace Robust.Server.Console.Commands;
Expand Down Expand Up @@ -58,9 +59,9 @@ public override void Execute(IConsoleShell shell, string argStr, string[] args)

appearance.SetData(uid, ScaleVisuals.Scale, oldScale * scale, appearanceComponent);

if (_entityManager.TryGetComponent(uid, out FixturesComponent? manager))
if (_entityManager.TryGetComponent(uid, out PhysicsComponent? body))
{
foreach (var (id, fixture) in manager.Fixtures)
foreach (var (id, fixture) in body.Fixtures)
{
switch (fixture.Shape)
{
Expand All @@ -70,10 +71,10 @@ public override void Execute(IConsoleShell shell, string argStr, string[] args)
edge.Vertex0 * scale,
edge.Vertex1 * scale,
edge.Vertex2 * scale,
edge.Vertex3 * scale, manager);
edge.Vertex3 * scale, body);
break;
case PhysShapeCircle circle:
physics.SetPositionRadius(uid, id, fixture, circle, circle.Position * scale, circle.Radius * scale, manager);
physics.SetPositionRadius(uid, id, fixture, circle, circle.Position * scale, circle.Radius * scale, body);
break;
case PolygonShape poly:
var verts = poly.Vertices;
Expand All @@ -83,7 +84,7 @@ public override void Execute(IConsoleShell shell, string argStr, string[] args)
verts[i] *= scale;
}

physics.SetVertices(uid, id, fixture, poly, verts, manager);
physics.SetVertices(uid, id, fixture, poly, verts, body);
break;
default:
throw new NotImplementedException();
Expand Down
2 changes: 1 addition & 1 deletion Robust.Shared/Containers/SharedContainerSystem.Insert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ private void RecursivelyUpdatePhysics(Entity<TransformComponent, PhysicsComponen
// player for any entity inside of a container during init.
_physics.SetLinearVelocity(entity, Vector2.Zero, false, body: physics);
_physics.SetAngularVelocity(entity, 0, false, body: physics);
_physics.SetCanCollide(entity, false, false, body: physics);
_physics.SetCanCollide(entity, false, body: physics);
}

foreach (var child in entity.Comp1._children)
Expand Down
19 changes: 19 additions & 0 deletions Robust.Shared/GameObjects/EntityManager.ComponentDeltas.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using Robust.Shared.Timing;

namespace Robust.Shared.GameObjects;
Expand All @@ -22,6 +23,24 @@ public uint GetModifiedFields(IComponentDelta delta, GameTick fromTick)
return fields;
}

public int GetNetworkedFieldIndex(IComponentDelta delta, string fieldName)
{
var compReg = ComponentFactory.GetRegistration(delta);

return GetNetworkedFieldIndex(compReg, fieldName);
}

public int GetNetworkedFieldIndex(ComponentRegistration compReg, string fieldName)
{
if (!compReg.NetworkedFieldLookup.TryGetValue(fieldName, out var idx))
{
throw new InvalidOperationException(
$"Tried to get networked field index for component {compReg.Type} that doesn't implement it");
}

return idx;
}

public void DirtyField(EntityUid uid, IComponentDelta comp, string fieldName, MetaDataComponent? metadata = null)
{
var compReg = ComponentFactory.GetRegistration(comp);
Expand Down
9 changes: 4 additions & 5 deletions Robust.Shared/GameObjects/Systems/CollisionWakeSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,18 @@ private void OnJointAdd(EntityUid uid, CollisionWakeComponent component, JointAd
_physics.SetCanCollide(uid, true);
}

internal void UpdateCanCollide(Entity<PhysicsComponent> entity, bool checkTerminating = true, bool dirty = true)
internal void UpdateCanCollide(Entity<PhysicsComponent> entity, bool checkTerminating = true)
{
if (_query.TryGetComponent(entity, out var wakeComp))
UpdateCanCollide(entity.Owner, wakeComp, entity.Comp, checkTerminating: checkTerminating, dirty: dirty);
UpdateCanCollide(entity.Owner, wakeComp, entity.Comp, checkTerminating: checkTerminating);
}

internal void UpdateCanCollide(
EntityUid uid,
CollisionWakeComponent component,
PhysicsComponent? body = null,
TransformComponent? xform = null,
bool checkTerminating = true,
bool dirty = true)
bool checkTerminating = true)
{
if (!component.Enabled)
return;
Expand All @@ -99,7 +98,7 @@ internal void UpdateCanCollide(
(TryComp(uid, out JointComponent? jointComponent) && jointComponent.JointCount > 0) ||
xform.GridUid == null;

_physics.SetCanCollide(uid, canCollide, dirty, body: body);
_physics.SetCanCollide(uid, canCollide, body: body);
}
}
}
Loading
Loading