@@ -125,8 +125,8 @@ public abstract partial class SharedPhysicsSystem
125
125
*/
126
126
private const int MaxIslands = 256 ;
127
127
128
- private readonly ObjectPool < List < PhysicsComponent > > _islandBodyPool =
129
- new DefaultObjectPool < List < PhysicsComponent > > ( new ListPolicy < PhysicsComponent > ( ) , MaxIslands ) ;
128
+ private readonly ObjectPool < List < Entity < PhysicsComponent > > > _islandBodyPool =
129
+ new DefaultObjectPool < List < Entity < PhysicsComponent > > > ( new ListPolicy < Entity < PhysicsComponent > > ( ) , MaxIslands ) ;
130
130
131
131
private readonly ObjectPool < List < Contact > > _islandContactPool =
132
132
new DefaultObjectPool < List < Contact > > ( new ListPolicy < Contact > ( ) , MaxIslands ) ;
@@ -140,7 +140,7 @@ public abstract partial class SharedPhysicsSystem
140
140
internal record struct IslandData (
141
141
int Index ,
142
142
bool LoneIsland ,
143
- List < PhysicsComponent > Bodies ,
143
+ List < Entity < PhysicsComponent > > Bodies ,
144
144
List < Contact > Contacts ,
145
145
List < ( Joint Original , Joint Joint ) > Joints ,
146
146
List < ( Joint Joint , float Error ) > BrokenJoints )
@@ -161,17 +161,17 @@ internal record struct IslandData(
161
161
/// </summary>
162
162
public int Offset = 0 ;
163
163
164
- public readonly List < PhysicsComponent > Bodies = Bodies ;
164
+ public readonly List < Entity < PhysicsComponent > > Bodies = Bodies ;
165
165
public readonly List < Contact > Contacts = Contacts ;
166
166
public readonly List < ( Joint Original , Joint Joint ) > Joints = Joints ;
167
167
public bool PositionSolved = false ;
168
168
public readonly List < ( Joint Joint , float Error ) > BrokenJoints = BrokenJoints ;
169
169
}
170
170
171
171
// Caching for island generation.
172
- private readonly HashSet < PhysicsComponent > _islandSet = new ( 64 ) ;
173
- private readonly Stack < PhysicsComponent > _bodyStack = new ( 64 ) ;
174
- private readonly List < PhysicsComponent > _awakeBodyList = new ( 256 ) ;
172
+ private readonly HashSet < Entity < PhysicsComponent > > _islandSet = new ( 64 ) ;
173
+ private readonly Stack < Entity < PhysicsComponent > > _bodyStack = new ( 64 ) ;
174
+ private readonly List < Entity < PhysicsComponent > > _awakeBodyList = new ( 256 ) ;
175
175
176
176
// Config
177
177
private bool _warmStarting ;
@@ -282,7 +282,7 @@ public void Step(EntityUid uid, PhysicsMapComponent component, float frameTime,
282
282
283
283
private void ClearForces ( PhysicsMapComponent component )
284
284
{
285
- foreach ( var body in component . AwakeBodies )
285
+ foreach ( var ( _ , body ) in component . AwakeBodies )
286
286
{
287
287
// TODO: Netsync
288
288
body . Force = Vector2 . Zero ;
@@ -315,13 +315,12 @@ private void Solve(EntityUid uid, PhysicsMapComponent component, float frameTime
315
315
var islandJoints = new List < ( Joint Original , Joint Joint ) > ( ) ;
316
316
317
317
// Build the relevant islands / graphs for all bodies.
318
- foreach ( var seed in _awakeBodyList )
318
+ foreach ( var ( seedUid , seed ) in _awakeBodyList )
319
319
{
320
320
// I tried not running prediction for non-contacted entities but unfortunately it looked like shit
321
321
// when contact broke so if you want to try that then GOOD LUCK.
322
- if ( seed . Island ) continue ;
323
-
324
- var seedUid = seed . Owner ;
322
+ if ( seed . Island )
323
+ continue ;
325
324
326
325
if ( ! metaQuery . TryGetComponent ( seedUid , out var metadata ) )
327
326
{
@@ -342,19 +341,21 @@ private void Solve(EntityUid uid, PhysicsMapComponent component, float frameTime
342
341
var bodies = _islandBodyPool . Get ( ) ;
343
342
var contacts = _islandContactPool . Get ( ) ;
344
343
var joints = _islandJointPool . Get ( ) ;
345
- _bodyStack . Push ( seed ) ;
344
+ _bodyStack . Push ( ( seedUid , seed ) ) ;
346
345
347
346
seed . Island = true ;
348
347
349
- while ( _bodyStack . TryPop ( out var body ) )
348
+ while ( _bodyStack . TryPop ( out var bodyEnt ) )
350
349
{
351
- var bodyUid = body . Owner ;
350
+ var bodyUid = bodyEnt . Owner ;
351
+ var body = bodyEnt . Comp ;
352
352
353
- bodies . Add ( body ) ;
354
- _islandSet . Add ( body ) ;
353
+ bodies . Add ( bodyEnt ) ;
354
+ _islandSet . Add ( bodyEnt ) ;
355
355
356
356
// Static bodies don't propagate islands
357
- if ( body . BodyType == BodyType . Static ) continue ;
357
+ if ( body . BodyType == BodyType . Static )
358
+ continue ;
358
359
359
360
// As static bodies can never be awake (unlike Farseer) we'll set this after the check.
360
361
SetAwake ( bodyUid , body , true , updateSleepTime : false ) ;
@@ -383,9 +384,10 @@ private void Solve(EntityUid uid, PhysicsMapComponent component, float frameTime
383
384
var other = bodyA == body ? bodyB : bodyA ;
384
385
385
386
// Was the other body already added to this island?
386
- if ( other . Island ) continue ;
387
+ if ( other . Island )
388
+ continue ;
387
389
388
- _bodyStack . Push ( other ) ;
390
+ _bodyStack . Push ( bodyEnt ) ;
389
391
other . Island = true ;
390
392
}
391
393
@@ -468,13 +470,13 @@ private void Solve(EntityUid uid, PhysicsMapComponent component, float frameTime
468
470
469
471
if ( ! bodyA . Island )
470
472
{
471
- _bodyStack . Push ( bodyA ) ;
473
+ _bodyStack . Push ( ( joint . BodyAUid , bodyA ) ) ;
472
474
bodyA . Island = true ;
473
475
}
474
476
475
477
if ( ! bodyB . Island )
476
478
{
477
- _bodyStack . Push ( bodyB ) ;
479
+ _bodyStack . Push ( ( joint . BodyBUid , bodyB ) ) ;
478
480
bodyB . Island = true ;
479
481
}
480
482
}
@@ -487,7 +489,7 @@ private void Solve(EntityUid uid, PhysicsMapComponent component, float frameTime
487
489
// Bodies not touching anything, hence we can just add it to the lone island.
488
490
if ( contacts . Count == 0 && joints . Count == 0 )
489
491
{
490
- DebugTools . Assert ( bodies . Count == 1 && bodies [ 0 ] . BodyType != BodyType . Static ) ;
492
+ DebugTools . Assert ( bodies . Count == 1 && bodies [ 0 ] . Comp . BodyType != BodyType . Static ) ;
491
493
loneIsland . Bodies . Add ( bodies [ 0 ] ) ;
492
494
idx = loneIsland . Index ;
493
495
}
@@ -501,7 +503,7 @@ private void Solve(EntityUid uid, PhysicsMapComponent component, float frameTime
501
503
// Allow static bodies to be re-used in other islands
502
504
for ( var i = 0 ; i < bodies . Count ; i ++ )
503
505
{
504
- var body = bodies [ i ] ;
506
+ var body = bodies [ i ] . Comp ;
505
507
506
508
// Static bodies can participate in other islands
507
509
if ( body . BodyType == BodyType . Static )
@@ -537,8 +539,8 @@ private void ReturnIsland(IslandData island)
537
539
{
538
540
foreach ( var body in island . Bodies )
539
541
{
540
- DebugTools . Assert ( body . IslandIndex . ContainsKey ( island . Index ) ) ;
541
- body . IslandIndex . Remove ( island . Index ) ;
542
+ DebugTools . Assert ( body . Comp . IslandIndex . ContainsKey ( island . Index ) ) ;
543
+ body . Comp . IslandIndex . Remove ( island . Index ) ;
542
544
}
543
545
544
546
_islandBodyPool . Return ( island . Bodies ) ;
@@ -561,7 +563,7 @@ private void ReturnIsland(IslandData island)
561
563
562
564
protected virtual void Cleanup ( PhysicsMapComponent component , float frameTime )
563
565
{
564
- foreach ( var body in _islandSet )
566
+ foreach ( var ( _ , body ) in _islandSet )
565
567
{
566
568
if ( ! body . Island || body . Deleted )
567
569
{
@@ -679,7 +681,7 @@ private void SolveIslands(EntityUid uid, PhysicsMapComponent component, List<Isl
679
681
/// If this is the first time a body has been updated this tick update its position for lerping.
680
682
/// Due to substepping we have to check it every time.
681
683
/// </summary>
682
- protected virtual void UpdateLerpData ( PhysicsMapComponent component , List < PhysicsComponent > bodies , EntityQuery < TransformComponent > xformQuery )
684
+ protected virtual void UpdateLerpData ( PhysicsMapComponent component , List < Entity < PhysicsComponent > > bodies , EntityQuery < TransformComponent > xformQuery )
683
685
{
684
686
685
687
}
@@ -718,9 +720,9 @@ private void SolveIsland(
718
720
719
721
for ( var i = 0 ; i < island . Bodies . Count ; i ++ )
720
722
{
721
- var body = island . Bodies [ i ] ;
723
+ var ( bodyUid , body ) = island . Bodies [ i ] ;
722
724
var ( worldPos , worldRot ) =
723
- _transform . GetWorldPositionRotation ( xformQuery . GetComponent ( body . Owner ) , xformQuery ) ;
725
+ _transform . GetWorldPositionRotation ( xformQuery . GetComponent ( bodyUid ) , xformQuery ) ;
724
726
725
727
var transform = new Transform ( worldPos , worldRot ) ;
726
728
var position = Physics . Transform . Mul ( transform , body . LocalCenter ) ;
@@ -899,7 +901,7 @@ private void SolveIsland(
899
901
{
900
902
for ( var i = 0 ; i < bodyCount ; i ++ )
901
903
{
902
- var body = island . Bodies [ i ] ;
904
+ var body = island . Bodies [ i ] . Comp ;
903
905
904
906
if ( body . BodyType == BodyType . Static ) continue ;
905
907
@@ -930,9 +932,10 @@ private void SolveIsland(
930
932
931
933
for ( var i = 0 ; i < bodyCount ; i ++ )
932
934
{
933
- var body = island . Bodies [ i ] ;
935
+ var body = island . Bodies [ i ] . Comp ;
934
936
935
- if ( body . BodyType == BodyType . Static ) continue ;
937
+ if ( body . BodyType == BodyType . Static )
938
+ continue ;
936
939
937
940
if ( ! body . SleepingAllowed ||
938
941
body . AngularVelocity * body . AngularVelocity > data . AngTolSqr ||
@@ -965,16 +968,16 @@ private void SolveIsland(
965
968
ArrayPool < ContactPositionConstraint > . Shared . Return ( positionConstraints ) ;
966
969
}
967
970
968
- private void FinalisePositions ( int start , int end , int offset , List < PhysicsComponent > bodies , EntityQuery < TransformComponent > xformQuery , Vector2 [ ] positions , float [ ] angles , Vector2 [ ] solvedPositions , float [ ] solvedAngles )
971
+ private void FinalisePositions ( int start , int end , int offset , List < Entity < PhysicsComponent > > bodies , EntityQuery < TransformComponent > xformQuery , Vector2 [ ] positions , float [ ] angles , Vector2 [ ] solvedPositions , float [ ] solvedAngles )
969
972
{
970
973
for ( var i = start ; i < end ; i ++ )
971
974
{
972
- var body = bodies [ i ] ;
975
+ var ( bodyUid , body ) = bodies [ i ] ;
973
976
974
977
if ( body . BodyType == BodyType . Static )
975
978
continue ;
976
979
977
- var xform = xformQuery . GetComponent ( body . Owner ) ;
980
+ var xform = xformQuery . GetComponent ( bodyUid ) ;
978
981
var parentXform = xformQuery . GetComponent ( xform . ParentUid ) ;
979
982
var ( _, parentRot , parentInvMatrix ) = parentXform . GetWorldPositionRotationInvMatrix ( xformQuery ) ;
980
983
var worldRot = ( float ) ( parentRot + xform . _localRotation ) ;
@@ -1019,7 +1022,8 @@ private void UpdateBodies(
1019
1022
1020
1023
// So technically we don't /need/ to skip static bodies here but it saves us having to check for deferred updates so we'll do it anyway.
1021
1024
// Plus calcing worldpos can be costly so we skip that too which is nice.
1022
- if ( body . BodyType == BodyType . Static ) continue ;
1025
+ if ( body . Comp . BodyType == BodyType . Static )
1026
+ continue ;
1023
1027
1024
1028
var uid = body . Owner ;
1025
1029
var position = positions [ offset + i ] ;
@@ -1048,7 +1052,7 @@ private void UpdateBodies(
1048
1052
}
1049
1053
1050
1054
if ( physicsDirtied )
1051
- Dirty ( uid , body ) ;
1055
+ Dirty ( body ) ;
1052
1056
}
1053
1057
}
1054
1058
0 commit comments