Box2d Flash AS3 (1.4.3 released)

Yes, I understand that this is a physics engine. That's why I wonder if anyone has gone a step further to create a gamelib where the physics is one component, amongst e.g. pathfinding etc.

Anyways this is my first shot:

Quote:
//======================
// Draw DisplayObject
//======================
public function DrawDisplayObject(bb:b2Body):void{
// get actor
var dispObj:DisplayObject = DisplayObject(bb.m_userData);
if(dispObj==null){return;}
// add actor if needed
if(!m_sprite.getChildByName(dispObj.name)){
m_sprite.addChild(dispObj);
}
// position actor
dispObj.x = bb.m_position.x * m_physScale;
dispObj.y = bb.m_position.y * m_physScale;
dispObj.rotation = bb.m_rotation;
}


This works great for the positioning, but the rotation is off. Any ideas?
Hey Ygryggil,

The rotation is in radians, you will need to convert it to degrees like so:
Code:
dispObj.rotation = bb.m_rotation * (180.0 / Math.PI);
skatehead wrote:
The rotation is in radians, you will need to convert it to degrees like so:
Code:
dispObj.rotation = bb.m_rotation * (180.0 / Math.PI);

the problem is that in flash, the movieclip's reference point is the left upper corner, but in box2D it's the body's center
glezmen wrote:
the problem is that in flash, the movieclip's reference point is the left upper corner, but in box2D it's the body's center


This works fine with my code. I have an Actor class that centers the DisplayObject:

Code:
package{
import flash.display.Sprite;
import flash.display.DisplayObject;

public class Actor extends Sprite{
   
   public function Actor(name:String, dispObj:DisplayObject){
      this.name = name;
      this.addChild(dispObj);
      dispObj.x = -(dispObj.width/2);
      dispObj.y = -(dispObj.height/2);
   }   
//end
}
}


Then you add the actor like this:
Code:
var body:b2Body = m_world.CreateBody(bd);
body.m_userData = new Actor("char", myDisplayObject);


And the tweaked function with the correct rotation. Thanks btw!

Code:
public function DrawDisplayObject(bb:b2Body):void{
         // get actor
         var dispObj:DisplayObject = DisplayObject(bb.m_userData);
         if(dispObj==null){return;}         
         // add actor if needed
         if(!m_sprite.getChildByName(dispObj.name)){
            m_sprite.addChild(dispObj);
         }
         // position actor
         dispObj.x = bb.m_position.x * m_physScale;
         dispObj.y = bb.m_position.y * m_physScale;
         //dispObj.rotation = bb.m_rotation;
         dispObj.rotation = bb.m_rotation * (180.0 / Math.PI);
      }
I want to make a 2D platformer and I'm looking at making good movement for the character. The following questions come up:

1. The character is a rectangle, and it shouldn't be rotating that much. As it stands the character goes bouncing around as soon as things topple on him. Can you turn rotation for a b2Body off? Or is there another approach?

2. How do you propose movement, as I'm using a walk cycle. So far I'm using SetLinearVelocity(), but it's behaving like more like a car than a humanoid. Any ideas?

3. What approach should I use for jumping movement?

Thanks. 🙂
You can use bodyDef.preventRotation = true; to stop your character from rotating.

There's some more info about character movement in this thread
http://www.box2d.org/forum/viewtopic.php?f=3&t=160
Yet again thanks. That's a good thread, especially if the author there takes time to share his results. 😉

Have you made any tests if drawing or copypixeling bitmaps is more efficient than adding display objects?
I've found this in the Box2D documentation:

Quote:
Fast moving objects in Box2D are called bullets. You should decide based on your game design if something should be a bullet or not. If you decide a body should be treated as a bullet, use the following setting.
Code:
bodyDef.isBullet = true;



but I cannot find it in Box2DFlashAS3. Was it removed in newer version or is not yet implemented to Flash?
Hi glezmen,

The isBullet property is only available in the Continuous Physics Prototype of Box2D, which has not been ported to Flash.
I'm waiting for 2.0 before implementing the updates.

Thanks
skatehead wrote:
The isBullet property is only available in the Continuous Physics Prototype of Box2D, which has not been ported to Flash.
I'm waiting for 2.0 before implementing the updates.


thanks for the answer!

Original thread: https://box2d.org/forum/viewtopic.php?t=47