sarka86
04-20-2009, 11:27 AM
Hi!
Thank to newBlack I accessed the magic world of box2D for Flash. Unfortunately it's a hard world :) I read documentation (which I only found about C++ programming) and tutorials but I still have many doubts.
What I'm trying to build is a flash game: there are 3 walls (bottom, left and right) and balls fall from the top bounce on the walls and collide with each other till they stop. If they're near a ball with the same color they disappear. But this is a further improvement...
I'm trying to build the first part using box2D, starting from a tutorial by Emanuele Feronato (http://www.emanueleferonato.com/2009/01/27/box2d-tutorial-for-the-absolute-beginners/). Too bad I'm not getting what I want :)
First of all I have no idea about how to add the 2nd and 3rd wall. Should I create 2 other solid bodies? Or are there better ways (such as a polygon with point shaped like the union of the 3 walls)?
Then it's about coding. I debugged all the syntax errors but still can't find why the balls are not falling but just "exploding" through the stage. I guess I did some mistake with the gravity in the world, but I can't find it. If someone can be so nice to give a look at it here it is :)
addEventListener(Event.ENTER_FRAME, Update, false, 0, true);
var worldAABB:b2AABB = new b2AABB();
worldAABB.lowerBound.Set(-100.0, -100.0);
worldAABB.upperBound.Set(100.0, 100.0);
var gravity:b2Vec2 = new b2Vec2(0.0, 10.0);
var doSleep:Boolean = true;
m_world = new b2World(worldAABB, gravity, doSleep);
var body:b2Body;
var bodyDef:b2BodyDef;
var circleDef1:b2CircleDef;
var circleDef2:b2CircleDef;
var boxDef:b2PolygonDef;
bodyDef = new b2BodyDef();
bodyDef.position.Set(10, 12);
boxDef = new b2PolygonDef();
boxDef.SetAsBox(30, 3);
boxDef.friction = 0.3;
boxDef.density = 0;
bodyDef.userData = new PhysGround();
bodyDef.userData.width = 30 * 2 * 30;
bodyDef.userData.height = 30 * 2 * 3;
addChild(bodyDef.userData);
body = m_world.CreateBody(bodyDef);
body.CreateShape(boxDef);
body.SetMassFromShapes();
for (var i:int = 1; i < 10; i++){
bodyDef = new b2BodyDef();
bodyDef.position.x = Math.random() * 15 + 5;
bodyDef.position.y = Math.random() * 10;
var rX:Number = 0.7*2*30;
var rY:Number = 0.5*2*30;
// redCircle
if (Math.random() < 0.5){
circleDef2 = new b2CircleDef();
circleDef2.radius = rY;
circleDef2.density = 1.0;
circleDef2.friction = 0.5;
circleDef2.restitution = 0.2;
bodyDef.userData = new PhysCircle2();
bodyDef.userData.width = rY;
bodyDef.userData.height = rY;
body = m_world.CreateBody(bodyDef);
body.CreateShape(circleDef2);
}
// blueCircle
else {
circleDef1 = new b2CircleDef();
circleDef1.radius = rX;
circleDef1.density = 1.0;
circleDef1.friction = 0.5;
circleDef1.restitution = 0.2;
bodyDef.userData = new PhysCircle();
bodyDef.userData.width = rX;
bodyDef.userData.height = rX;
body = m_world.CreateBody(bodyDef);
body.CreateShape(circleDef1);
}
body.SetMassFromShapes();
addChild(bodyDef.userData);
}
}
public function Update(e:Event):void{
m_world.Step(m_timeStep, m_iterations);
// Go through body list and update sprite positions/rotations
for (var bb:b2Body = m_world.m_bodyList; bb; bb = bb.m_next){
if (bb.m_userData is Sprite){
bb.m_userData.x = bb.GetPosition().x * 30;
bb.m_userData.y = bb.GetPosition().y * 30;
bb.m_userData.rotation = bb.GetAngle() * (180/Math.PI);
}
}
}
public var m_world:b2World;
public var m_iterations:int = 10;
public var m_timeStep:Number = 1.0/30.0;
physCircle and physCircle2 are 2 symbols I created which contain the balls.
Thank to newBlack I accessed the magic world of box2D for Flash. Unfortunately it's a hard world :) I read documentation (which I only found about C++ programming) and tutorials but I still have many doubts.
What I'm trying to build is a flash game: there are 3 walls (bottom, left and right) and balls fall from the top bounce on the walls and collide with each other till they stop. If they're near a ball with the same color they disappear. But this is a further improvement...
I'm trying to build the first part using box2D, starting from a tutorial by Emanuele Feronato (http://www.emanueleferonato.com/2009/01/27/box2d-tutorial-for-the-absolute-beginners/). Too bad I'm not getting what I want :)
First of all I have no idea about how to add the 2nd and 3rd wall. Should I create 2 other solid bodies? Or are there better ways (such as a polygon with point shaped like the union of the 3 walls)?
Then it's about coding. I debugged all the syntax errors but still can't find why the balls are not falling but just "exploding" through the stage. I guess I did some mistake with the gravity in the world, but I can't find it. If someone can be so nice to give a look at it here it is :)
addEventListener(Event.ENTER_FRAME, Update, false, 0, true);
var worldAABB:b2AABB = new b2AABB();
worldAABB.lowerBound.Set(-100.0, -100.0);
worldAABB.upperBound.Set(100.0, 100.0);
var gravity:b2Vec2 = new b2Vec2(0.0, 10.0);
var doSleep:Boolean = true;
m_world = new b2World(worldAABB, gravity, doSleep);
var body:b2Body;
var bodyDef:b2BodyDef;
var circleDef1:b2CircleDef;
var circleDef2:b2CircleDef;
var boxDef:b2PolygonDef;
bodyDef = new b2BodyDef();
bodyDef.position.Set(10, 12);
boxDef = new b2PolygonDef();
boxDef.SetAsBox(30, 3);
boxDef.friction = 0.3;
boxDef.density = 0;
bodyDef.userData = new PhysGround();
bodyDef.userData.width = 30 * 2 * 30;
bodyDef.userData.height = 30 * 2 * 3;
addChild(bodyDef.userData);
body = m_world.CreateBody(bodyDef);
body.CreateShape(boxDef);
body.SetMassFromShapes();
for (var i:int = 1; i < 10; i++){
bodyDef = new b2BodyDef();
bodyDef.position.x = Math.random() * 15 + 5;
bodyDef.position.y = Math.random() * 10;
var rX:Number = 0.7*2*30;
var rY:Number = 0.5*2*30;
// redCircle
if (Math.random() < 0.5){
circleDef2 = new b2CircleDef();
circleDef2.radius = rY;
circleDef2.density = 1.0;
circleDef2.friction = 0.5;
circleDef2.restitution = 0.2;
bodyDef.userData = new PhysCircle2();
bodyDef.userData.width = rY;
bodyDef.userData.height = rY;
body = m_world.CreateBody(bodyDef);
body.CreateShape(circleDef2);
}
// blueCircle
else {
circleDef1 = new b2CircleDef();
circleDef1.radius = rX;
circleDef1.density = 1.0;
circleDef1.friction = 0.5;
circleDef1.restitution = 0.2;
bodyDef.userData = new PhysCircle();
bodyDef.userData.width = rX;
bodyDef.userData.height = rX;
body = m_world.CreateBody(bodyDef);
body.CreateShape(circleDef1);
}
body.SetMassFromShapes();
addChild(bodyDef.userData);
}
}
public function Update(e:Event):void{
m_world.Step(m_timeStep, m_iterations);
// Go through body list and update sprite positions/rotations
for (var bb:b2Body = m_world.m_bodyList; bb; bb = bb.m_next){
if (bb.m_userData is Sprite){
bb.m_userData.x = bb.GetPosition().x * 30;
bb.m_userData.y = bb.GetPosition().y * 30;
bb.m_userData.rotation = bb.GetAngle() * (180/Math.PI);
}
}
}
public var m_world:b2World;
public var m_iterations:int = 10;
public var m_timeStep:Number = 1.0/30.0;
physCircle and physCircle2 are 2 symbols I created which contain the balls.