Box2D Sprites

Hi, I'm trying to figure out how to use Box2D, and I can't figure out how to get graphics to draw instead of the debug sprites. In my head, it sounds really simple: I just need it to say: [code] graphics.beginFill(0x00FF00); graphics.drawCircle(x, y, ra…

Viewing 1 to 8 (8 Total)
Box2D Sprites

ohblahitsme

ohblahitsme
Total Posts: 15
Joined: May 14, 2012

Hi, I'm trying to figure out how to use Box2D, and I can't figure out how to get graphics to draw instead of the debug sprites. In my head, it sounds really simple: I just need it to say:
graphics.beginFill(0x00FF00);
graphics.drawCircle(x, y, radius);

and it should work, but it's not really working. I think it's because it's not updating the drawing even when the Box2D object moves, but I don't know how to make it do that. Any ideas?

Tags:
Posted on May 26, 2012 at 12:52 AM

arlg

arlg
Total Posts: 11
Joined: November 19, 2011

Re: Box2D Sprites

Hi, yep, it's not updating the drawing when the box2d physic object move.

You first have to attach your sprite ( that can be your graphics.drawCircle ) to the b2body object, when you create it, with the setUserData method :

body.setUserData(yourSprite);

and then, you have to update the sprite position in the box2d loop.

What I do is to push all my bodies in an Array, and so I can acces to them everywhere.

So you will have in your box2d loop something like :

//b2BodyElements is my array of bodies
for (myBody in b2BodyElements)
{
//If the body has an attached Sprite
if(myBody.getUserData() != null && Std.is(cast(myBody.getUserData(), Sprite), Sprite))
{
//Sets the x, y, and rotation of the sprite
generalBody.getUserData().x = generalBody.getPosition().x ;
generalBody.getUserData().y = generalBody.getPosition().y ;
generalBody.getUserData().rotation = generalBody.getAngle();
}
}


This is a simple render loop for box2d.

When I started box2d for haxe I followed tutorials on those websites :
http://blog.allanbishop.com/
http://www.emanueleferonato.com/...

(be careful, sometimes they are not for 2.1a version and there are little differences )

Posted on May 27, 2012 at 2:56 PM

ohblahitsme

ohblahitsme
Total Posts: 15
Joined: May 14, 2012

Re: Box2D Sprites

Thanks for the websites! Unfortunately, the sprite still isn't following the box2d debug sprite. Right now I have two classes. One is called World.hx and it holds the information for the ground object as well as the EnterFrame event handler. This is my EnterFrame function:


private function this_onEnterFrame(event:Event):Void {

MyWorld.step(1 / 30, 10, 10);
MyWorld.clearForces();
MyWorld.drawDebugData();
player.body.getUserData().x = player.body.getPosition().x;
player.body.getUserData().y = player.body.getPosition().y;

}


Then in my Player.hx file, I have this:


public function defineCharacter(x:Float, y:Float, radius:Float):Void {

// This contains all the box2D stuff
// PHYSICS_SCALE is defined in World.hx
var bodyDef = new B2BodyDef();
bodyDef.position.set(x * World.PHYSICS_SCALE, y * World.PHYSICS_SCALE);

bodyDef.type = B2Body.b2_dynamicBody;

var circle = new B2CircleShape(radius * World.PHYSICS_SCALE);

var fixtureDef = new B2FixtureDef();
fixtureDef.shape = circle;

var body = World.MyWorld.createBody(bodyDef);
body.createFixture(fixtureDef);
body.setUserData(drawCharacter(x, y, radius));

}


I'm not really sure what I'm still doing wrong.

Posted on May 27, 2012 at 4:43 PM

arlg

arlg
Total Posts: 11
Joined: November 19, 2011

Re: Box2D Sprites

Does the sprite moves a little ?

body.getUserData() returns the body position in meters, but not in pixels, to you have to multiply this value to get your sprite moving on your stage.

For example if your PHYSICS_SCALE is 1 / 30, you will have to multiply body.getPosition() by 30 to get your sprite follow its debug sprite.

In your case :

player.body.getUserData().x = player.body.getPosition().x *METERS_TO_PIXELS; // meters to pixels equals 30
player.body.getUserData().y = player.body.getPosition().y *METERS_TO_PIXELS;

Posted on May 28, 2012 at 9:46 AM

ohblahitsme

ohblahitsme
Total Posts: 15
Joined: May 14, 2012

Re: Box2D Sprites

No, it's not moving at all. I multiplied it by my physics factor but still no dice. Any other ideas?

Posted on May 30, 2012 at 11:14 AM

Jon

Jon
Total Posts: 272
Joined: March 08, 2012

Re: Box2D Sprites

Turn on debug drawing to verify that your simulation's working the way you think it is. For example...

var sprite = new Sprite();
var debugDrawer = new B2DebugDraw();
debugDrawer.setSprite(sprite);
debugDrawer.setDrawScale(10);
debugDrawer.setFlags(B2DebugDraw.e_shapeBit);
world.setDebugDraw(debugDrawer);
addChild(sprite);

Posted on May 30, 2012 at 10:23 PM

Aymeric

Aymeric
Total Posts: 10
Joined: March 27, 2012

Re: Box2D Sprites

Hi, it's off of topic but I've a bug with Box2D NME with collision detection/management.

It seems that the beginContact & endContact listeners are fired all the time if a dynamic body is on a static body (like a Hero on a platform), whereas it fires only once if there are 2 dynamics bodies. I'm spammed by all the start/end/start/end in the console...

Any ideas ?

Posted on May 31, 2012 at 7:06 AM

ohblahitsme

ohblahitsme
Total Posts: 15
Joined: May 14, 2012

Re: Box2D Sprites

Turn on debug drawing to verify that your simulation's working the way you think it is. For example...

var sprite = new Sprite();
var debugDrawer = new B2DebugDraw();
debugDrawer.setSprite(sprite);
debugDrawer.setDrawScale(10);
debugDrawer.setFlags(B2DebugDraw.e_shapeBit);
world.setDebugDraw(debugDrawer);
addChild(sprite);


I have debug draw on and it is doing what I want it to (the debug circle is falling and stopping at the ground)

Posted on May 31, 2012 at 12:04 PM