Hello all.
Im doing a small project for a friend and using Box2D for the first time.
All was going very fine. I studied box2d a bit and then found this neat class http://actionsnippet.com/?page_id=1391 which makes a lot easier a pratical to create the boxes and render the world. I was able to create dragable falling boxes in no time.
But when I needed to do something a little more sophisticated, Im found an issue that I have no idea how to solve.
The project is just a portifolio, using box2d to display the works as a constant stream of falling boxes. No big deal, is almost done and you can see it here:
http://www.fabianodequeiroz.com/
Boxes keep falling at a bar at the bottom of the screen. When the user clicks at a box, the project is open. Here comes the "elevator": I have to lift the bar to the middle of the screen.
So I create this class "Bar". It is like this:
Is very simple: it just creates the box and sets an y property so I can use with TweenLite to move. Sp methods moveUp and moveDown are called to move the bar, but the actual moving is done here:
Well, to my own surprise, it works. The bar moves, moving the boxes above it. And it ends in the exact position it is supposed to. Hooray for me.
Except that is not quite right. As you can see in the site, sometimes, when the bar goes up, some of the boxes above it will slide through the bar instead of being lifted with it. Moving down, though, works perfectlly.
Im obviously doing someting or everything wrong here, but with my poor knowledge of box2d, I dont even know where to start. So, please, help me here.
Am I doing this elevator properly? If not, what would be the correct way of doing it? Why are the boxes passing through the bar?
Any help, hints, tips, information, will be much appreciated.
Thank you.
Im doing a small project for a friend and using Box2D for the first time.
All was going very fine. I studied box2d a bit and then found this neat class http://actionsnippet.com/?page_id=1391 which makes a lot easier a pratical to create the boxes and render the world. I was able to create dragable falling boxes in no time.
But when I needed to do something a little more sophisticated, Im found an issue that I have no idea how to solve.
The project is just a portifolio, using box2d to display the works as a constant stream of falling boxes. No big deal, is almost done and you can see it here:
http://www.fabianodequeiroz.com/
Boxes keep falling at a bar at the bottom of the screen. When the user clicks at a box, the project is open. Here comes the "elevator": I have to lift the bar to the middle of the screen.
So I create this class "Bar". It is like this:
Code:
public class Bar extends EventDispatcher
{
public function Bar(world:QBoxWorld)
{
this._world = world;
this.create();
}//contructor
//// MEMBERS ////
private var _world:QBoxWorld;
private var _box:BoxObject;
//// PROPERTIES ////
public function set y(val:Number):void
{
var vec:b2Vec2 = new b2Vec2(this._box.x, val * QBoxWorld.p2m);
this._box.body.SetXForm(vec, 0);
}//set y
public function get y():Number
{
return (this._box.userData as Sprite).y;
}//get y
//// METHODS ////
protected function create():void
{
var gwidth:Number = MainView.inst.divRef.width;
var gx:Number = (MainView.inst.stage.stageWidth - gwidth) / 2;
var gpos:Point = QBoxWorld.getBoxMPos(gx, MainView.inst.divRef.y, gwidth, MainView.inst.divRef.height);
this._box = this._world.addBox({x:gpos.x, y:gpos.y, width:gwidth * QBoxWorld.p2m, height:MainView.inst.divRef.height * QBoxWorld.p2m, density:0, friction:1, fillColor:0x242527, lineAlpha:0}) as BoxObject;
MainView.inst.stage.addEventListener(Event.RESIZE, onResize);
}//create
public function moveUp(callback:Function):void
{
TweenLite.to(this, 2, {y:422, onComplete:callback});
}//moveUp
public function moveDown(callback:Function):void
{
TweenLite.to(this, 1, {y:733, onComplete:callback});
}//moveUp
}//Bar
{
public function Bar(world:QBoxWorld)
{
this._world = world;
this.create();
}//contructor
//// MEMBERS ////
private var _world:QBoxWorld;
private var _box:BoxObject;
//// PROPERTIES ////
public function set y(val:Number):void
{
var vec:b2Vec2 = new b2Vec2(this._box.x, val * QBoxWorld.p2m);
this._box.body.SetXForm(vec, 0);
}//set y
public function get y():Number
{
return (this._box.userData as Sprite).y;
}//get y
//// METHODS ////
protected function create():void
{
var gwidth:Number = MainView.inst.divRef.width;
var gx:Number = (MainView.inst.stage.stageWidth - gwidth) / 2;
var gpos:Point = QBoxWorld.getBoxMPos(gx, MainView.inst.divRef.y, gwidth, MainView.inst.divRef.height);
this._box = this._world.addBox({x:gpos.x, y:gpos.y, width:gwidth * QBoxWorld.p2m, height:MainView.inst.divRef.height * QBoxWorld.p2m, density:0, friction:1, fillColor:0x242527, lineAlpha:0}) as BoxObject;
MainView.inst.stage.addEventListener(Event.RESIZE, onResize);
}//create
public function moveUp(callback:Function):void
{
TweenLite.to(this, 2, {y:422, onComplete:callback});
}//moveUp
public function moveDown(callback:Function):void
{
TweenLite.to(this, 1, {y:733, onComplete:callback});
}//moveUp
}//Bar
Is very simple: it just creates the box and sets an y property so I can use with TweenLite to move. Sp methods moveUp and moveDown are called to move the bar, but the actual moving is done here:
Code:
public function set y(val:Number):void
{
var vec:b2Vec2 = new b2Vec2(this._box.x, val * QBoxWorld.p2m);
this._box.body.SetXForm(vec, 0);
}//set y
{
var vec:b2Vec2 = new b2Vec2(this._box.x, val * QBoxWorld.p2m);
this._box.body.SetXForm(vec, 0);
}//set y
Well, to my own surprise, it works. The bar moves, moving the boxes above it. And it ends in the exact position it is supposed to. Hooray for me.
Except that is not quite right. As you can see in the site, sometimes, when the bar goes up, some of the boxes above it will slide through the bar instead of being lifted with it. Moving down, though, works perfectlly.
Im obviously doing someting or everything wrong here, but with my poor knowledge of box2d, I dont even know where to start. So, please, help me here.
Am I doing this elevator properly? If not, what would be the correct way of doing it? Why are the boxes passing through the bar?
Any help, hints, tips, information, will be much appreciated.
Thank you.