Jun 062011
 

If you’ve ever used Flashpunk or Box2D, you’ll know that they’re pretty amazing tools for flash game development. However, they don’t play all that nicely together.

To fix this, I’ve put together Box2FP, a library which combines these two libraries. Below is a video tutorial of how it works (please note that you should read the documentation for Flashpunk and Box2D before using Box2FP).

  • Guest

    Cool, I love video tutorial. But can you upload 720p version ?

    • http://www.facebook.com/pdyxs Paul Sztajer

      Working on it… it’s proving hard to do (I think I used the wrong software here…)

    • http://www.facebook.com/pdyxs Paul Sztajer

      …and done! 720p version is now up

      • Guest

        Wow that’s fast. Thanks, now I can see what you type clearly

  • http://Danikgames.com Danik

    I had a lot of problems getting my body to move. The problem was _pause in Box2DWorld was true by default. By calling unpause() it worked.

    • http://www.facebook.com/pdyxs Paul Sztajer

      I was sure that I’d fixed this in the last update… did you update the source from github?

      • http://Danikgames.com Danik

        I was using this one:
        https://github.com/pdyxs/Box2FP/blob/master/fabulaexmachina/box2fp/Box2DWorld.as

        I see now that the “fabulaexmachina.box2fp” package is older, so I should be using “net.box2fp” instead, where it is fixed. I didn’t realise they were the “same thing” so to speak.

        • http://www.facebook.com/pdyxs Paul Sztajer

          Right… seems like I fail at git. The fabulaexmachina package was supposed to be deleted last time I updated, but it didn’t – thanks for pointing that out!

  • http://Danikgames.com Danik

    How would you recommend to go about adding entities from other entities’ logic?
    If I’m right, new entities’ bodies are created the next world update, so if I create an entity in another entity’s update(), I need to wait until after the next world update before I can access it’s body? (to add joints)

    • http://www.facebook.com/pdyxs Paul Sztajer

      Yeah that’s corrrect. It probably depends on your application, but I’d say the easiest way is to create the joint in the new entity’s ‘added’ function. So in the constructor of the 2nd object, you have an argument that is the entity to create a joint to. Chuck that into a variable, and then create the joint in added(), after you call super.added().

      It’ll look something something like this (syntax might be wrong here, I’m going off memory):
      public class ToBeJoined extends Box2dEntity {
      ToBeJoined(x, y, w, h, joinTo:Box2dEntity = null) {
      super(…);
      _joinTo = joinTo;
      }

      override public function added():void {
      super.added();
      if (_joinTo != null) {
      //make your joint
      }
      }
      }

      • http://Danikgames.com Danik

        I see, thanks!
        There is one problem I can think of though. Since the world simulates one step, the positions for the joints might not be accurate any longer. (say I create a new entity right at the players position while he is moving, then add the joint one step later)
        Maybe it would be good to have some function to “flush” the creation of the bodies before the next world update?

        • http://www.facebook.com/pdyxs Paul Sztajer

          Hmm that’s a good point. I’m not 100% sure how to get flashpunk to do that but I’ll think on it (suggestions welcome), what you could do in the meantime is just sync the positions of the bodies in the ‘added’ function before you make the joint.

          So in the added function above: body.setPosition(_joinTo.body.getPosition());

          • http://Danikgames.com Danik

            Maybe you could move the creation of the b2Body to it’s own method, that is called by added() and can be called by the user, like so:

            http://pastie.org/2046884

            Or maybe create it in the constructor by default if that doesn’t lead to problems.

          • http://www.facebook.com/pdyxs Paul Sztajer

            Good idea – I’ve changed it a bit to make it so that the create method itself checks, and so that you have to send it the b2world yourself (this is what makes putting it in the constructor hard, so you’d need to send the constructor the b2world to create it in the constructor, if that makes sense). Let me know if that’s what you were after.

          • http://Danikgames.com Danik

            Hmm, I thought I replied before. :S

            Anyhow, thanks for the response, looks like this is exactly what I’m after.
            Also thanks for this library, it’s pretty awesome!

          • Anonymous

            No problems – make sure you send us a link to what you make with it!

  • Ramen Noodles

    I curious if you plan to make more tutorials. There are a lot of things I want to know how to do using Box2d and Flashpunk. I don’t know if anyone in the flashpunk community use this library but from the amount of threads on the site its either easy for everyone, except me, to use or no one uses it :/

    Anyway I’m trying to find out how fixtures work and connecting one entity to another when selected consecutively with the mouse. Getting mouse functionality seems confusing to me with box2d.

    Also I’m having a performance problem, when world reaches 30 – 40 entities my framerate begins to drop. Ideally I would like to have at least 200 entities on screen and I’m sure as to why my game lags compared to other box2d projects having over 1000 objects without any framerate drop. I’m trying to keep a steady 60fps. Help with this would really great >_<

  • http://www.facebook.com/Teonnyn Merlin Matthews

    I’d like to get the Box2DFP Sample code if at all possible, the “Get Sample Code” link no longer works and takes you to a playtesting signup page.

  • Pingback: Linux + FlashPunk + Box2D « ByteCrow – LUTO

  • Pingback: Linux + FlashPunk + Box2D « ByteCrow – LUTO

  • ivve

    Great library, thanks!
    But I get a strange problem, running your example Box2FPSample – the image, attached to the circle, is rotating different way – at some distance from the circle and by another angle. I don’t change anything in your sample, but the result is different than it looks in your tutorial. Do you have any idea about this bug? Thanks!

  • http://twitter.com/#!/imcg imcg

    Awesome. Thanks. Just what I needed :)

  • Sauli Koti

    I can’t see any submit button for gtin the sample code. ( “To get the sample code for this example, enter your name and email address and click ‘Get Sample Code’.”)

  • Pingback: Flashpunk and Box2D – Unlogic