Browse Source

Physics tweaks.

Mark Sibly 7 years ago
parent
commit
365561a4a3

+ 45 - 2
modules/mojo3d/scene/components/joint.monkey2

@@ -1,5 +1,11 @@
 Namespace mojo3d
 
+Private
+
+'Global nullBody:btRigidBody=New btRigidBody( 0,Null,Null,Null )
+
+Public
+
 Class Joint Extends Component
 	
 	Const ERP:=0		'error reduction parameter - http://bulletphysics.org/mediawiki-1.5.8/index.php/Definitions
@@ -148,6 +154,8 @@ Class HingeJoint Extends Joint
 		Super.New( entity )
 		
 		Axis=New Vec3f( 0,1,0 )
+		MinAngle=180
+		MaxAngle=-180
 		
 		AddInstance()
 	End
@@ -159,6 +167,8 @@ Class HingeJoint Extends Joint
 		ConnectedBody=joint.ConnectedBody
 		Pivot=joint.Pivot
 		Axis=joint.Axis
+		MinAngle=joint.MinAngle
+		MaxAngle=joint.MaxAngle
 		
 		AddInstance( joint )
 	End
@@ -193,20 +203,51 @@ Class HingeJoint Extends Joint
 		_axis1=axis
 	End
 	
+	Property MinAngle:Float()
+		
+		Return _minAngle
+		
+	Setter( angle:Float ) 
+		
+		_minAngle=angle
+		
+		SetLimits()
+	End
+	
+	Property MaxAngle:Float()
+		
+		Return _maxAngle
+		
+	Setter( angle:Float ) 
+		
+		_maxAngle=angle
+		
+		SetLimits()
+	End
+	
 	Protected
 	
 	Field _connected:RigidBody
 	Field _pivot1:Vec3f
 	Field _axis1:Vec3f
+	Field _minAngle:Float
+	Field _maxAngle:Float
+	
+	Method SetLimits()
+		
+		If Not _btconstraint Return
+		
+		Cast<btHingeConstraint>(_btconstraint).setLimit( _minAngle*Pi/180,_maxAngle*Pi/180 )
+	End
 	
 	Method OnCreate() Override
 		
 		Local btBody1:=Entity.GetComponent<RigidBody>().btBody
-		Assert( btBody1,"BallSocketJoint: No rigid body" )	'todo: fail nicely
+		Assert( btBody1,"HingeJoint: No rigid body" )	'todo: fail nicely
 		
 		If _connected
 			Local btBody2:=_connected.btBody
-			Assert( btBody2,"BallSocketJoint: No rigid body" )	'todo: fail nicely
+			Assert( btBody2,"HingeJoint: No rigid body" )	'todo: fail nicely
 			Local tform:=_connected.Entity.InverseMatrix * Entity.Matrix
 			Local pivot2:=tform * _pivot1
 			Local axis2:=tform.m * _axis1
@@ -214,6 +255,8 @@ Class HingeJoint Extends Joint
 		Else
 			_btconstraint=New btHingeConstraint( btBody1,_pivot1,_axis1 )
 		End
+		
+		SetLimits()
 	End
 	
 End

+ 7 - 0
modules/mojo3d/scene/entityexts.monkey2

@@ -27,6 +27,13 @@ Class Entity Extension
 		Return GetComponent<Collider>()
 	End
 
+	#rem monkeydoc The joint attached to the entity.
+	#end
+	Property Joint:Joint()
+		
+		Return GetComponent<Joint>()
+	End
+
 	#rem monkeydoc The animator attached to the entity.
 	#end
 	Property Animator:Animator()

+ 3 - 1
modules/mojo3d/tests/hingechain.monkey2

@@ -4,7 +4,7 @@ Namespace myapp3d
 #Import "<mojo>"
 #Import "<mojo3d>"
 
-#Reflect mojo3d
+'#Reflect mojo3d
 
 Using std..
 Using mojo..
@@ -71,6 +71,8 @@ Class MyWindow Extends Window
 				joint.ConnectedBody=prev.RigidBody
 				joint.Pivot=New Vec3f( 0,-.5,0 )
 				joint.Axis=New Vec3f( 1,0,0 )
+				joint.MinAngle=-90
+				joint.MaxAngle=90
 			Endif
 			
 			prev=copy

+ 1 - 1
modules/mojo3d/tests/jointchain.monkey2

@@ -4,7 +4,7 @@ Namespace myapp3d
 #Import "<mojo>"
 #Import "<mojo3d>"
 
-#Reflect mojo3d
+'#Reflect mojo3d
 
 Using std..
 Using mojo..