Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Box 2D question? — Gideros Forum

Box 2D question?

plamenplamen Member
edited November 2012 in General questions
Hi guys, I am having trouble finding "delete world" function in box2d. Here is from box2d.org docs:

b2World* myWorld = new b2World(gravity, doSleep);
... do stuff ...
delete myWorld;

What should be Gideros version of the last line? Any help?

I might need more coffee this morning, i guess :-/

Likes: amaximov

Tagged:
+1 -1 (+1 / -0 )Share on Facebook

Comments

  • ar2rsawseenar2rsawseen Maintainer
    Accepted Answer
    @plamen I think you just need to nil it out
    local world = b2.World.new(0, 10, true)
    world = nil
    Additionally you'd probably need to remove enterframe event where you are performing steps etc. And better deleting all bodies.

    But all in all nil in lua is similar to delete
  • plamenplamen Member
    edited November 2012
    @ar2rsawseen Are you sure? I know about nil in Lua but box2d is a bit different story and as you see, you have to destroy body with special method not just make it nil if you want to free resources used by the physics framework. The case is i destroyed all the bodies in the level but i need to destroy the world also...
  • ar2rsawseenar2rsawseen Maintainer
    Accepted Answer
    Well but there is no specific method for removing a world in C library either.
    I believe nil should do the trick.

    Additionally you can unload the box2d library if you won't be using it anymore using the unload.

    But we can wait for @atilim or @OZApps to confirm it :)

    Likes: plamen

    +1 -1 (+1 / -0 )Share on Facebook
  • atilimatilim Maintainer
    edited November 2012 Accepted Answer
    To extend what @ar2rsawseen said: When a world object is collected, then it's deleted. But bodies and joints also hold a reference to the world object. Therefore, when a world object and its bodies and its joints are not accessible, then they will be collected and deleted on the next garbage collection cycle.

    Here is a simple example:
    require "box2d"
     
    world = b2.World.new(0, 9.8)
    body = world:createBody{}
     
    world.proxy = newproxy(true)
    getmetatable(world.proxy).__gc = function() print("world is collected!") end
     
    world = nil
    body = nil
     
    collectgarbage()
    If you run this code, you'll see "world is collected!" message. But if you comment out the line body=nil, you won't see the message. (and here there is no need to call destroyBody)

    Likes: plamen

    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.