About Monkey 2 › Forums › Monkey 2 Programming Help › mojo3d: attempted box physics not working!
Tagged: 3d, bounds, collider, dynamicbody, mojo3d, physics, rigidbody, staticbody
This topic contains 5 replies, has 2 voices, and was last updated by
Mark Sibly
1 year, 8 months ago.
-
AuthorPosts
-
August 14, 2017 at 10:36 pm #9825
I can’t tell why this isn’t working: it builds static and dynamic bodies (CreateStaticBox/CreateDynamicBox) from a model passed to it, and everything renders OK, but my dynamic box body falls through the static ground body!
The method I use (using Model.Mesh.Bounds for New BoxCollider’s Boxf) works if I modify shapes.monkey2 to use ths method, so I don’t think that’s the problem, and hard-coding the sizes didn’t fix it (see commented-out returns in above functions)…
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177#Import "<std>"#Import "<mojo3d>"#Import "<mojo3d-physics>"Using std..Using mojo..Using mojo3d..' ----------------------------------------' Application name...' ----------------------------------------Global AppName:String = "Physics test"' ----------------------------------------Function CreateStaticBox:StaticBody (model:Model)' Build static body from model's mesh bounds (Boxf) and model...' Works in shapes.monkey2 with line 74 modified as per:' meshes[0]=Mesh.CreateBox( New Boxf( -1,-5,-1,1,5,1 ),1,1,1 )' colliders[0]=New BoxCollider(meshes[0].Bounds)'( New Boxf( -1,-5,-1,1,5,1 ) )Return New StaticBody (New BoxCollider (model.Mesh.Bounds), model)' Manual Boxf size doesn't do it...' Return New StaticBody (New BoxCollider (New Boxf (-50, -0.5, -50, 50, 0.5, 50)), model)EndFunction CreateDynamicBox:DynamicBody (model:Model)' Build dynamic body from model's mesh bounds (Boxf) and model...Return New DynamicBody (New BoxCollider (model.Mesh.Bounds), model)' Return New DynamicBody (New BoxCollider (New Boxf (-0.5, -0.5, -0.5, 0.5, 0.5, 0.5)), model)End' 1 x 1 x 1 box... gets resized after creation in demo below...Function CreateEasyBox:Model (color:Color = Color.Aluminum)Return New Model (Mesh.CreateBox (New Boxf (-0.5, -0.5, -0.5, 0.5, 0.5, 0.5)), New PbrMaterial (color))EndClass Game Extends Window' Basic 3D scene requirements...Field scene:SceneField camera:CameraField light:LightField ground:StaticBodyField box:DynamicBodyField phys_go:BoolMethod New (title:String, width:Int, height:Int, flags:WindowFlags)Super.New (title, width, height, flags)scene = Scene.GetCurrent ()camera = New Cameracamera.Move (0, 5, -15)light = New Lightlight.Move (-50, 50, 0)' Create mesh/entity...Local ground_model:Model = CreateEasyBox (Color.Green)ground_model.SetScale (100, 1, 100)ground_model.Move (0, -0.5, 0)' Build static physics body from mesh/entity...ground = CreateStaticBox (ground_model)' Create mesh/entity...Local box_model:Model = CreateEasyBox (Color.Red)box_model.SetScale (1, 1, 1)box_model.Move (0, 10, 0)box_model.Rotate (1, 2, 4)' Build static physics body from mesh/entity...box = CreateDynamicBox (box_model)' Light ground...light.PointAt (ground_model)' Look at box...camera.PointAt (box_model)EndMethod ProcessInput:Void ()If Keyboard.KeyHit (Key.Space) Then phys_go = TrueIf Keyboard.KeyHit (Key.Escape) Then App.Terminate ()If Keyboard.KeyDown (Key.A)camera.Move (0.0, 0.0, 0.5)EndIf Keyboard.KeyDown (Key.Z)camera.Move (0.0, 0.0, -0.5)EndIf Keyboard.KeyDown (Key.Left)camera.Rotate (0.0, 1.0, 0.0)EndIf Keyboard.KeyDown (Key.Right)camera.Rotate (0.0, -1.0, 0.0)EndIf Keyboard.KeyDown (Key.Up)camera.Rotate (1.0, 0.0, 0.0, True)EndIf Keyboard.KeyDown (Key.Down)camera.Rotate (-1.0, 0.0, 0.0, True)EndEndMethod OnRender (canvas:Canvas) OverrideRequestRender ()If phys_goWorld.GetDefault ().Update ()EndifProcessInput ()scene.Render (canvas, camera)If Not phys_gocanvas.DrawText ("Hit SPACE to start physics...", 20, 20)EndifEndEndFunction Main ()' Windowed mode...Local width:Int = 640Local height:Int = 480Local flags:WindowFlags = WindowFlags.CenterNew AppInstanceNew Game (AppName, width, height, flags)App.Run ()EndHelp!
August 15, 2017 at 5:13 am #9832I think the problem here is that SetScale doesn’t currently affect the physics, only the rendering, so the ground box looks big but is still only actually a unit cube as far as the phyics is concerned.
I’ll have a think about how best to deal with this but for now thje BoxCollider size will need to be it’s ‘actual’ size.
August 15, 2017 at 12:27 pm #9838Hi Mark, I thought that, but I’m not actually convinced!
[IGNORE: See last post!]
See my second paragraph — it works when I use that same method in shapes.monkey2, I think because I:
- create my box,
- resize it,
- pass that resized box (with, presumably, its resized .Bounds Boxf) to the collider setup.
So, collider setup should be using the .Bounds of the resized box model at that point, not scaling after creating the collider. Try this (modded shapes.monkey2):
[EDIT: See stripped-down version below.]
Also, using hard-coded values didn’t work — see the commented-out return values in CreateStatc/DynamicBox in first example.
August 15, 2017 at 11:01 pm #9843[IGNORE: See last post! But see comment about tweaking ANGLE const in this demo!]
Here’s a stripped-down version of shapes.monkey2 showing mesh.bounds being used sucessfully for collider:
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687Namespace myapp#Import "<std>"#Import "<mojo>"#Import "<mojo3d>"#Import "<mojo3d-physics>"#Import "assets/"Using std..Using mojo..Using mojo3d..Const ANGLE:Float = 40 ' 10-20 for weirdness!Class MyWindow Extends WindowField _scene:SceneField _camera:CameraField _light:LightField _ground:ModelMethod New( title:String="Simple mojo app",width:Int=640,height:Int=480,flags:WindowFlags=WindowFlags.Resizable )Super.New( title,width,height,flags )_scene=Scene.GetCurrent()'create camera'_camera=New Camera_camera.Move( 0,10,-10 )'create light'_light=New Light_light.Move (-10, 10, 10)_light.RotateX( 90 ) 'aim directional light 'down'.'create ground'Local groundBox:=New Boxf( -60,-1,-60,60,0,60 )_ground=Model.CreateBox( groundBox,16,16,16,New PbrMaterial( Color.Green ) )Local gcollider:Collider=New BoxCollider( groundBox )Local gbody:=New StaticBody( gcollider,_ground )'create some meshes/collidersLocal mesh:=New MeshLocal collider:=New Collidermesh=Mesh.CreateBox( New Boxf( -1,-5,-1,1,5,1 ),1,1,1 )collider=New BoxCollider(mesh.Bounds) ' USING MESH.BOUNDS ************Local material:= New PbrMaterial (New Color (255, 0, 255))Local model:=New Model( mesh,material )model.Move( 0,10,0 )model.Rotate (0, 0, ANGLE)Local body:=New DynamicBody( collider,model )_light.PointAt (_ground)_camera.PointAt (_ground)EndMethod OnRender( canvas:Canvas ) OverrideIf Keyboard.KeyHit (Key.Escape) Then App.Terminate ()If Keyboard.KeyDown (Key.Up) Then _camera.Rotate (1.0, 0.0, 0.0, True)If Keyboard.KeyDown (Key.Down) Then _camera.Rotate (-1.0, 0.0, 0.0, True)RequestRender()World.GetDefault().Update()_scene.Render( canvas,_camera )EndEndFunction Main()New AppInstanceNew MyWindowApp.Run()EndThis demo also highlights some weirdness I noticed in the original sometimes — change const ANGLE at the top to 10-20 and watch it settle at a strange angle!
August 15, 2017 at 11:11 pm #9844Eek, maybe ignore all of this — my tweaked demo doesn’t use SetScale! Adding
Monkey1_ground.SetScale (100, 1, 100)causes it to fall through. I assumed the bounds would be updated, then the collider built using that updated bounds (ie. thought you were referring to resizing not working after the collider is built), but apparently bounds stays fixed…
August 16, 2017 at 3:40 am #9845SetScale is an algorithmic entity scale factor used by the renderer. It does not affect actual mesh vertex positions (as stored in memory) therefore does not affect mesh bounds either. If there was an Entity.Bounds, it would probably affect that though!
Mesh.TransformVertices and Mesh.FitVertices on the other hand *do* affect mesh vertex positions, so do affect mesh bounds.
In general, my advice for now would be to avoid SetScale if you’re using physics and build all your meshes at the right size in the first place. You can also use Mesh.FitVertices if you want to physically scale a mesh to a certain size etc.
-
AuthorPosts
You must be logged in to reply to this topic.