|
@@ -3,7 +3,7 @@ Namespace mojo3d.physics
|
|
|
|
|
|
#Import "native/kinematicmotionstate.h"
|
|
|
|
|
|
-Extern
|
|
|
+Extern Private
|
|
|
|
|
|
Class bbKinematicMotionState Extends btMotionState
|
|
|
|
|
@@ -11,7 +11,7 @@ Class bbKinematicMotionState Extends btMotionState
|
|
|
|
|
|
End
|
|
|
|
|
|
-Public
|
|
|
+Private
|
|
|
|
|
|
Class KinematicMotionState Extends bbKinematicMotionState
|
|
|
|
|
@@ -30,309 +30,189 @@ Private
|
|
|
Field _entity:Entity
|
|
|
End
|
|
|
|
|
|
-Class Entity Extension
|
|
|
-
|
|
|
- Property RigidBody:RigidBody()
|
|
|
-
|
|
|
- Return GetDynamicProperty<RigidBody>( "$rigidBody" )
|
|
|
- End
|
|
|
-
|
|
|
-End
|
|
|
+Public
|
|
|
|
|
|
-#rem monkeydoc The RigidBody class.
|
|
|
-#end
|
|
|
-Class RigidBody
|
|
|
+Class RigidBody Extends Component
|
|
|
|
|
|
- Method Init( collider:Collider,entity:Entity,mass:Float,collGroup:Int,collMask:Int,btmotion:btMotionState )
|
|
|
+ Const Type:=New ComponentType( "RigidBody",1,ComponentTypeFlags.Singleton )
|
|
|
|
|
|
- Local inertia:btVector3=collider ? collider.CalculateLocalInertia( mass ) Else Null
|
|
|
-
|
|
|
- _collider=collider
|
|
|
- _entity=entity
|
|
|
- _mass=mass
|
|
|
- _collGroup=collGroup
|
|
|
- _collMask=collMask
|
|
|
- _btmotion=btmotion
|
|
|
-
|
|
|
- _btbody=New btRigidBody( mass,btmotion,collider.btShape,inertia )
|
|
|
-
|
|
|
- _btbody.setFriction( 1 )
|
|
|
- _btbody.setRollingFriction( 1 )
|
|
|
- _btbody.setRestitution( 0 )
|
|
|
-
|
|
|
- 'If Cast<MeshCollider>( _collider ) _btbody.setCollisionFlags( _btbody.getCollisionFlags() | btCollisionObject.CF_CUSTOM_MATERIAL_CALLBACK )
|
|
|
-
|
|
|
- If Not _entity Return
|
|
|
-
|
|
|
- _entity.SetDynamicProperty( "$rigidBody",Self )
|
|
|
-
|
|
|
- Local world:=entity.Scene.World
|
|
|
-
|
|
|
- _entity.Shown+=Lambda()
|
|
|
-
|
|
|
- world.Add( Self )
|
|
|
- End
|
|
|
-
|
|
|
- _entity.Hidden+=Lambda()
|
|
|
-
|
|
|
- world.Remove( Self )
|
|
|
- End
|
|
|
+ Method New( entity:Entity )
|
|
|
|
|
|
- If _entity.Visible world.Add( Self )
|
|
|
+ Super.New( entity,Type )
|
|
|
End
|
|
|
-
|
|
|
- Property Collider:Collider()
|
|
|
+
|
|
|
+ Property World:World()
|
|
|
|
|
|
- Return _collider
|
|
|
+ Return World.GetWorld( Entity.Scene )
|
|
|
End
|
|
|
|
|
|
- Property Entity:Entity()
|
|
|
-
|
|
|
- Return _entity
|
|
|
- End
|
|
|
+ Property Kinematic:Bool()
|
|
|
+
|
|
|
+ Return _kinematic
|
|
|
|
|
|
- Property CollisionGroup:Short()
|
|
|
+ Setter( kinematic:Bool )
|
|
|
|
|
|
- Return _collGroup
|
|
|
+ _kinematic=kinematic
|
|
|
End
|
|
|
|
|
|
- Property CollisionMask:Short()
|
|
|
+ Property Mass:Float()
|
|
|
|
|
|
- Return _collMask
|
|
|
+ Return _mass
|
|
|
+
|
|
|
+ Setter( mass:Float )
|
|
|
+
|
|
|
+ _mass=mass
|
|
|
End
|
|
|
-
|
|
|
+
|
|
|
Property Friction:Float()
|
|
|
-
|
|
|
- Return _btbody.getFriction()
|
|
|
+
|
|
|
+ Return _friction
|
|
|
|
|
|
Setter( friction:Float )
|
|
|
-
|
|
|
- _btbody.setFriction( friction )
|
|
|
+
|
|
|
+ _friction=friction
|
|
|
End
|
|
|
|
|
|
Property RollingFriction:Float()
|
|
|
-
|
|
|
- Return _btbody.getRollingFriction()
|
|
|
+
|
|
|
+ Return _rfriction
|
|
|
|
|
|
Setter( friction:Float )
|
|
|
-
|
|
|
- _btbody.setRollingFriction( friction )
|
|
|
+
|
|
|
+ _rfriction=friction
|
|
|
End
|
|
|
|
|
|
Property Restitution:Float()
|
|
|
|
|
|
- Return _btbody.getRestitution()
|
|
|
+ Return _restitution
|
|
|
|
|
|
Setter( restitution:Float )
|
|
|
-
|
|
|
- _btbody.setRestitution( restitution )
|
|
|
- End
|
|
|
-
|
|
|
- Property btBody:btRigidBody()
|
|
|
-
|
|
|
- Return _btbody
|
|
|
+
|
|
|
+ _restitution=restitution
|
|
|
End
|
|
|
-
|
|
|
-Protected
|
|
|
|
|
|
- Field _mass:Float
|
|
|
- Field _collider:Collider
|
|
|
- Field _entity:Entity
|
|
|
- Field _collGroup:Int
|
|
|
- Field _collMask:Int
|
|
|
-
|
|
|
- Field _btmotion:btMotionState
|
|
|
- Field _btbody:btRigidBody
|
|
|
- Field _seq:Int
|
|
|
-
|
|
|
- Method OnValidate() Virtual
|
|
|
- End
|
|
|
-
|
|
|
- Method OnUpdate() Virtual
|
|
|
- End
|
|
|
-
|
|
|
-Internal
|
|
|
+ Property CollisionGroup:Short()
|
|
|
|
|
|
- Method Validate()
|
|
|
+ Return _collGroup
|
|
|
|
|
|
- If _entity OnValidate()
|
|
|
- End
|
|
|
-
|
|
|
- Method Update()
|
|
|
+ Setter( collGroup:Short )
|
|
|
|
|
|
- If _entity OnUpdate()
|
|
|
+ _collGroup=collGroup
|
|
|
End
|
|
|
|
|
|
-End
|
|
|
-
|
|
|
-Class StaticBody Extends RigidBody
|
|
|
-
|
|
|
- Method New( collider:Collider,entity:Entity,collGroup:Int=1,collMask:Int=1 )
|
|
|
-
|
|
|
- Init( collider,entity,0,collGroup,collMask,Null )
|
|
|
+ Property CollisionMask:Short()
|
|
|
|
|
|
- If entity entity.Copied+=Lambda( copy:Entity )
|
|
|
+ Return _collMask
|
|
|
|
|
|
- Local body:=New StaticBody( collider,copy,collGroup,collMask )
|
|
|
-
|
|
|
- body.Friction=Friction
|
|
|
- body.RollingFriction=RollingFriction
|
|
|
- body.Restitution=Restitution
|
|
|
- End
|
|
|
+ Setter( collMask:Short )
|
|
|
|
|
|
+ _collMask=collMask
|
|
|
End
|
|
|
|
|
|
-Protected
|
|
|
+ Property btBody:btRigidBody()
|
|
|
|
|
|
- Method OnValidate() Override
|
|
|
-
|
|
|
- If _seq=_entity.Seq Return
|
|
|
-
|
|
|
- _btbody.setWorldTransform( _entity.Matrix )
|
|
|
+ Return _btbody
|
|
|
End
|
|
|
|
|
|
- Method OnUpdate() Override
|
|
|
+ Method OnCopy:RigidBody( entity:Entity ) Override
|
|
|
|
|
|
- _seq=_entity.Seq
|
|
|
- End
|
|
|
-
|
|
|
-End
|
|
|
-
|
|
|
-Class KinematicBody Extends RigidBody
|
|
|
-
|
|
|
- Method New( collider:Collider,entity:Entity,collGroup:Int=1,collMask:Int=1 )
|
|
|
-
|
|
|
- Init( collider,entity,0,collGroup,collMask,entity ? New KinematicMotionState( entity ) Else Null )
|
|
|
-
|
|
|
- _btbody.setCollisionFlags( _btbody.getCollisionFlags() | btCollisionObject.CF_KINEMATIC_OBJECT )
|
|
|
- _btbody.setActivationState( DISABLE_DEACTIVATION )
|
|
|
+ Local body:=New RigidBody( entity )
|
|
|
|
|
|
- If entity entity.Copied+=Lambda( copy:Entity )
|
|
|
+ body.Kinematic=Kinematic
|
|
|
+ body.Mass=Mass
|
|
|
+ body.Friction=Friction
|
|
|
+ body.RollingFriction=RollingFriction
|
|
|
+ body.Restitution=Restitution
|
|
|
+ body.CollisionGroup=CollisionGroup
|
|
|
+ body.CollisionMask=CollisionMask
|
|
|
|
|
|
- Local body:=New KinematicBody( collider,copy,collGroup,collMask )
|
|
|
-
|
|
|
- body.Friction=Friction
|
|
|
- body.RollingFriction=RollingFriction
|
|
|
- body.Restitution=Restitution
|
|
|
- End
|
|
|
-
|
|
|
- End
|
|
|
-
|
|
|
-Protected
|
|
|
-
|
|
|
- Method OnValidate() Override
|
|
|
+ Return body
|
|
|
End
|
|
|
-
|
|
|
- Method OnUpdate() Override
|
|
|
- End
|
|
|
-
|
|
|
-End
|
|
|
-
|
|
|
-Class DynamicBody Extends RigidBody
|
|
|
|
|
|
- Method New( collider:Collider,entity:Entity,mass:Float=1,collGroup:Int=1,collMask:Int=1 )
|
|
|
-
|
|
|
- Init( collider,entity,mass,collGroup,collMask,entity ? New btDefaultMotionState( entity.Matrix ) Else null )
|
|
|
-
|
|
|
- If entity entity.Copied+=Lambda( copy:Entity )
|
|
|
+ Method OnBeginUpdate() Override
|
|
|
|
|
|
- Local body:=New DynamicBody( collider,copy,mass,collGroup,collMask )
|
|
|
-
|
|
|
- body.Gravity=Gravity
|
|
|
- body.Friction=Friction
|
|
|
- body.RollingFriction=RollingFriction
|
|
|
- body.Restitution=Restitution
|
|
|
+ If Not _btbody
|
|
|
+ Validate()
|
|
|
+ Return
|
|
|
End
|
|
|
- End
|
|
|
-
|
|
|
- Property Mass:Float()
|
|
|
|
|
|
- Return _mass
|
|
|
- End
|
|
|
-
|
|
|
- Property Gravity:Vec3f()
|
|
|
-
|
|
|
- Return _btbody.getGravity()
|
|
|
-
|
|
|
- Setter( gravity:Vec3f )
|
|
|
-
|
|
|
- _btbody.setGravity( gravity )
|
|
|
- End
|
|
|
+ If _kinematic Return
|
|
|
|
|
|
- Property LinearVelocity:Vec3f()
|
|
|
-
|
|
|
- Return _btbody.getLinearVelocity()
|
|
|
-
|
|
|
- Setter( velocity:Vec3f )
|
|
|
-
|
|
|
- _btbody.setLinearVelocity( velocity )
|
|
|
+ If _seq=Entity.Seq Return
|
|
|
+
|
|
|
+ _btbody.clearForces()
|
|
|
+ _btbody.setLinearVelocity( New Vec3f( 0 ) )
|
|
|
+ _btbody.setAngularVelocity( New Vec3f( 0 ) )
|
|
|
+ _btbody.setWorldTransform( Entity.Matrix )
|
|
|
+ _btmotion.setWorldTransform( Entity.Matrix )
|
|
|
End
|
|
|
|
|
|
- Property AngularVelocity:Vec3f()
|
|
|
+ Method OnUpdate( elapsed:Float ) Override
|
|
|
|
|
|
- Return _btbody.getAngularVelocity()
|
|
|
-
|
|
|
- Setter( velocity:Vec3f )
|
|
|
+ If _kinematic Return
|
|
|
|
|
|
- _btbody.setAngularVelocity( velocity )
|
|
|
- End
|
|
|
-
|
|
|
- Method ApplyForce( force:Vec3f,relativePos:Vec3f )
|
|
|
+ Local tform:=_btmotion.getWorldTransform()
|
|
|
|
|
|
- _btbody.applyForce( force,relativePos )
|
|
|
- End
|
|
|
-
|
|
|
- Method ApplyCentralForce( force:Vec3f )
|
|
|
+ Entity.Position=tform.getOrigin()
|
|
|
|
|
|
- _btbody.applyCentralForce( force )
|
|
|
- End
|
|
|
-
|
|
|
- Method ApplyTorque( torque:Vec3f )
|
|
|
+ Entity.Basis=tform.getBasis()
|
|
|
|
|
|
- _btbody.applyTorque( torque )
|
|
|
+ _seq=Entity.Seq
|
|
|
End
|
|
|
|
|
|
- Method ApplyImpulse( impulse:Vec3f,relativePos:Vec3f )
|
|
|
+ Method OnDestroy() Override
|
|
|
|
|
|
- _btbody.applyImpulse( impulse,relativePos )
|
|
|
- End
|
|
|
-
|
|
|
- Method ApplyCentralImpulse( impulse:Vec3f )
|
|
|
+ If Not _btbody Return
|
|
|
|
|
|
- _btbody.applyCentralImpulse( impulse )
|
|
|
- End
|
|
|
-
|
|
|
- Method ApplyTorqueImpulse( torque:Vec3f )
|
|
|
+ World.GetCurrent().Remove( Self )
|
|
|
|
|
|
- _btbody.applyTorqueImpulse( torque )
|
|
|
+ _btbody=Null
|
|
|
End
|
|
|
|
|
|
-Protected
|
|
|
+ Internal
|
|
|
|
|
|
- Method OnValidate() Override
|
|
|
+ Method Validate:btRigidBody()
|
|
|
|
|
|
- If _seq=_entity.Seq Return
|
|
|
-
|
|
|
- _btbody.clearForces()
|
|
|
-
|
|
|
- _btbody.setLinearVelocity( New Vec3f( 0 ) )
|
|
|
-
|
|
|
- _btbody.setAngularVelocity( New Vec3f( 0 ) )
|
|
|
+ If _btbody Return _btbody
|
|
|
+
|
|
|
+ If _kinematic
|
|
|
+ _btmotion=New KinematicMotionState( Entity )
|
|
|
+ Else
|
|
|
+ _btmotion=New btDefaultMotionState( Entity.Matrix )
|
|
|
+ Endif
|
|
|
|
|
|
- _btbody.setWorldTransform( _entity.Matrix )
|
|
|
+ Local collider:=Entity.GetComponent<Collider>()
|
|
|
|
|
|
- _btmotion.setWorldTransform( _entity.Matrix )
|
|
|
- End
|
|
|
-
|
|
|
- Method OnUpdate() Override
|
|
|
+ Local inertia:btVector3=collider ? collider.CalculateLocalInertia( _mass ) Else Null
|
|
|
|
|
|
- Local tform:=_btmotion.getWorldTransform()
|
|
|
+ _btbody=New btRigidBody( _mass,_btmotion,collider.btShape,inertia )
|
|
|
+
|
|
|
+ If _kinematic
|
|
|
+ _btbody.setCollisionFlags( _btbody.getCollisionFlags() | btCollisionObject.CF_KINEMATIC_OBJECT )
|
|
|
+ _btbody.setActivationState( DISABLE_DEACTIVATION )
|
|
|
+ Endif
|
|
|
|
|
|
- _entity.Position=tform.getOrigin()
|
|
|
+ _btbody.setFriction( _friction )
|
|
|
+ _btbody.setRollingFriction( _rfriction )
|
|
|
+ _btbody.setRestitution( _restitution )
|
|
|
|
|
|
- _entity.Basis=tform.getBasis()
|
|
|
+ World.Add( Self )
|
|
|
|
|
|
- _seq=_entity.Seq
|
|
|
+ Return _btbody
|
|
|
End
|
|
|
+
|
|
|
+ Private
|
|
|
+
|
|
|
+ Field _kinematic:Bool=False
|
|
|
+ Field _mass:Float=1
|
|
|
+ Field _friction:Float=1
|
|
|
+ Field _rfriction:Float=1
|
|
|
+ Field _restitution:Float=0
|
|
|
+ Field _collGroup:Short=1
|
|
|
+ Field _collMask:Short=1
|
|
|
+
|
|
|
+ Field _btmotion:btMotionState
|
|
|
+ Field _btbody:btRigidBody
|
|
|
+ Field _seq:Int
|
|
|
|
|
|
End
|