About Monkey 2 › Forums › Monkey 2 Code Library › Repositioning physics bodies
Tagged: bodies, body, physics, re-position, reposition
This topic contains 0 replies, has 1 voice, and was last updated by DruggedBunny 1 month, 1 week ago.
-
AuthorPosts
-
January 9, 2019 at 12:42 pm #15870
Quick sample that allows repositioning of physics bodies, which I didn’t think was possible until now.
Main intent is to avoid reallocation of physics bodies, allowing re-use.
Don’t rely on this — not heavily tested!
EDIT: This was failing inconsistently on independent program runs. I’ve revised it and it now seems much more reliable.
You just include the Entity class extension below, then call the reposition and/or reorientation methods from your entity. (If no RigidBody is attached, nothing happens.)
Monkey
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162Class Entity Extension' reset_linear_velocity: zero-out linear velocity when repositioningMethod RepositionRigidBody (x:Float, y:Float, z:Float, reset_linear_velocity:Bool = True)If Not RigidBody Then ReturnLocal stored_state:Int = RigidBody.btBody.getActivationState ()RigidBody.btBody.setActivationState (bullet.DISABLE_DEACTIVATION) ' Needed as sleeping objects won't move otherwise!RigidBody.btBody.clearForces ()If reset_linear_velocityRigidBody.btBody.setLinearVelocity (New bullet.btVector3 (0, 0, 0))EndifLocal transform:bullet.btTransform = New bullet.btTransformLocal vector:bullet.btVector3 = New bullet.btVector3 (x, y, z)transform.setBasis (RigidBody.btBody.getWorldTransform ().getBasis ())transform.setOrigin (vector)RigidBody.btBody.proceedToTransform (transform)RigidBody.btBody.setActivationState (stored_state)End' reset_rotation_velocity: zero-out angular velocity when repositioningMethod ReorientRigidBody (pitch:Float, roll:Float, yaw:Float, reset_rotation_velocity:Bool = True)If Not RigidBody Then ReturnLocal stored_state:Int = RigidBody.btBody.getActivationState ()RigidBody.btBody.setActivationState (bullet.DISABLE_DEACTIVATION) ' Needed as sleeping objects won't move otherwise!RigidBody.btBody.clearForces ()If reset_rotation_velocityRigidBody.btBody.setAngularVelocity (New bullet.btVector3 (0, 0, 0))EndifLocal transform:bullet.btTransform = New bullet.btTransformLocal rotation:Mat3f = New Mat3f ()rotation = rotation.Rotate (pitch, roll, yaw)transform.setBasis (rotation)transform.setOrigin (RigidBody.btBody.getWorldTransform ().getOrigin ())RigidBody.btBody.proceedToTransform (transform)RigidBody.btBody.setActivationState (stored_state)EndEndDemo… the point being that the rigid body is being manually re-positioned, instead of having to re-create it:
Monkey
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194Namespace myapp3d#Import "<std>"#Import "<mojo>"#Import "<mojo3d>"Using std..Using mojo..Using mojo3d..Class Entity Extension' reset_linear_velocity: zero-out linear velocity when repositioningMethod RepositionRigidBody (x:Float, y:Float, z:Float, reset_linear_velocity:Bool = True)If Not RigidBody Then ReturnLocal stored_state:Int = RigidBody.btBody.getActivationState ()RigidBody.btBody.setActivationState (bullet.DISABLE_DEACTIVATION) ' Needed as sleeping objects won't move otherwise!RigidBody.btBody.clearForces ()If reset_linear_velocityRigidBody.btBody.setLinearVelocity (New bullet.btVector3 (0, 0, 0))EndifLocal transform:bullet.btTransform = New bullet.btTransformLocal vector:bullet.btVector3 = New bullet.btVector3 (x, y, z)transform.setBasis (RigidBody.btBody.getWorldTransform ().getBasis ())transform.setOrigin (vector)RigidBody.btBody.proceedToTransform (transform)RigidBody.btBody.setActivationState (stored_state)End' reset_rotation_velocity: zero-out angular velocity when repositioningMethod ReorientRigidBody (pitch:Float, roll:Float, yaw:Float, reset_rotation_velocity:Bool = True)If Not RigidBody Then ReturnLocal stored_state:Int = RigidBody.btBody.getActivationState ()RigidBody.btBody.setActivationState (bullet.DISABLE_DEACTIVATION) ' Needed as sleeping objects won't move otherwise!RigidBody.btBody.clearForces ()If reset_rotation_velocityRigidBody.btBody.setAngularVelocity (New bullet.btVector3 (0, 0, 0))EndifLocal transform:bullet.btTransform = New bullet.btTransformLocal rotation:Mat3f = New Mat3f ()rotation = rotation.Rotate (pitch, roll, yaw)transform.setBasis (rotation)transform.setOrigin (RigidBody.btBody.getWorldTransform ().getOrigin ())RigidBody.btBody.proceedToTransform (transform)RigidBody.btBody.setActivationState (stored_state)EndEndClass Brick Extends BehaviourFunction Create:Brick (width:Float = 3.0, height:Float = 1.0, depth:Float = 2.0)width = width * 0.5height = height * 0.5depth = depth * 0.5Local box:Boxf = New Boxf (-width, -height, -depth, width, height, depth)Local model:Model = Model.CreateBox (box, 2, 2, 2, New PbrMaterial (Color.Red))model.Move (0, 7.5, 0)model.AddComponent <RigidBody> ()Local collider:BoxCollider = model.AddComponent <BoxCollider> ()collider.Box = boxReturn New Brick (model)EndMethod New (entity:Entity)Super.New (entity)AddInstance ()EndMethod OnStart () OverrideEntity.RigidBody.ApplyTorqueImpulse (New Vec3f (1, 2, 4))EndEndClass MyWindow Extends WindowField _scene:SceneField _camera:CameraField _light:LightField _ground:ModelField brick:BrickMethod New( title:String="Simple mojo3d app",width:Int=640,height:Int=480,flags:WindowFlags=WindowFlags.Resizable )Super.New( title,width,height,flags )EndMethod OnCreateWindow() Override'create (current) scene_scene=New Scene_scene.ClearColor = New Color( 0.2, 0.6, 1.0 )_scene.AmbientLight = _scene.ClearColor * 0.25_scene.FogColor = _scene.ClearColor_scene.FogNear = 1.0_scene.FogFar = 200.0'create camera_camera=New Camera( Self )_camera.AddComponent<FlyBehaviour>()_camera.Move( 0,2.5,-5 )'create light_light=New Light_light.CastsShadow=True_light.Rotate( 45, 45, 0 )'create groundLocal groundBox:=New Boxf( -100,-1,-100,100,0,100 )Local groundMaterial:=New PbrMaterial( Color.Lime )_ground=Model.CreateBox( groundBox,1,1,1,groundMaterial )_ground.CastsShadow=False_ground.Alpha = 0.8' Visual marker...Model.CreateCylinder (0.1, 20, Axis.Y, 8, New PbrMaterial (Color.White)).Move (0, 10, 20)Local gb:RigidBody = _ground.AddComponent <RigidBody> ()gb.Mass = 0Local gc:BoxCollider = _ground.AddComponent <BoxCollider> ()gc.Box = groundBoxbrick = Brick.Create ()EndMethod OnRender( canvas:Canvas ) OverrideIf Keyboard.KeyHit (Key.Escape) Then App.Terminate ()If Keyboard.KeyHit (Key.Space)brick.Entity.RepositionRigidBody (Rnd (-25, 25), Rnd (5, 25), Rnd (5, 20))brick.Entity.ReorientRigidBody (0, 0, 0)Endif_camera.PointAt (brick.Entity)RequestRender()_scene.Update()_camera.Render( canvas )EndEndFunction Main()New AppInstanceNew MyWindowApp.Run()End -
AuthorPosts
You must be logged in to reply to this topic.