hello hello!
ive started looking into box2d one day ago and i'm still confused about many things, even the diferent versions make it harder to find proper explanations (besides that i'm probably just dumb 🙂 )
i'm planning to do a mario-style game, and i've set up my scene, but moving the hero is proving to be problematic. the thing is that he doesnt jump up when i apply an impulse, and even weirder thing is that he moves the same no matter what b2Vec2 value i put there...
i also cant figure out how to set the weight (that would be density) to bodies... they all fall with the same speed
or... why, if a box falls on the edge of another box, it doesnt rotate...
here's the code - any reply would be useful, just getting to talk about this is a good way of learning 🙂
thank you!
ive started looking into box2d one day ago and i'm still confused about many things, even the diferent versions make it harder to find proper explanations (besides that i'm probably just dumb 🙂 )
i'm planning to do a mario-style game, and i've set up my scene, but moving the hero is proving to be problematic. the thing is that he doesnt jump up when i apply an impulse, and even weirder thing is that he moves the same no matter what b2Vec2 value i put there...
i also cant figure out how to set the weight (that would be density) to bodies... they all fall with the same speed
or... why, if a box falls on the edge of another box, it doesnt rotate...
here's the code - any reply would be useful, just getting to talk about this is a good way of learning 🙂
thank you!
Code:
package {
import Box2D.Collision.b2AABB;
import Box2D.Collision.Shapes.b2MassData;
import Box2D.Collision.Shapes.b2PolygonShape;
import Box2D.Collision.Shapes.b2Shape;
import Box2D.Common.Math.b2Vec2;
import Box2D.Dynamics.b2Body;
import Box2D.Dynamics.b2BodyDef;
import Box2D.Dynamics.b2DebugDraw;
import Box2D.Dynamics.b2FixtureDef;
import Box2D.Dynamics.b2World;
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.utils.getDefinitionByName;
import lib.Hero;
import lib.IceBerg1;
import lib.IceBerg2;
import lib.IceBerg3;
import lib.IceBerg4;
import lib.Water;
/**
* ...
* @author Páll Zoltán
*/
public class PhysicsEngine extends Sprite{
private var _data:XML;
private var _world:b2World;
private var _heroBody:b2Body;
public function PhysicsEngine(data:XML) {
this._data = data;
ReflectionUtil.registerType(IceBerg1);
ReflectionUtil.registerType(IceBerg2);
ReflectionUtil.registerType(IceBerg3);
ReflectionUtil.registerType(IceBerg4);
}
public function init():void {
this.createWorld();
this.createObjects();
stage.addEventListener(Event.ENTER_FRAME, this.handleEnterFrame);
}
private function createWorld():void {
//var aabb:b2AABB = new b2AABB();
//aabb.lowerBound = new b2Vec2( -100, -100);
//aabb.upperBound = new b2Vec2(this._data.@width + 100, this._data.@height + 100);
this._world = new b2World(new b2Vec2(0, 100), true);
var m_sprite:Sprite;
m_sprite = new Sprite();
addChild(m_sprite);
var dbgDraw:b2DebugDraw = new b2DebugDraw();
var dbgSprite:Sprite = new Sprite();
m_sprite.addChild(dbgSprite);
dbgDraw.SetSprite(m_sprite);
dbgDraw.SetDrawScale(1);
dbgDraw.SetAlpha(1);
dbgDraw.SetFillAlpha(.5);
dbgDraw.SetLineThickness(1);
dbgDraw.SetFlags(b2DebugDraw.e_centerOfMassBit | b2DebugDraw.e_controllerBit | b2DebugDraw.e_controllerBit | b2DebugDraw.e_jointBit | b2DebugDraw.e_pairBit | b2DebugDraw.e_shapeBit);
this._world.SetDebugDraw(dbgDraw);
}
private function createObjects():void {
var boxBodyDefinition:b2BodyDef;
var boxBody:b2Body;
var boxBodyShape:b2PolygonShape;
var boxBodyFixture:b2FixtureDef;
var displayItem:DisplayObject;
var length:Number;
var i:Number;
length = this._data.icebergs.children().length();
// ground
displayItem = new Water();
boxBodyDefinition = new b2BodyDef();
boxBodyDefinition.position.x = 0;
boxBodyDefinition.position.y = this._data.@height;
boxBodyDefinition.type = b2Body.b2_staticBody;
boxBodyShape = new b2PolygonShape();
boxBodyShape.SetAsBox(displayItem.width / 2, 1);
boxBodyFixture = new b2FixtureDef();
boxBodyFixture.shape = boxBodyShape;
boxBodyFixture.density = 1;
boxBody = this._world.CreateBody(boxBodyDefinition);
boxBody.SetUserData(displayItem);
boxBody.CreateFixture(boxBodyFixture);
var ground:b2Body = boxBody;
this.addChild(displayItem);
// icebergs
for (i = 0; i < length; i++) {
displayItem = new (getDefinitionByName(this._data.icebergs.ib[i].@className) as Class);
boxBodyDefinition = new b2BodyDef();
boxBodyDefinition.position.x = Number(this._data.icebergs.ib[i].@x);
boxBodyDefinition.position.y = ground.GetPosition().y - displayItem.height / 2 - 1;
boxBodyDefinition.type = b2Body.b2_staticBody;
boxBodyShape = new b2PolygonShape();
boxBodyShape.SetAsBox(displayItem.width / 2, displayItem.height / 2);
boxBodyFixture = new b2FixtureDef();
boxBodyFixture.shape = boxBodyShape;
boxBody = this._world.CreateBody(boxBodyDefinition);
boxBody.SetUserData(displayItem);
boxBody.CreateFixture(boxBodyFixture);
this.addChild(displayItem);
}
// hero
displayItem = new Hero();
boxBodyDefinition = new b2BodyDef();
boxBodyDefinition.position.x = Number(this._data.hero.@x);
boxBodyDefinition.position.y = Number(this._data.hero.@y);
boxBodyDefinition.type = b2Body.b2_dynamicBody;
boxBodyShape = new b2PolygonShape();
boxBodyShape.SetAsBox(displayItem.width / 2, displayItem.height / 2);
boxBodyFixture = new b2FixtureDef();
boxBodyFixture.shape = boxBodyShape;
this._heroBody = boxBody = this._world.CreateBody(boxBodyDefinition);
boxBody.SetUserData(displayItem);
boxBody.CreateFixture(boxBodyFixture);
this.addChild(displayItem);
stage.addEventListener(KeyboardEvent.KEY_DOWN, this.handleKeys);
//stage.addEventListener(KeyboardEvent.KEY_UP, this.handleKeys);
}
private function handleKeys(event:KeyboardEvent):void{
switch (event.keyCode) {
case Keyboard.LEFT:
this._heroBody.ApplyImpulse(new b2Vec2( -10, 0), new b2Vec2());
break;
case Keyboard.RIGHT:
this._heroBody.ApplyImpulse(new b2Vec2( 10, 0), new b2Vec2());
break;
case Keyboard.UP:
// this._heroBody.SetAwake(true);
// this._heroBody.SetLinearVelocity(new b2Vec2(0, -15));
this._heroBody.ApplyImpulse(new b2Vec2(0, -15));
break;
}
}
private function handleEnterFrame(event:Event):void {
//var body:b2Body = this._world.GetBodyList();
//
//while (body && body.GetUserData()) {
//body.GetUserData().x = body.GetPosition().x;
//body.GetUserData().y = body.GetPosition().y;
//body.GetUserData().rotation = body.GetAngle() * 180 / Math.PI;
//body = body.GetNext();
//}
this._world.Step(.2, 30, 30);
this._world.ClearForces();
this._world.DrawDebugData();
}
}
}
import Box2D.Collision.b2AABB;
import Box2D.Collision.Shapes.b2MassData;
import Box2D.Collision.Shapes.b2PolygonShape;
import Box2D.Collision.Shapes.b2Shape;
import Box2D.Common.Math.b2Vec2;
import Box2D.Dynamics.b2Body;
import Box2D.Dynamics.b2BodyDef;
import Box2D.Dynamics.b2DebugDraw;
import Box2D.Dynamics.b2FixtureDef;
import Box2D.Dynamics.b2World;
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.utils.getDefinitionByName;
import lib.Hero;
import lib.IceBerg1;
import lib.IceBerg2;
import lib.IceBerg3;
import lib.IceBerg4;
import lib.Water;
/**
* ...
* @author Páll Zoltán
*/
public class PhysicsEngine extends Sprite{
private var _data:XML;
private var _world:b2World;
private var _heroBody:b2Body;
public function PhysicsEngine(data:XML) {
this._data = data;
ReflectionUtil.registerType(IceBerg1);
ReflectionUtil.registerType(IceBerg2);
ReflectionUtil.registerType(IceBerg3);
ReflectionUtil.registerType(IceBerg4);
}
public function init():void {
this.createWorld();
this.createObjects();
stage.addEventListener(Event.ENTER_FRAME, this.handleEnterFrame);
}
private function createWorld():void {
//var aabb:b2AABB = new b2AABB();
//aabb.lowerBound = new b2Vec2( -100, -100);
//aabb.upperBound = new b2Vec2(this._data.@width + 100, this._data.@height + 100);
this._world = new b2World(new b2Vec2(0, 100), true);
var m_sprite:Sprite;
m_sprite = new Sprite();
addChild(m_sprite);
var dbgDraw:b2DebugDraw = new b2DebugDraw();
var dbgSprite:Sprite = new Sprite();
m_sprite.addChild(dbgSprite);
dbgDraw.SetSprite(m_sprite);
dbgDraw.SetDrawScale(1);
dbgDraw.SetAlpha(1);
dbgDraw.SetFillAlpha(.5);
dbgDraw.SetLineThickness(1);
dbgDraw.SetFlags(b2DebugDraw.e_centerOfMassBit | b2DebugDraw.e_controllerBit | b2DebugDraw.e_controllerBit | b2DebugDraw.e_jointBit | b2DebugDraw.e_pairBit | b2DebugDraw.e_shapeBit);
this._world.SetDebugDraw(dbgDraw);
}
private function createObjects():void {
var boxBodyDefinition:b2BodyDef;
var boxBody:b2Body;
var boxBodyShape:b2PolygonShape;
var boxBodyFixture:b2FixtureDef;
var displayItem:DisplayObject;
var length:Number;
var i:Number;
length = this._data.icebergs.children().length();
// ground
displayItem = new Water();
boxBodyDefinition = new b2BodyDef();
boxBodyDefinition.position.x = 0;
boxBodyDefinition.position.y = this._data.@height;
boxBodyDefinition.type = b2Body.b2_staticBody;
boxBodyShape = new b2PolygonShape();
boxBodyShape.SetAsBox(displayItem.width / 2, 1);
boxBodyFixture = new b2FixtureDef();
boxBodyFixture.shape = boxBodyShape;
boxBodyFixture.density = 1;
boxBody = this._world.CreateBody(boxBodyDefinition);
boxBody.SetUserData(displayItem);
boxBody.CreateFixture(boxBodyFixture);
var ground:b2Body = boxBody;
this.addChild(displayItem);
// icebergs
for (i = 0; i < length; i++) {
displayItem = new (getDefinitionByName(this._data.icebergs.ib[i].@className) as Class);
boxBodyDefinition = new b2BodyDef();
boxBodyDefinition.position.x = Number(this._data.icebergs.ib[i].@x);
boxBodyDefinition.position.y = ground.GetPosition().y - displayItem.height / 2 - 1;
boxBodyDefinition.type = b2Body.b2_staticBody;
boxBodyShape = new b2PolygonShape();
boxBodyShape.SetAsBox(displayItem.width / 2, displayItem.height / 2);
boxBodyFixture = new b2FixtureDef();
boxBodyFixture.shape = boxBodyShape;
boxBody = this._world.CreateBody(boxBodyDefinition);
boxBody.SetUserData(displayItem);
boxBody.CreateFixture(boxBodyFixture);
this.addChild(displayItem);
}
// hero
displayItem = new Hero();
boxBodyDefinition = new b2BodyDef();
boxBodyDefinition.position.x = Number(this._data.hero.@x);
boxBodyDefinition.position.y = Number(this._data.hero.@y);
boxBodyDefinition.type = b2Body.b2_dynamicBody;
boxBodyShape = new b2PolygonShape();
boxBodyShape.SetAsBox(displayItem.width / 2, displayItem.height / 2);
boxBodyFixture = new b2FixtureDef();
boxBodyFixture.shape = boxBodyShape;
this._heroBody = boxBody = this._world.CreateBody(boxBodyDefinition);
boxBody.SetUserData(displayItem);
boxBody.CreateFixture(boxBodyFixture);
this.addChild(displayItem);
stage.addEventListener(KeyboardEvent.KEY_DOWN, this.handleKeys);
//stage.addEventListener(KeyboardEvent.KEY_UP, this.handleKeys);
}
private function handleKeys(event:KeyboardEvent):void{
switch (event.keyCode) {
case Keyboard.LEFT:
this._heroBody.ApplyImpulse(new b2Vec2( -10, 0), new b2Vec2());
break;
case Keyboard.RIGHT:
this._heroBody.ApplyImpulse(new b2Vec2( 10, 0), new b2Vec2());
break;
case Keyboard.UP:
// this._heroBody.SetAwake(true);
// this._heroBody.SetLinearVelocity(new b2Vec2(0, -15));
this._heroBody.ApplyImpulse(new b2Vec2(0, -15));
break;
}
}
private function handleEnterFrame(event:Event):void {
//var body:b2Body = this._world.GetBodyList();
//
//while (body && body.GetUserData()) {
//body.GetUserData().x = body.GetPosition().x;
//body.GetUserData().y = body.GetPosition().y;
//body.GetUserData().rotation = body.GetAngle() * 180 / Math.PI;
//body = body.GetNext();
//}
this._world.Step(.2, 30, 30);
this._world.ClearForces();
this._world.DrawDebugData();
}
}
}