1- use crate :: data:: Arena ;
1+ use crate :: data:: { Arena , HasModifiedFlag , ModifiedObjects } ;
22use crate :: dynamics:: {
33 ImpulseJointSet , IslandManager , MultibodyJointSet , RigidBody , RigidBodyChanges , RigidBodyHandle ,
44} ;
@@ -22,6 +22,20 @@ impl BodyPair {
2222 }
2323}
2424
25+ pub ( crate ) type ModifiedRigidBodies = ModifiedObjects < RigidBodyHandle , RigidBody > ;
26+
27+ impl HasModifiedFlag for RigidBody {
28+ #[ inline]
29+ fn has_modified_flag ( & self ) -> bool {
30+ self . changes . contains ( RigidBodyChanges :: MODIFIED )
31+ }
32+
33+ #[ inline]
34+ fn set_modified_flag ( & mut self ) {
35+ self . changes |= RigidBodyChanges :: MODIFIED ;
36+ }
37+ }
38+
2539#[ cfg_attr( feature = "serde-serialize" , derive( Serialize , Deserialize ) ) ]
2640#[ derive( Clone , Default , Debug ) ]
2741/// A set of rigid bodies that can be handled by a physics pipeline.
@@ -31,27 +45,27 @@ pub struct RigidBodySet {
3145 // parallelism because the `Receiver` breaks the Sync impl.
3246 // Could we avoid this?
3347 pub ( crate ) bodies : Arena < RigidBody > ,
34- pub ( crate ) modified_bodies : Vec < RigidBodyHandle > ,
48+ pub ( crate ) modified_bodies : ModifiedRigidBodies ,
3549}
3650
3751impl RigidBodySet {
3852 /// Create a new empty set of rigid bodies.
3953 pub fn new ( ) -> Self {
4054 RigidBodySet {
4155 bodies : Arena :: new ( ) ,
42- modified_bodies : Vec :: new ( ) ,
56+ modified_bodies : ModifiedObjects :: default ( ) ,
4357 }
4458 }
4559
4660 /// Create a new set of rigid bodies, with an initial capacity.
4761 pub fn with_capacity ( capacity : usize ) -> Self {
4862 RigidBodySet {
4963 bodies : Arena :: with_capacity ( capacity) ,
50- modified_bodies : Vec :: with_capacity ( capacity) ,
64+ modified_bodies : ModifiedRigidBodies :: with_capacity ( capacity) ,
5165 }
5266 }
5367
54- pub ( crate ) fn take_modified ( & mut self ) -> Vec < RigidBodyHandle > {
68+ pub ( crate ) fn take_modified ( & mut self ) -> ModifiedRigidBodies {
5569 std:: mem:: take ( & mut self . modified_bodies )
5670 }
5771
@@ -79,7 +93,10 @@ impl RigidBodySet {
7993 rb. changes . set ( RigidBodyChanges :: all ( ) , true ) ;
8094
8195 let handle = RigidBodyHandle ( self . bodies . insert ( rb) ) ;
82- self . modified_bodies . push ( handle) ;
96+ // Using push_unchecked because this is a brand new rigid-body with the MODIFIED
97+ // flags set but isn’t in the modified_bodies yet.
98+ self . modified_bodies
99+ . push_unchecked ( handle, & mut self . bodies [ handle. 0 ] ) ;
83100 handle
84101 }
85102
@@ -152,7 +169,7 @@ impl RigidBodySet {
152169 pub fn get_unknown_gen_mut ( & mut self , i : u32 ) -> Option < ( & mut RigidBody , RigidBodyHandle ) > {
153170 let ( rb, handle) = self . bodies . get_unknown_gen_mut ( i) ?;
154171 let handle = RigidBodyHandle ( handle) ;
155- Self :: mark_as_modified ( handle, rb, & mut self . modified_bodies ) ;
172+ self . modified_bodies . push_once ( handle, rb) ;
156173 Some ( ( rb, handle) )
157174 }
158175
@@ -161,22 +178,11 @@ impl RigidBodySet {
161178 self . bodies . get ( handle. 0 )
162179 }
163180
164- pub ( crate ) fn mark_as_modified (
165- handle : RigidBodyHandle ,
166- rb : & mut RigidBody ,
167- modified_bodies : & mut Vec < RigidBodyHandle > ,
168- ) {
169- if !rb. changes . contains ( RigidBodyChanges :: MODIFIED ) {
170- rb. changes = RigidBodyChanges :: MODIFIED ;
171- modified_bodies. push ( handle) ;
172- }
173- }
174-
175181 /// Gets a mutable reference to the rigid-body with the given handle.
176182 #[ cfg( not( feature = "dev-remove-slow-accessors" ) ) ]
177183 pub fn get_mut ( & mut self , handle : RigidBodyHandle ) -> Option < & mut RigidBody > {
178184 let result = self . bodies . get_mut ( handle. 0 ) ?;
179- Self :: mark_as_modified ( handle, result, & mut self . modified_bodies ) ;
185+ self . modified_bodies . push_once ( handle, result) ;
180186 Some ( result)
181187 }
182188
@@ -195,7 +201,7 @@ impl RigidBodySet {
195201 handle : RigidBodyHandle ,
196202 ) -> Option < & mut RigidBody > {
197203 let result = self . bodies . get_mut ( handle. 0 ) ?;
198- Self :: mark_as_modified ( handle, result, & mut self . modified_bodies ) ;
204+ self . modified_bodies . push_once ( handle, result) ;
199205 Some ( result)
200206 }
201207
@@ -210,7 +216,9 @@ impl RigidBodySet {
210216 self . modified_bodies . clear ( ) ;
211217 let modified_bodies = & mut self . modified_bodies ;
212218 self . bodies . iter_mut ( ) . map ( move |( h, b) | {
213- modified_bodies. push ( RigidBodyHandle ( h) ) ;
219+ // NOTE: using `push_unchecked` because we just cleared `modified_bodies`
220+ // before iterating.
221+ modified_bodies. push_unchecked ( RigidBodyHandle ( h) , b) ;
214222 ( RigidBodyHandle ( h) , b)
215223 } )
216224 }
@@ -256,7 +264,7 @@ impl Index<crate::data::Index> for RigidBodySet {
256264impl IndexMut < RigidBodyHandle > for RigidBodySet {
257265 fn index_mut ( & mut self , handle : RigidBodyHandle ) -> & mut RigidBody {
258266 let rb = & mut self . bodies [ handle. 0 ] ;
259- Self :: mark_as_modified ( handle, rb, & mut self . modified_bodies ) ;
267+ self . modified_bodies . push_once ( handle, rb) ;
260268 rb
261269 }
262270}
0 commit comments