Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Asking For Box2d tips — Gideros Forum

Asking For Box2d tips

neverjoyneverjoy Member
edited May 2016 in General questions
Hi all, in the past days i am learning with box2d.
Its been fun testing objects and stuff.

Now, i have a question, what is your golden rule in working with box2d?
What points you constantly remind yourself when working with it?
Methods to efficiently code and avoid fps loss?
I would also like to target low end phones, what should I keep in mind?
Tips you can give to a box2d noob like me?



Ps: how to represent a explosion force in box2d?



Thanks for the time reading this.

Comments

  • antixantix Member
    edited May 2016 Accepted Answer
    I have played a little with box2d and it's fun. Check out this site.. https://www.iforce2d.net/b2dtut/

    Its not LUA code but you'll get the gist of what hes on about. There is a page there where the guy uses box2d particles to simulate explosions, very cool stuff.

    Likes: neverjoy

    +1 -1 (+1 / -0 )Share on Facebook
  • @antix thanks for pointing me to that site, until now, i usually avoid other tutorials because they are not lua and not related to gideros, but it seems that the concepts apply universally. thanks again!
  • john26john26 Maintainer
    Accepted Answer
    Gideros pretty much implements Box2D directly. So anything you find in other tutorials will apply especially tutorials on running Box2D from its native C++. Gideros does not attempt to simplify Box2D the way other engines do (like Corona) so the learning curve can be a bit steep but you get access to the whole library. There are also many Gideros Box2D tutorials on this page

    http://giderosmobile.com/guide

    some tips:
    1) You can't destroy or create box2D bodies within the BEGIN_CONTACT, END_CONTACT listeners, eg,

    world:addEventListener(Event.BEGIN_CONTACT,collideBEGIN)

    you should not try to destroy bodies within the collideBEGIN function. BUT this does not mean you need to wait till bodies are out of contact before destroying them, you just can't destroy them within the actual listener. Eg if you have 2 crates stacked on top of each other there will be a BEGIN_CONTACT when they first touch. But even if they remain in contact you can destroy either one of them later on within an ENTER_FRAME listener.

    2) You must remember to actually destroy bodies when going to the next level. It's not enough to destroy the corresponding Sprites. For instance its common to have a Sprite eg ball=Bitmap.new(Texture.new("ball.png")) and then add a body reference as a field in the ball table, ball.body=world:createBody(..). Then you get rid of the ball with
    ball:removeFromParent()
    ball=nil
    now you've kind of created a memory leak because there is no easy way to access the body. (probably you can step through all bodies in the world but which belongs to ball?)

    You should of course do
    ball:removeFromParent()
    world:destroyBody(ball.body)
    ball=nil

    Likes: neverjoy

    +1 -1 (+1 / -0 )Share on Facebook
  • neverjoyneverjoy Member
    edited May 2016
    @john26 thanks sir for these gems espcially the link... i didn't know about this destroy on contact stuff so it saves me from scratching my hair until it goes bald because of unknown errors... many thanks sir!


    edit: i have a question.. in my case i wanted to create a body that does destruction physics(body is broken apart into smaller pieces on impact), which way is the right approach? making small body components and attach them together by weld joint and destroy that joint on impact? or create one body with multiple fixtures and then on impact, recreate smaller bodies based on created multiple fixtures?

    also i dont know how to manage their sprites(does an object with multiple fixtures can have one sprite? etc.)

    i dont quite know the purpose of creating multiple fixtures and what scenarios creating them best applies.

  • totebototebo Member
    To break up a body, this would be my approach:

    1. Create the unbroken body and add to Box2D
    2. Create the broken body pieces as a separate body and disable it from collision detection (with setActive(false) if memory serves, or exclude it from collision detection in some other way).
    3. When the first body needs to beak up, exclude it from collisions, move the second body to its position and location, then activate the second body

    This would be quite efficient, because adding and removing bodies and fixtures are hard work for the CPU.
    My Gideros games: www.totebo.com
Sign In or Register to comment.