About Monkey 2 › Forums › Monkey 2 Programming Help › mojo3d physics… why am I not colliding?
Tagged: collision group, collision mask, mojo3d, physics
This topic contains 4 replies, has 2 voices, and was last updated by
DruggedBunny
1 year, 3 months ago.
-
AuthorPosts
-
January 9, 2018 at 4:09 am #12764
I’m sure I ran into this months ago, but can’t find any trace of it!
The box and ground here don’t collide, though both have default collision group and mask of 1.
See Class PhysBox and CreateArena (“gb = New PhysBox” creates ground).
I’m unsure if I’m missing something, or if it was some kind of timing thing causing the collision to go undetected.
Would really like to see some kind of wrapper for this stuff, which is obviously what I was attempting here…
Also, how do the collision groups/masks work? Using mojo3d\tests\shapes.monkey2 I see CollisionMask is always 127, though these values default to 1 in the definition of New RigidBody (entity).
Something like cubes with collision group 1, spheres with collision group 2, and masks for both being (1 + 2) = 3 would mean cubes and spheres collide, something like that?
Should both values being 1 for ground and box cause collision here?
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224#Import "<std>"#Import "<mojo>"#Import "<mojo3d>"Using std..Using mojo..Using mojo3d..Class PhysBoxField box:BoxfField model:ModelField collider:BoxColliderField body:RigidBodyMethod New (width:Float = 1, height:Float = 1, depth:Float = 1, mass:Float = 1, material:Material = Null, group:Int = 1, mask:Int = 1)box = New Boxf (-width * 0.5, height * 0.5, depth * 0.5, width * 0.5, -height * 0.5, -depth * 0.5)If Not materialmaterial = New PbrMaterial (Color.Red)Endifmodel = Model.CreateBox (box, 8, 8, 8, material)collider = New BoxCollider (model)collider.Box = boxbody = New RigidBody (model)body.Mass = massbody.CollisionGroup = groupbody.CollisionMask = maskEndMethod Move (x:Float, y:Float, z:Float)model.Move (x, y, z)EndEndClass Game Extends WindowConst CAMERA_MOVE:Float = 0.05Const SHIFT_BOOST:Float = 4.0Field cam_boost:Float = 1.0Field scene:SceneField cam:CameraField light:LightField light_model:ModelField ground:ModelField ground_pixels:PixmapField heightmap:PixmapField terrain_material:PbrMaterialField gb:PhysBoxField pb:PhysBoxMethod New (title:String, width:Int, height:Int, flags:WindowFlags)Super.New (title, width, height, flags)CreateArena ()pb = New PhysBox ()pb.Move (0, 10, 5)EndMethod CreateArena:Void ()cam = New Cameracam.Near = 0.01cam.Far = 1000light = New Lightlight.CastsShadow = Truelight.Range = 1000light.Move (0, 50, 10) ' Yes, the sun is 50m above us...light_model = Model.CreateSphere (1, 32, 32, New PbrMaterial (Color.Yellow), light)light_model.CastsShadow = Falseground_pixels = New Pixmap (256, 256, PixelFormat.RGBA8)ground_pixels.Clear (Color.Grey)Local pixel_toggle:Int = 0For Local gp_y:Int = 0 Until ground_pixels.HeightFor Local gp_x:Int = 0 Until ground_pixels.WidthIf pixel_toggle Then ground_pixels.SetPixel (gp_x, gp_y, Color.White)pixel_toggle = 1 - pixel_toggleNextpixel_toggle = 1 - pixel_toggleNext#remheightmap = New Pixmap (4, 4, PixelFormat.RGBA8)heightmap.Clear (Color.Black)terrain_material = New PbrMaterial ()terrain_material.ColorTexture = New Texture (ground_pixels, TextureFlags.None)ground = Model.CreateTerrain (heightmap, New Boxf (-256, 0, -256, 256, 0, 256), terrain_material)ground.Move (0, -2, 0)light.PointAt (ground)#endgb = New PhysBox (100, 1, 100, 0, New PbrMaterial (Color.Green))gb.Move (0, -5, 0)light.PointAt (gb.model)scene = Scene.GetCurrent ()scene.AmbientLight = Color.White * 0.75scene.ClearColor = Color.Sky * 0.75EndMethod UpdateGame ()If Keyboard.KeyHit (Key.Escape)App.Terminate ()EndifIf Keyboard.KeyDown (Key.LeftShift)cam_boost = SHIFT_BOOSTElsecam_boost = 1.0EndifIf Keyboard.KeyDown (Key.A)cam.Move (0.0, 0.0, CAMERA_MOVE * cam_boost)EndifIf Keyboard.KeyDown (Key.Z)cam.Move (0.0, 0.0, -CAMERA_MOVE * cam_boost)EndifIf Keyboard.KeyDown (Key.Left)cam.Rotate (0.0, 1.0, 0.0)EndifIf Keyboard.KeyDown (Key.Right)cam.Rotate (0.0, -1.0, 0.0)EndifIf Keyboard.KeyDown (Key.Up)cam.Rotate (1.0, 0.0, 0.0, True)EndifIf Keyboard.KeyDown (Key.Down)cam.Rotate (-1.0, 0.0, 0.0, True)EndifEndMethod ShadowText:Void (canvas:Canvas, s:String, x:Float, y:Float)canvas.Color = Color.Blackcanvas.DrawText (s, x + 1, y + 1)canvas.Color = Color.Whitecanvas.DrawText (s, x, y)EndMethod RenderText (canvas:Canvas)ShadowText (canvas, "FPS: " + App.FPS, 20.0, 20.0)ShadowText (canvas, "A/Z + Cursors to move camera", 20.0, 60.0)EndMethod OnRender (canvas:Canvas) OverrideUpdateGame ()RequestRender ()scene.Update ()scene.Render (canvas, cam)RenderText (canvas)EndEndFunction Run3D (title:String, width:Int, height:Int, flags:WindowFlags = WindowFlags.Center)New AppInstanceNew Game (title, width, height, flags)App.Run ()EndFunction Main ()Run3D ("3D Scene", 640, 480, WindowFlags.Center)' Run3D ("3D Scene", 1920, 1080, WindowFlags.Fullscreen)EndJanuary 9, 2018 at 4:40 am #12765Collision groups/masks are just the way bullet does it and I may yet change it for mojo3d.
Both mask and group are just (16 bit) bitmasks, and both (entity1->mask & entity2->group) AND (entity2->mask & entity1->group) must be non-0 for entities to collide. I think – I find it really confusing myself too!
Safest thing to do is just make mask and group the same for all entities (non-0 though), and all entities can then collide. They default to 1 anyway.
This stuff is very much in flux right now, but I am working on it this week/right now.
January 9, 2018 at 8:06 am #12771Thanks, Mark — thy are both 1 at the moment, but aren’t colliding in this demo. Will be playing again tonight though, no doubt.
January 9, 2018 at 8:08 pm #12784Sorted this — turned out to be the order in which I was declaring the Boxf parameters at start of PhysBox.New ()!
Was:
Monkey1box = New Boxf (-width * 0.5, height * 0.5, depth * 0.5, width * 0.5, -height * 0.5, -depth * 0.5)… but should be:
Monkey1box = New Boxf (-width * 0.5, -height * 0.5, -depth * 0.5, width * 0.5, height * 0.5, depth * 0.5)January 9, 2018 at 9:08 pm #12785This is pretty cool, but I can’t figure out how to delete/remove RigidBodies… see KeyHit (Key.Space). I’m trying to clear my stack of PhysBoxes (containing RigidBody and Model references), but they always remain! Perhaps not implemented yet? There is code that looks intended to work in RigidBody.OnDestroy…
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238#Import "<std>"#Import "<mojo>"#Import "<mojo3d>"Using std..Using mojo..Using mojo3d..Class PhysBoxField box:BoxfField model:ModelField collider:BoxColliderField body:RigidBodyMethod New (width:Float = 1, height:Float = 1, depth:Float = 1, mass:Float = 1, material:Material = Null, group:Int = 1, mask:Int = 1)If Not materialmaterial = New PbrMaterial (Color.Red)Endifbox = New Boxf (-width * 0.5, -height * 0.5, -depth * 0.5, width * 0.5, height * 0.5, depth * 0.5)model = Model.CreateBox (box, 8, 8, 8, material)collider = New BoxCollider (model)body = New RigidBody (model)collider.Box = boxbody.Mass = massbody.CollisionGroup = groupbody.CollisionMask = maskEndMethod Move (x:Float, y:Float, z:Float)model.Move (x, y, z)EndMethod Rotate (pitch:Float, roll:Float, yaw:Float, localspace:Bool = False)model.Rotate (pitch, roll, yaw, localspace)EndEndClass Game Extends WindowConst CAMERA_MOVE:Float = 0.05Const SHIFT_BOOST:Float = 4.0Field cam_boost:Float = 1.0Field scene:SceneField cam:CameraField light:LightField ground:PhysBoxField boxes:Stack <PhysBox>Method New (title:String, width:Int, height:Int, flags:WindowFlags)Super.New (title, width, height, flags)SwapInterval = 1CreateArena ()EndMethod CreateArena:Void ()SeedRnd (Millisecs ())scene = Scene.GetCurrent ()ground = New PhysBox (100, 1, 100, 0, New PbrMaterial (Color.Green * 0.25))cam = New Cameralight = New Lightscene.AmbientLight = Color.White * 0.75scene.ClearColor = Color.Sky * 0.75cam.FOV = 100cam.Move (0, 20, -20)ground.Move (0, -5, 0)cam.Near = 0.01cam.Far = 1000light.CastsShadow = Truelight.Range = 1000light.Move (0, 50, 10)cam.PointAt (ground.model)light.PointAt (ground.model)SpewBoxes (200)EndMethod SpewBoxes (num:Int)boxes = New Stack <PhysBox>For Local loop:Int = 1 To numLocal scale:Float = Rnd (0.5, 4.0)Local color:Color = New Color (Rnd (0.4, 1.0), Rnd (0.4, 1.0), Rnd (0.4, 1.0))' Just for clarity in New below...Local dimension:Float = scaleLocal mass:Float = scaleLocal pb:PhysBox = New PhysBox (dimension, dimension, dimension, mass, New PbrMaterial (color))pb.Move (Rnd (-10, 10), Rnd (100), Rnd (-10, 10))' *** Set loop to max 1 and enable this -- physics mesh still appears unturned when settled!' pb.Rotate (45, 45, 45)NextEndMethod UpdateGame ()If Keyboard.KeyHit (Key.Escape)App.Terminate ()EndifIf Keyboard.KeyHit (Key.S)light.CastsShadow = Not light.CastsShadowEndifIf Keyboard.KeyHit (Key.Space)For Local pb:PhysBox = Eachin boxes' *** HELP! ***' pb.body.Destroy () ' er, = Null? OnDestroy? Um...' How about, per http://monkeycoder.co.nz/forums/topic/cast-object-to-int-handle-and-back/#post-10084pb.model.Destroy () ' Nope, all there!Nextboxes.Clear () ' Nah, bodies remain in Bullet! Prob need to destroy entities too...Print boxes.LengthSpewBoxes (200)EndifIf Keyboard.KeyDown (Key.LeftShift)cam_boost = SHIFT_BOOSTElsecam_boost = 1.0EndifIf Keyboard.KeyDown (Key.A)cam.Move (0.0, 0.0, CAMERA_MOVE * cam_boost)EndifIf Keyboard.KeyDown (Key.Z)cam.Move (0.0, 0.0, -CAMERA_MOVE * cam_boost)EndifIf Keyboard.KeyDown (Key.Left)cam.Rotate (0.0, 1.0, 0.0)EndifIf Keyboard.KeyDown (Key.Right)cam.Rotate (0.0, -1.0, 0.0)EndifIf Keyboard.KeyDown (Key.Up)cam.Rotate (1.0, 0.0, 0.0, True)EndifIf Keyboard.KeyDown (Key.Down)cam.Rotate (-1.0, 0.0, 0.0, True)EndifEndMethod ShadowText:Void (canvas:Canvas, s:String, x:Float, y:Float)canvas.Color = Color.Blackcanvas.DrawText (s, x + 1, y + 1)canvas.Color = Color.Whitecanvas.DrawText (s, x, y)EndMethod RenderText (canvas:Canvas)ShadowText (canvas, "FPS: " + App.FPS, 20.0, 20.0)ShadowText (canvas, "A/Z + Cursors to move camera", 20.0, 40.0)ShadowText (canvas, "SHIFT to boost", 20.0, 60.0)ShadowText (canvas, "SPACE to spew boxes", 20.0, 80.0)ShadowText (canvas, "S to toggle shadows", 20.0, 100.0)EndMethod OnRender (canvas:Canvas) OverrideUpdateGame ()RequestRender ()scene.Update ()scene.Render (canvas, cam)RenderText (canvas)EndEndFunction Run3D (title:String, width:Int, height:Int, flags:WindowFlags = WindowFlags.Center)New AppInstanceNew Game (title, width, height, flags)App.Run ()EndFunction Main ()Run3D ("3D Scene", 960, 540, WindowFlags.Center) ' 1/4 HD!' Run3D ("3D Scene", 1920, 1080, WindowFlags.Fullscreen)EndRotation doesn’t work, either, as the RigidBody doesn’t pick up the Model’s rotation, which I suppose it to be expected, though it picks up position. Not sure how this should be handled…
-
AuthorPosts
You must be logged in to reply to this topic.