About Monkey 2 › Forums › Monkey 2 Code Library › Collision groups setup/demo
Tagged: collision groups, collision masks, collision types, collisions
This topic contains 0 replies, has 1 voice, and was last updated by 
 DruggedBunny
 11 months, 1 week ago.
- 
		AuthorPosts
 - 
		
			
				
May 13, 2018 at 12:24 am #14615
Not the most intuitive demo to play, but this basically shows how to enable collisions betweens different types of objects, so the collision setup within the code is the important part here.
In this case, the different types are represented by red, blue and yellow cubes. Use the white ball to bash cubes against each other — red collide against red, blue against blue and yellow against yellow; any other combination will fail to collide, which can be seen by the flickering intersections.
Setting it all up is a little mind-bending, but it’s not actually too different to Blitz3D now that (I think) I’ve got it…
See comments to enable red/yellow collisions, for example:
[/crayon]Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274[crayon-5cb9a1e6c60cb489898619 inline="true" ]'#Import "<std>"#Import "<mojo3d>"Using std..Using mojo..Using mojo3d..Global AppName:String = "My 3D Game"Class Game Extends WindowConst SHIFT_BOOST:Float = 5.0Field camera_boost:Float = 1.0' Basic 3D scene requirements...Field scene:SceneField camera:CameraField light:Light' Test cube...Field cube:ModelField c_body:RigidBodyMethod New (title:String, width:Int, height:Int, flags:WindowFlags)Super.New (title, width, height, flags)SeedRnd (Millisecs ())#RemBullet uses a signed short int to store the collision groups and the collision masks.This means you can have up to 15 different groups, and 15 different masks....collision masks must match both ways for there to be a collision.#End' Collision types (MASKS)Local COLL_NOTHING:Short = 0Local COLL_GROUND:Short = 1Local COLL_CUBE:Short = 2Local COLL_RED:Short = 4Local COLL_BLUE:Short = 8Local COLL_YELLOW:Short = 16' -----------------------------------' Collision groups' -----------------------------------' Ground collides with all cubesLocal GROUND_COLLIDES_WITH:Short = COLL_CUBE | COLL_RED | COLL_BLUE | COLL_YELLOW' Player cube collides with ground and all other cubesLocal CUBE_COLLIDES_WITH:Short = COLL_GROUND | COLL_RED | COLL_BLUE | COLL_YELLOW' Red only collides with Red' Blue only collides with Blue' Yellow only collides with Yellow' All additionally collide with ground and (player) cube' To allow Red to collide with Yellow, uncomment first and last lines here:Local RED_COLLIDES_WITH:Short = COLL_GROUND | COLL_RED | COLL_CUBE ' | COLL_YELLOWLocal BLUE_COLLIDES_WITH:Short = COLL_GROUND | COLL_BLUE | COLL_CUBELocal YELLOW_COLLIDES_WITH:Short = COLL_GROUND | COLL_YELLOW | COLL_CUBE ' | COLL_REDscene = Scene.GetCurrent ()camera = New Cameracamera.Near = 0.1camera.Move (0, 10, -2)Local north:Model = Model.CreateCone (5, 10, Axis.Y, 32, New PbrMaterial (Color.HotPink))north.Move (0, 0, 10)light = New Lightlight.Move (-100, 100, -100)light.CastsShadow = True' All cubes use this box size...Local c_box:Boxf = New Boxf (-0.5, -0.5, -0.5, 0.5, 0.5, 0.5)' Player cube (white)' cube = Model.CreateBox (c_box, 2, 2, 2, New PbrMaterial (Color.Aluminum))cube = Model.CreateSphere (0.5, 16, 16, New PbrMaterial (Color.Aluminum))cube.Move (0, 10, 0)cube.Rotate (0, 45, 0)' Local c_coll:BoxCollider = cube.AddComponent <BoxCollider> ()Local c_coll:SphereCollider = cube.AddComponent <SphereCollider> ()' c_coll.Box = c_boxc_coll.Radius = 0.5c_body = cube.AddComponent <RigidBody> ()c_body.CollisionMask = COLL_CUBEc_body.CollisionGroup = CUBE_COLLIDES_WITH' Red cubesFor Local loop:Int = 1 To 10Local red_cube:Modelred_cube = Model.CreateBox (c_box, 2, 2, 2, New PbrMaterial (Color.Red))red_cube.Move (Rnd (-5, 5), Rnd (5.5, 20), Rnd (-5, 5))red_cube.Rotate (0, Rnd (360), 0)Local red_coll:BoxCollider = red_cube.AddComponent <BoxCollider> ()red_coll.Box = c_boxLocal red_body:RigidBody = red_cube.AddComponent <RigidBody> ()red_body.Mass = 1.0red_body.CollisionMask = COLL_REDred_body.CollisionGroup = RED_COLLIDES_WITHNext' Blue cubesFor Local loop:Int = 1 To 10Local blue_cube:Modelblue_cube = Model.CreateBox (c_box, 2, 2, 2, New PbrMaterial (Color.Blue))blue_cube.Move (Rnd (-5, 5), Rnd (5.5, 20), Rnd (-5, 5))blue_cube.Rotate (0, Rnd (360), 0)Local blue_coll:BoxCollider = blue_cube.AddComponent <BoxCollider> ()blue_coll.Box = c_boxLocal blue_body:RigidBody = blue_cube.AddComponent <RigidBody> ()blue_body.Mass = 1.0blue_body.CollisionMask = COLL_BLUEblue_body.CollisionGroup = BLUE_COLLIDES_WITHNext' Yellow cubesFor Local loop:Int = 1 To 10Local yellow_cube:Modelyellow_cube = Model.CreateBox (c_box, 2, 2, 2, New PbrMaterial (Color.Yellow))yellow_cube.Move (Rnd (-5, 5), Rnd (5.5, 20), Rnd (-5, 5))yellow_cube.Rotate (0, Rnd (360), 0)Local yellow_coll:BoxCollider = yellow_cube.AddComponent <BoxCollider> ()yellow_coll.Box = c_boxLocal yellow_body:RigidBody = yellow_cube.AddComponent <RigidBody> ()yellow_body.Mass = 1.0yellow_body.CollisionMask = COLL_YELLOWyellow_body.CollisionGroup = YELLOW_COLLIDES_WITHNext' GroundLocal g_box:Boxf = New Boxf (-100, -5, -100, 100, 5, 100)Local ground:Model = Model.CreateBox (g_box, 4, 4, 4, New PbrMaterial (Color.Green * 0.25))Local g_coll:BoxCollider = ground.AddComponent <BoxCollider> ()g_coll.Box = g_boxLocal g_body:RigidBody = ground.AddComponent <RigidBody> ()g_body.Mass = 0.0g_body.CollisionMask = COLL_GROUNDg_body.CollisionGroup = GROUND_COLLIDES_WITHlight.PointAt (ground)EndMethod OnRender (canvas:Canvas) OverrideProcessInput ()RequestRender ()scene.Update ()scene.Render (canvas)canvas.DrawText ("Cubes of different colours SHOULD intersect (will flicker)!", 20, 20)canvas.DrawText ("Cubes of same colours should collide when pushed together", 20, 40)canvas.DrawText ("Try to bash same-coloured cubes together to see them collide!", 20, 80)canvas.DrawText ("Cursors control white ball", 20, 120)EndMethod ProcessInput:Void ()camera.PointAt (cube)If Keyboard.KeyHit (Key.Space) Then light.CastsShadow = Not light.CastsShadowIf Keyboard.KeyHit (Key.Escape) Then App.Terminate ()If Keyboard.KeyDown (Key.Left)c_body.ApplyImpulse (camera.Basis * New Vec3f (-0.2, 0, 0))EndifIf Keyboard.KeyDown (Key.Right)c_body.ApplyImpulse (camera.Basis * New Vec3f (0.2, 0, 0))EndifIf Keyboard.KeyDown (Key.Up)c_body.ApplyImpulse (camera.Basis * New Vec3f (0, 0, 0.2))EndifIf Keyboard.KeyDown (Key.Down)c_body.ApplyImpulse (camera.Basis * New Vec3f (0, 0, -0.2))EndifEndEndFunction Main ()' Windowed mode...Local width:Int = 640Local height:Int = 480Local flags:WindowFlags = WindowFlags.CenterNew AppInstanceNew Game (AppName, width, height, flags)App.Run ()End - 
		AuthorPosts
 
You must be logged in to reply to this topic.