About Monkey 2 › Forums › Monkey 2 Programming Help › Does Bullet auto-remove bodies once settled?
Tagged: bullet, delete, physics, remove, simulation
This topic contains 5 replies, has 2 voices, and was last updated by
DruggedBunny
1 year, 3 months ago.
-
AuthorPosts
-
January 15, 2018 at 12:25 am #12864
This little demo builds a wall of blocks, and if you hold B it applies a vertical force to all blocks.
If you press it before all blocks have settled, they rise up as expected.
However, if you just run it and let the blocks settle (leave for 5 seconds or so), they don’t respond to B, even though the list count shows all the objects are present.
If you run it and boost them into the air, then wait until all are settled (again, leave for around 5 seconds), they no longer respond.
As I see it, either something’s wrong in my logic (no way!!), or Bullet is auto-removing/deactivating the rigid bodies from the simulation 5 seconds or so after they’ve settled.
It’s not down to UpdateBoxes (this can be commented out) or rebuilding the wall via Space bar, as if you do nothing my PhysBox removal logic isn’t even activated.
EDIT: Fixed
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321#Import "<std>"#Import "<mojo>"#Import "<mojo3d>"#Import "<bullet>"Using std..Using mojo..Using mojo3d..Const WALL_WIDTH:Int = 10Const WALL_HEIGHT:Int = 5Class PhysBoxClass PhysParamsField mass:FloatField group:ShortField mask:ShortEndField box:BoxfField model:ModelField collider:BoxColliderField body:RigidBodyField init:PhysParamsMethod New (width:Float = 1, height:Float = 1, depth:Float = 1, mass:Float = 1, material:Material = Null, group:Int = 1, mask:Int = 1)' Store setup params for PhysBox.Start ()init = New PhysParamsinit.mass = massinit.group = groupinit.mask = maskIf 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, 1, 1, 1, material)EndMethod Start ()collider = New BoxCollider (model)body = New RigidBody (model)collider.Box = boxbody.Mass = init.massbody.CollisionGroup = init.groupbody.CollisionMask = init.maskbody.btBody.setActivationState (bullet.DISABLE_DEACTIVATION)box = Nullinit = NullEndMethod 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 CAMERA_BOOST:Float = 4.0Field cam_boost:Float = 1.0Field scene:SceneField cam:CameraField light:LightField ground:PhysBoxField boxes:List <PhysBox>Field bullets:List <PhysBox>Method New (title:String, width:Int, height:Int, flags:WindowFlags)Super.New (title, width, height, flags)SwapInterval = 1CreateArena (15)EndMethod CreateArena:Void (ground_size:Float = 100)SeedRnd (Millisecs ())scene = Scene.GetCurrent ()ground = New PhysBox (ground_size, 1, ground_size, 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, 5, -15)ground.Start ()cam.Near = 0.01cam.Far = 1000light.CastsShadow = Truelight.Range = 1000light.Move (0, 20, 10)cam.PointAt (ground.model)light.PointAt (ground.model)boxes = New List <PhysBox>bullets = New List <PhysBox>BuildWall (WALL_WIDTH, WALL_HEIGHT)EndMethod BuildWall (width:Int, height:Int)For Local y:Int = 0 Until heightFor Local x:Int = 0 Until widthLocal color:Color = New Color (Rnd (0.4, 1.0), Rnd (0.4, 1.0), Rnd (0.4, 1.0))' Create new PhysBox...Local pb:PhysBox = New PhysBox (1, 1, 1, 1, New PbrMaterial (color))' Position PhysBox...pb.Move (x - (width / 2.0), y + 1, 0)boxes.Add (pb)' Start its physics...pb.Start ()NextNextEndMethod DropBox ()Local pb:PhysBox = New PhysBox (1, 1, 1, 1, New PbrMaterial (Color.Red))pb.Move (0, 50, 0)boxes.Add (pb)pb.Start ()EndMethod BumpWall ()Local vec:Vec3f = New Vec3f (0.0, 0.5, 0.0)'Local count:IntFor Local pb:PhysBox = Eachin boxes'If Not pb.collider Then Print "No collider"'If Not pb.body Then Print "No body"pb.body.ApplyImpulse (vec)'Print "Body count: " + count'count = count + 1Next'Print Millisecs ()EndMethod UpdateBoxes ()For Local pb:PhysBox = Eachin boxesIf pb.model.Y < -20pb.model.Scale = pb.model.Scale * 0.75If pb.model.Scale.x < 0.01pb.model.Destroy ()boxes.Remove (pb)EndifEndifNextEndMethod UpdateGame ()UpdateBoxes ()If Keyboard.KeyDown (Key.D)DropBox ()EndifIf Keyboard.KeyDown (Key.B)BumpWall ()EndifIf 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 boxespb.model.Destroy ()boxes.Remove (pb)NextBuildWall (WALL_WIDTH, WALL_HEIGHT)EndifIf Keyboard.KeyDown (Key.LeftShift)cam_boost = CAMERA_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 rebuild wall", 20.0, 80.0)ShadowText (canvas, "S to toggle shadows", 20.0, 100.0)ShadowText (canvas, "B to boost boxes", 20.0, 120.0)ShadowText (canvas, "Boxes: " + boxes.Count (), 20.0, 160.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)EndJanuary 15, 2018 at 12:30 am #12865Actually, they’re (apparently) being deactivated, not removed from the simulation — if some stop responding but others are still active (you need some settled on the ground for 5 seconds, while some are still in the air, to create this situation), they can be brought back into play when hit by others.
I assume this is some default Bullet setup…
It can also be way less than 5 seconds…
(If all are settled and non-responsive, BTW, hit Space to rebuild the wall and thereby bring all boxes into play.)
January 15, 2018 at 12:39 am #12866Updated code to drop new boxes on hitting D — run the program, let boxes settle for 5 seconds, confirm no longer responding to B, then drop a box so it collides. They will now respond to B.
January 15, 2018 at 7:07 am #12868according to this a call to btCollisionObject::activate() might be needed to get the behavior you are after
January 15, 2018 at 8:02 am #12869Looks good, thanks, Simon… will see if we have access to it later!
January 15, 2018 at 11:36 pm #12873OK, fixed this (thanks, skidracer!)…
1) Added #Import “<bullet>”
2) Added body.btBody.setActivationState (bullet.DISABLE_DEACTIVATION) in PhysBox.Start ()
I suppose it’s just because it’s early days, but guess RigidBody would eventually expand to give more direct access to the rest of this stuff — it took a little bit of digging around in the source, and I don’t know what might get made private over time.
(I get that making most bodies sleep is expected, BTW!)
Reading Bullet resources online today, hope to see constraints come in next, and I notice there’s a built-in vehicle class — would be awesome to see that in mojo3d as standard (with demo, cough)!
Stiil, early days and plenty to learn with what’s there…
Code at top updated.
-
AuthorPosts
You must be logged in to reply to this topic.