File tree Expand file tree Collapse file tree 2 files changed +31
-4
lines changed
packages/dev/core/src/Physics/v2 Expand file tree Collapse file tree 2 files changed +31
-4
lines changed Original file line number Diff line number Diff line change @@ -94,6 +94,11 @@ export interface PhysicsAggregateParameters {
9494 * Extents for box
9595 */
9696 extents ?: Vector3 ;
97+
98+ /**
99+ * mesh local center
100+ */
101+ center ?: Vector3 ;
97102}
98103/**
99104 * Helper class to create and interact with a PhysicsAggregate.
@@ -155,6 +160,7 @@ export class PhysicsAggregate {
155160
156161 this . body = new PhysicsBody ( transformNode , this . _scene ) ;
157162 this . _addSizeOptions ( ) ;
163+ this . _options . center = _options . center ?? this . body . getObjectCenterDelta ( ) ;
158164 this . shape = new PhysicsShape ( { type, parameters : this . _options as any } , this . _scene ) ;
159165
160166 this . material = new PhysicsMaterial ( this . _options . friction , this . _options . restitution , this . _scene ) ;
@@ -190,13 +196,11 @@ export class PhysicsAggregate {
190196 this . _options . pointB = this . _options . pointB ? this . _options . pointB : new Vector3 ( 0 , impostorExtents . y * 0.5 , 0 ) ;
191197 }
192198 break ;
199+ case ShapeType . MESH :
200+ case ShapeType . CONVEX_HULL :
193201 case ShapeType . BOX :
194202 this . _options . extents = this . _options . extents ? this . _options . extents : new Vector3 ( impostorExtents . x , impostorExtents . y , impostorExtents . z ) ;
195203 break ;
196- case ShapeType . MESH :
197- case ShapeType . CONVEX_HULL : {
198- break ;
199- }
200204 }
201205 }
202206
Original file line number Diff line number Diff line change @@ -372,6 +372,7 @@ export class PhysicsBody {
372372 if ( worldMatrix ) {
373373 worldMatrix . decompose ( scaling , undefined , undefined ) ;
374374 }
375+ tmAbstractMesh . refreshBoundingInfo ( ) ;
375376 const boundingInfo = tmAbstractMesh . getBoundingInfo ( ) ;
376377 // get the global scaling of the object
377378 const size = boundingInfo . boundingBox . extendSize . scale ( 2 ) . multiplyInPlace ( scaling ) ;
@@ -388,6 +389,28 @@ export class PhysicsBody {
388389 }
389390 }
390391
392+ /**
393+ * returns the delta between the object bounding box center and the mesh origin
394+ * @returns delta between object bounding box center and origin
395+ */
396+ public getObjectCenterDelta ( ) : Vector3 {
397+ const tmAbstractMesh = this . transformNode as AbstractMesh ;
398+ if ( tmAbstractMesh . getBoundingInfo ) {
399+ const delta = new Vector3 ( ) ;
400+ const boundingInfo = tmAbstractMesh . getBoundingInfo ( ) ;
401+ this . transformNode . computeWorldMatrix ( true ) ;
402+ tmAbstractMesh . refreshBoundingInfo ( ) ;
403+ delta . copyFrom ( boundingInfo . boundingBox . centerWorld ) ;
404+ delta . subtractInPlace ( tmAbstractMesh . getAbsolutePosition ( ) ) ;
405+ delta . x /= tmAbstractMesh . scaling . x ;
406+ delta . y /= tmAbstractMesh . scaling . y ;
407+ delta . z /= tmAbstractMesh . scaling . z ;
408+ return delta ;
409+ } else {
410+ return Vector3 . Zero ( ) ;
411+ }
412+ }
413+
391414 /**
392415 * @returns geometric center of the associated mesh
393416 */
You can’t perform that action at this time.
0 commit comments