Box2DFlash Video tutorial

Hey dude,

Just wanted to say thanks for the tutorial. It was really helpful me comin from APE. Certainly answed many of my questions on the basics. If you require a mirror, i can host as well no probs.

Cheers.
Hello, a PM will do fine. 🙂 Be good to play with the balls and stuff, I want to try ropes/constraints etc too. 🙂
I got ShahSoft to host it. Big thanks to him, I just updated all the links(for this topic and the wiki). So everything should be good
Great work warhell. I'm also moving over from APE to Box2D so this is very handy.
Thanks for your kind tutorial and by the way, your voice sound nice 😀 that make a good part for a Lecturer 😁

Box2DFlash rocks 😀
firstly a big thanks to warhell for this great tutorial and to ShahSoft for hosting it for the community... I would just like to bring it to your notice that the page has been offline for the past couple of days.

thanks again!
This tutorial was awesome for walking through the foundations of box2D, but I'm still slamming my head against a wall trying to make a dynamic body. According to the 2_0_2 (I thought the newest release was 2.0.1) manual, you use .CreateBody(), but .createDynamicBody or .createStaticBody() are all I can find in the b2world.as.

I've looked through the helloWorld.as but I can't figure out what the trick is. I've noticed:
density and restitution properties,
body = m_world.CreateDynamicBody(bodyDef);//but I don't know what 'body' is.
and something called PhysBox.

Here's what I've done, only three new lines(marked), but I only get a new error:
Code:
import Box2D.Collision.*; // bounding box
import Box2D.Common.Math.*; // vector
import Box2D.Dynamics.*; //bodies and world
import Box2D.Collision.Shapes.*; //shapes
import flash.display.*; //sprit class

var worldBox:b2AABB = new b2AABB();

worldBox.lowerBound.Set(-100,-100);//1 meter = 30 pixels
worldBox.upperBound.Set(100,100);

//define gravity
var gravity:b2Vec2 = new b2Vec2(0, 10);

var bSleep:Boolean = true;

var myWorld:b2World = new b2World(worldBox,gravity,bSleep);

//define shape
var boxDef:b2PolygonDef = new b2PolygonDef();
boxDef.SetAsBox(3,1);
boxDef.friction = 0.3;
boxDef.density = 0.5;///////////////////////newline/////////////////////////////////////////
boxDef.restitution = 0.3;///////////////////////newline//////////////////////////////////////

//define body
var bodyDef:b2BodyDef = new b2BodyDef();
bodyDef.userData = new Sprite();
bodyDef.userData.width = 90;
bodyDef.userData.height = 30;
bodyDef.position.Set(10,7)

addChild(bodyDef.userData);
myWorld.CreateDynamicBody(boxDef);///////////////////////newline, returning compile errors
//compile error:1067: Implicit coercion of a value of type Box2D.Collision.Shapes:b2PolygonDef to an unrelated type Box2D.Dynamics:b2BodyDef.

var body:b2Body = myWorld.CreateStaticBody(bodyDef);
body.CreateShape(boxDef);
body.SetMassFromShapes();

//debug Draw
var dbgDraw:b2DebugDraw = new b2DebugDraw();
dbgDraw.m_sprite = new Sprite();
addChild(dbgDraw.m_sprite);
dbgDraw.m_drawScale = 30.0;
dbgDraw.m_fillAlpha = 0.3;
dbgDraw.m_lineThickness = 1.0;
dbgDraw.m_drawFlags = b2DebugDraw.e_shapeBit| b2DebugDraw.e_centerOfMassBit;

myWorld.SetDebugDraw(dbgDraw);

var m_timeStep:Number = 1.0/24.0;
var m_iterations:int = 10;

addEventListener (Event.ENTER_FRAME, Update, false, 0, true);

function Update(e:Event):void{
   myWorld.Step(m_timeStep, m_iterations);
   
}


I know I'm missing something basic, but there must be others like me who want to build off this tutorial.
The Body is simply a "Body" variable, it contains all your shapes.
All your shapes in a body remain the same distance and rotation from each other (i think)

You wrote your boxDef not your bodydef.
thats why you get the error in this line:
Code:
myWorld.CreateDynamic[b]Body[/b]([b]box[/b]Def);///////////////////////newline, returning compile errors


Physbox is a custom class that is in the .fla file which just contains a picture.
This is set as the UserData because thats how the engine handles custom content?

Also I think some of your code is jumbled thats why you're getting the silly errors.
Heres what I do when creating objects:
Create my Body, in this case i'm using protag as my body.
Create a body defination.
Create a Shape Defination.
Apply Body defination to Body.
Add shape defination to Body.

Code:
var protag:b2Body; //my body, the name you call it by later
var protagBD:b2BodyDef = new b2BodyDef; //my body's defination
protagBD.position.Set(275/30, 200/30);//setting the position of the body
var protagSD:b2PolygonDef = new b2PolygonDef;//my Shape Defination
protagSD.SetAsBox(20/30, 20/30);//setting my shape as a box and defining HALF it's height and width.
protagSD.density = 0.5;//Setting the density
protagSD.restitution = 0.3;//settign restitution or bouncyness
protag = m_world.CreateDynamicBody(protagBD);//applying my body Defination to my body
protag.CreateShape(protagSD);//creating my shape into my body
protag.SetMassFromShapes;//setting the mass from my shape


Important things to note, size and position of your bodies and shapes is NOT in pixels, it's 30 times the number you put in since Box2d works in meters and the scale is 30px to 1 meter.
Also if you're using version 2.00 like me you should download the C++ version and use the manual from there.
Thanks Yager 🙂

This line was a converted from another tutorial on dynamic objects and I guess I got confused 😛,
Code:
myWorld.CreateDynamic[b]Body[/b]([b]box[/b]Def);///////////////////////newline, returning compile errors

It turns out the line is completely un-needed and only gets in the way. While reading your post I stumbled onto the line that I was trying to find:
Code:
var body:b2Body = myWorld.CreateStaticBody(bodyDef);

This is why my dynamic object isn't working (duh). I've eliminated the 'newline' and simply changed the body declaration to .CreateDynamicBody().

Finally, it works.

For other newbies like me, to change this from a static object to a dynamic one, simply:
1) assign density and restitution values to the shape.
2) change the body variable to a .CreateDynamicBody() (instead of .CreateStaticBody).
The result is a square that simply falls off the screen, but you can build from there 🙂


Thanks again for the reply. 🙂
No problem, i'm currently working out Collision filtering...
Yea I also started about a month ago and the first step is really hard.
If you want to see my progress of my project check out my blog

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