@@ -29,8 +29,10 @@ public class JoltPhysics3D : IPhysics3D
2929 private JobSystem jobSystem ;
3030
3131 //Tracking live bodies
32- private readonly List < JoltBodyPair > bodies = [ ] ;
33- private readonly List < JoltCharacterPair > characters = [ ] ;
32+ private readonly Dictionary < BodyID , JoltBodyPair > bodies = [ ] ;
33+ private readonly Dictionary < Entity , JoltBodyPair > entityBodies = [ ] ;
34+ private readonly Dictionary < BodyID , JoltCharacterPair > characters = [ ] ;
35+ private readonly Dictionary < Entity , JoltCharacterPair > entityCharacters = [ ] ;
3436 private readonly Lock threadLock = new ( ) ;
3537 private readonly CallbackGatherer callbackGatherer = new ( ) ;
3638
@@ -162,14 +164,11 @@ private bool TryFindBody(Body body, out IBody3D outBody)
162164 {
163165 lock ( threadLock )
164166 {
165- foreach ( var b in bodies )
167+ if ( bodies . TryGetValue ( body . ID , out var b ) )
166168 {
167- if ( b . body == body )
168- {
169- outBody = b ;
169+ outBody = b ;
170170
171- return true ;
172- }
171+ return true ;
173172 }
174173 }
175174
@@ -182,14 +181,11 @@ private bool TryFindBody(Character character, out IBody3D outBody)
182181 {
183182 lock ( threadLock )
184183 {
185- foreach ( var b in characters )
184+ if ( characters . TryGetValue ( character . BodyID , out var b ) )
186185 {
187- if ( b . character == character )
188- {
189- outBody = b ;
186+ outBody = b ;
190187
191- return true ;
192- }
188+ return true ;
193189 }
194190 }
195191
@@ -202,24 +198,18 @@ private bool TryFindBody(BodyID body, out IBody3D outBody)
202198 {
203199 lock ( threadLock )
204200 {
205- foreach ( var b in bodies )
201+ if ( bodies . TryGetValue ( body , out var b ) )
206202 {
207- if ( b . body . ID == body )
208- {
209- outBody = b ;
203+ outBody = b ;
210204
211- return true ;
212- }
205+ return true ;
213206 }
214207
215- foreach ( var b in characters )
208+ if ( characters . TryGetValue ( body , out var c ) )
216209 {
217- if ( b . character . BodyID == body )
218- {
219- outBody = b ;
210+ outBody = c ;
220211
221- return true ;
222- }
212+ return true ;
223213 }
224214 }
225215
@@ -391,18 +381,20 @@ public void Update(float deltaTime)
391381 {
392382 lock ( threadLock )
393383 {
394- if ( pair . body . IsActive == false )
384+ var p = pair . Value ;
385+
386+ if ( p . body . IsActive == false )
395387 {
396388 continue ;
397389 }
398390
399- pair . interpolatedPosition = Vector3 . Lerp ( pair . previousPosition , pair . currentPosition , Math . Clamp01 ( alpha ) ) ;
400- pair . interpolatedRotation = Quaternion . Slerp ( pair . previousRotation , pair . currentRotation , Math . Clamp01 ( alpha ) ) ;
391+ p . interpolatedPosition = Vector3 . Lerp ( p . previousPosition , p . currentPosition , Math . Clamp01 ( alpha ) ) ;
392+ p . interpolatedRotation = Quaternion . Slerp ( p . previousRotation , p . currentRotation , Math . Clamp01 ( alpha ) ) ;
401393
402- if ( pair . transform != null )
394+ if ( p . transform != null )
403395 {
404- pair . transform . Position = pair . interpolatedPosition ;
405- pair . transform . Rotation = pair . interpolatedRotation ;
396+ p . transform . Position = p . interpolatedPosition ;
397+ p . transform . Rotation = p . interpolatedRotation ;
406398 }
407399 }
408400 }
@@ -411,18 +403,20 @@ public void Update(float deltaTime)
411403 {
412404 lock ( threadLock )
413405 {
414- if ( pair . enabled == false )
406+ var p = pair . Value ;
407+
408+ if ( p . enabled == false )
415409 {
416410 continue ;
417411 }
418412
419- pair . interpolatedPosition = Vector3 . Lerp ( pair . previousPosition , pair . currentPosition , Math . Clamp01 ( alpha ) ) ;
420- pair . interpolatedRotation = Quaternion . Slerp ( pair . previousRotation , pair . currentRotation , Math . Clamp01 ( alpha ) ) ;
413+ p . interpolatedPosition = Vector3 . Lerp ( p . previousPosition , p . currentPosition , Math . Clamp01 ( alpha ) ) ;
414+ p . interpolatedRotation = Quaternion . Slerp ( p . previousRotation , p . currentRotation , Math . Clamp01 ( alpha ) ) ;
421415
422- if ( pair . transform != null )
416+ if ( p . transform != null )
423417 {
424- pair . transform . Position = pair . interpolatedPosition ;
425- pair . transform . Rotation = pair . interpolatedRotation ;
418+ p . transform . Position = p . interpolatedPosition ;
419+ p . transform . Rotation = p . interpolatedRotation ;
426420 }
427421 }
428422 }
@@ -437,47 +431,51 @@ private void Simulate()
437431 {
438432 foreach ( var pair in bodies )
439433 {
434+ var p = pair . Value ;
435+
440436 if ( Physics . InterpolatePhysics )
441437 {
442- pair . previousPosition = pair . currentPosition ;
443- pair . previousRotation = pair . currentRotation ;
438+ p . previousPosition = p . currentPosition ;
439+ p . previousRotation = p . currentRotation ;
444440 }
445441
446- if ( pair . entity . EnabledInHierarchy == false )
442+ if ( p . entity . EnabledInHierarchy == false )
447443 {
448- if ( pair . body . IsActive )
444+ if ( p . body . IsActive )
449445 {
450- physicsSystem . BodyInterface . DeactivateBody ( pair . body . ID ) ;
446+ physicsSystem . BodyInterface . DeactivateBody ( p . body . ID ) ;
451447 }
452448 }
453- else if ( pair . body . IsActive == false )
449+ else if ( p . body . IsActive == false )
454450 {
455- physicsSystem . BodyInterface . ActivateBody ( pair . body . ID ) ;
451+ physicsSystem . BodyInterface . ActivateBody ( p . body . ID ) ;
456452 }
457453 }
458454
459455 foreach ( var pair in characters )
460456 {
457+ var p = pair . Value ;
458+
461459 if ( Physics . InterpolatePhysics )
462460 {
463- pair . previousPosition = pair . currentPosition ;
464- pair . previousRotation = pair . currentRotation ;
461+ p . previousPosition = p . currentPosition ;
462+ p . previousRotation = p . currentRotation ;
465463 }
466464
467- if ( pair . entity . EnabledInHierarchy == false )
465+ if ( p . entity . EnabledInHierarchy == false )
468466 {
469- if ( pair . enabled )
467+ if ( p . enabled )
470468 {
471- pair . enabled = false ;
469+ p . enabled = false ;
472470
473- pair . character . RemoveFromPhysicsSystem ( ) ;
471+ p . character . RemoveFromPhysicsSystem ( ) ;
474472 }
475473 }
476- else if ( pair . enabled == false )
474+ else if ( p . enabled == false )
477475 {
478- pair . enabled = true ;
476+ p . enabled = true ;
479477
480- pair . character . AddToPhysicsSystem ( ) ;
478+ p . character . AddToPhysicsSystem ( ) ;
481479 }
482480 }
483481 }
@@ -490,37 +488,41 @@ private void Simulate()
490488 {
491489 foreach ( var pair in bodies )
492490 {
493- if ( pair . body . IsActive == false )
491+ var p = pair . Value ;
492+
493+ if ( p . body . IsActive == false )
494494 {
495495 continue ;
496496 }
497497
498- pair . currentPosition = pair . body . Position ;
499- pair . currentRotation = pair . body . Rotation ;
498+ p . currentPosition = p . body . Position ;
499+ p . currentRotation = p . body . Rotation ;
500500
501- if ( Physics . InterpolatePhysics == false && pair . transform != null )
501+ if ( Physics . InterpolatePhysics == false && p . transform != null )
502502 {
503- pair . transform . Position = pair . currentPosition ;
504- pair . transform . Rotation = pair . currentRotation ;
503+ p . transform . Position = p . currentPosition ;
504+ p . transform . Rotation = p . currentRotation ;
505505 }
506506 }
507507
508508 foreach ( var pair in characters )
509509 {
510- if ( pair . enabled == false )
510+ var p = pair . Value ;
511+
512+ if ( p . enabled == false )
511513 {
512514 continue ;
513515 }
514516
515- ( pair . currentPosition , pair . currentRotation ) = pair . character . GetPositionAndRotation ( ) ;
517+ ( p . currentPosition , p . currentRotation ) = p . character . GetPositionAndRotation ( ) ;
516518
517- if ( Physics . InterpolatePhysics == false && pair . transform != null )
519+ if ( Physics . InterpolatePhysics == false && p . transform != null )
518520 {
519- pair . transform . Position = pair . currentPosition ;
520- pair . transform . Rotation = pair . currentRotation ;
521+ p . transform . Position = p . currentPosition ;
522+ p . transform . Rotation = p . currentRotation ;
521523 }
522524
523- pair . character . PostSimulation ( 0.05f ) ;
525+ p . character . PostSimulation ( 0.05f ) ;
524526 }
525527 }
526528 }
@@ -559,7 +561,9 @@ private bool CreateCharacter(Entity entity, Vector3 position, Quaternion rotatio
559561 previousRotation = rotation . SafeNormalize ( ) ,
560562 } ;
561563
562- characters . Add ( pair ) ;
564+ characters . Add ( character . BodyID , pair ) ;
565+
566+ entityCharacters . Add ( entity , pair ) ;
563567
564568 body = pair ;
565569
@@ -649,7 +653,8 @@ private bool CreateBody(Entity entity, ShapeSettings settings, Vector3 position,
649653 previousRotation = rotation . SafeNormalize ( ) ,
650654 } ;
651655
652- bodies . Add ( pair ) ;
656+ bodies . Add ( b . ID , pair ) ;
657+ entityBodies . Add ( entity , pair ) ;
653658
654659 body = pair ;
655660
@@ -1050,7 +1055,8 @@ public void DestroyBody(IBody3D body)
10501055 physicsSystem . BodyInterface . DestroyBody ( pair . body . ID ) ;
10511056 }
10521057
1053- bodies . Remove ( pair ) ;
1058+ bodies . Remove ( pair . body . ID ) ;
1059+ entityBodies . Remove ( pair . entity ) ;
10541060 }
10551061 }
10561062 else if ( body is JoltCharacterPair characterPair )
@@ -1070,7 +1076,8 @@ public void DestroyBody(IBody3D body)
10701076 physicsSystem . BodyInterface . DestroyBody ( characterPair . character . BodyID ) ;
10711077 }
10721078
1073- characters . Remove ( characterPair ) ;
1079+ characters . Remove ( characterPair . character . BodyID ) ;
1080+ entityCharacters . Remove ( characterPair . entity ) ;
10741081 }
10751082 }
10761083 }
@@ -1313,20 +1320,14 @@ public IBody3D GetBody(Entity entity)
13131320 {
13141321 lock ( threadLock )
13151322 {
1316- foreach ( var body in bodies )
1323+ if ( entityBodies . TryGetValue ( entity , out var body ) )
13171324 {
1318- if ( body is JoltBodyPair pair && pair . entity == entity )
1319- {
1320- return body ;
1321- }
1325+ return body ;
13221326 }
13231327
1324- foreach ( var body in characters )
1328+ if ( entityCharacters . TryGetValue ( entity , out var character ) )
13251329 {
1326- if ( body is JoltCharacterPair pair && pair . entity == entity )
1327- {
1328- return body ;
1329- }
1330+ return character ;
13301331 }
13311332 }
13321333
@@ -1337,20 +1338,14 @@ public IBody3D GetBody(BodyID bodyID)
13371338 {
13381339 lock ( threadLock )
13391340 {
1340- foreach ( var body in bodies )
1341+ if ( bodies . TryGetValue ( bodyID , out var body ) )
13411342 {
1342- if ( body is JoltBodyPair pair && pair . body . ID == bodyID )
1343- {
1344- return body ;
1345- }
1343+ return body ;
13461344 }
13471345
1348- foreach ( var body in characters )
1346+ if ( characters . TryGetValue ( bodyID , out var character ) )
13491347 {
1350- if ( body is JoltCharacterPair pair && pair . character . BodyID == bodyID )
1351- {
1352- return body ;
1353- }
1348+ return character ;
13541349 }
13551350 }
13561351
@@ -1401,6 +1396,8 @@ public void DestroyAllBodies()
14011396
14021397 bodies . Clear ( ) ;
14031398 characters . Clear ( ) ;
1399+ entityBodies . Clear ( ) ;
1400+ entityCharacters . Clear ( ) ;
14041401 }
14051402 }
14061403
0 commit comments