Hi, i have been following some tutorials on box2d and wrote the below code following the tutorial step by step ......but the problem is i do not get to access some properties as mentioned in the tutorial , also after writing the code the crate goes through the floor and does not stop there....
I do not have access to the codes when i try to access the properties after the dot in my code... i have highlighted it in color in my code.....I am using the 2.0 version of box2d, all works well but the crate just falls through the floor, where am i going wrong and why i cannot access the properties can any one please guide me here .....
CreateShape and SetMassFromShapes i do not have access to also m_sprite,m_drawScale,m_lineThickness,m_fillAlpha i do not know where they come from ...
////////////////////////////////////////////////////////////////////////////
package
{
import Box2D.Collision.b2AABB;
import Box2D.Collision.Shapes.b2PolygonDef;
import Box2D.Common.Math.b2Vec2;
import Box2D.Dynamics.b2Body;
import Box2D.Dynamics.b2BodyDef;
import Box2D.Dynamics.b2DebugDraw;
import Box2D.Dynamics.b2World;
import flash.display.Sprite;
import flash.events.Event;
import flash.display.MovieClip;
/**
* ...
* @author jinisner
*/
public class HelloWorld extends MovieClip
{
private var _fallingCrate:b2Body;
protected var _world:b2World;
public static const Ratio:Number = 40;
public function HelloWorld()
{
setupWorld();
createWallsandFloors();
addACrate();
setUpDebugDraw();
addEventListener(Event.ENTER_FRAME, newFrameEvent);
}
private function setUpDebugDraw():void
{
var spriteToDraw:Sprite = new Sprite();
addChild(spriteToDraw);
var artistToHire:b2DebugDraw = new b2DebugDraw();
artistToHire.m_sprite = spriteToDraw;
artistToHire.m_drawScale = Ratio;
artistToHire.SetFlags(b2DebugDraw.e_shapeBit);
artistToHire.m_lineThickness = 2.0;
artistToHire.m_fillAlpha = 0.6;
_world.SetDebugDraw(artistToHire);
}
private function newFrameEvent(e:Event):void
{
_world.Step(1.0 / 3.0, 10);
trace("falling crate is at " + _fallingCrate.GetPosition().x + "," + _fallingCrate.GetPosition().y);
}
private function setupWorld():void
{
var universeSize:b2AABB = new b2AABB();
universeSize.lowerBound.Set( -3000/Ratio, -3000/Ratio);
universeSize.upperBound.Set(3000 / Ratio, 3000 / Ratio);
var gravity:b2Vec2 = new b2Vec2(0, 9.8);
var ignoreSleeping:Boolean = true;
_world = new b2World(universeSize, gravity, ignoreSleeping);
trace("My world has " + _world.GetBodyCount() + " bodies in it");
}
private function createWallsandFloors():void
{
var bigLongShapeDef:b2PolygonDef = new b2PolygonDef();
bigLongShapeDef.vertexCount = 4;
b2Vec2(bigLongShapeDef.vertices[0]).Set(0 / Ratio, 0 / Ratio);
b2Vec2(bigLongShapeDef.vertices[1]).Set(550 / Ratio, 0 / Ratio);
b2Vec2(bigLongShapeDef.vertices[2]).Set(550 / Ratio, 10 / Ratio);
b2Vec2(bigLongShapeDef.vertices[0]).Set(0 / Ratio, 10 / Ratio);
bigLongShapeDef.friction = 0.5;
bigLongShapeDef.restitution = 0.3;
bigLongShapeDef.density = 0.0;
//////////////////////////////////////////
var floorBodyDef:b2BodyDef = new b2BodyDef();
floorBodyDef.position.Set(0 / Ratio, 390 / Ratio);
////////////////////////////////////////////
var floorBody:b2Body = _world.CreateBody(floorBodyDef);
////////////////////////////////////////////////
floorBody.CreateShape(bigLongShapeDef);
floorBody.SetMassFromShapes();
trace("my world now has " + _world.GetBodyCount() + " wow" );
var tellSkinnyShapeDef:b2PolygonDef = new b2PolygonDef();
tellSkinnyShapeDef.SetAsBox(5 / Ratio, 195 / Ratio);
tellSkinnyShapeDef.friction = 0.5;
tellSkinnyShapeDef.restitution = 0.3;
tellSkinnyShapeDef.density = 0.0;
var wallBodyDef:b2BodyDef = new b2BodyDef();
wallBodyDef.position.Set(5 / Ratio, 195 / Ratio);
var leftWall:b2Body = _world.CreateBody(wallBodyDef);
leftWall.CreateShape(tellSkinnyShapeDef);
leftWall.SetMassFromShapes();
wallBodyDef.position.Set(545 / Ratio, 195 / Ratio);
var rightWall:b2Body = _world.CreateBody(wallBodyDef);
rightWall.CreateShape(tellSkinnyShapeDef);
rightWall.SetMassFromShapes();
}
private function addACrate():void
{
var fallCrateDef:b2PolygonDef = new b2PolygonDef();
fallCrateDef.SetAsBox(25 / Ratio, 25 / Ratio);
fallCrateDef.friction = 0.8;
fallCrateDef.restitution = 0.3;
fallCrateDef.density = 0.7;
var fallCrateBodyDef:b2BodyDef = new b2BodyDef();
fallCrateBodyDef.position.Set(250 / Ratio, -30 / Ratio);
fallCrateBodyDef.angle = 30 * Math.PI / 180;
_fallingCrate = _world.CreateBody(fallCrateBodyDef);
_fallingCrate.CreateShape(fallCrateDef);
_fallingCrate.SetMassFromShapes();
}
}
}
//////////////////////////////////////////////////////////////////
I do not have access to the codes when i try to access the properties after the dot in my code... i have highlighted it in color in my code.....I am using the 2.0 version of box2d, all works well but the crate just falls through the floor, where am i going wrong and why i cannot access the properties can any one please guide me here .....
CreateShape and SetMassFromShapes i do not have access to also m_sprite,m_drawScale,m_lineThickness,m_fillAlpha i do not know where they come from ...
////////////////////////////////////////////////////////////////////////////
package
{
import Box2D.Collision.b2AABB;
import Box2D.Collision.Shapes.b2PolygonDef;
import Box2D.Common.Math.b2Vec2;
import Box2D.Dynamics.b2Body;
import Box2D.Dynamics.b2BodyDef;
import Box2D.Dynamics.b2DebugDraw;
import Box2D.Dynamics.b2World;
import flash.display.Sprite;
import flash.events.Event;
import flash.display.MovieClip;
/**
* ...
* @author jinisner
*/
public class HelloWorld extends MovieClip
{
private var _fallingCrate:b2Body;
protected var _world:b2World;
public static const Ratio:Number = 40;
public function HelloWorld()
{
setupWorld();
createWallsandFloors();
addACrate();
setUpDebugDraw();
addEventListener(Event.ENTER_FRAME, newFrameEvent);
}
private function setUpDebugDraw():void
{
var spriteToDraw:Sprite = new Sprite();
addChild(spriteToDraw);
var artistToHire:b2DebugDraw = new b2DebugDraw();
artistToHire.m_sprite = spriteToDraw;
artistToHire.m_drawScale = Ratio;
artistToHire.SetFlags(b2DebugDraw.e_shapeBit);
artistToHire.m_lineThickness = 2.0;
artistToHire.m_fillAlpha = 0.6;
_world.SetDebugDraw(artistToHire);
}
private function newFrameEvent(e:Event):void
{
_world.Step(1.0 / 3.0, 10);
trace("falling crate is at " + _fallingCrate.GetPosition().x + "," + _fallingCrate.GetPosition().y);
}
private function setupWorld():void
{
var universeSize:b2AABB = new b2AABB();
universeSize.lowerBound.Set( -3000/Ratio, -3000/Ratio);
universeSize.upperBound.Set(3000 / Ratio, 3000 / Ratio);
var gravity:b2Vec2 = new b2Vec2(0, 9.8);
var ignoreSleeping:Boolean = true;
_world = new b2World(universeSize, gravity, ignoreSleeping);
trace("My world has " + _world.GetBodyCount() + " bodies in it");
}
private function createWallsandFloors():void
{
var bigLongShapeDef:b2PolygonDef = new b2PolygonDef();
bigLongShapeDef.vertexCount = 4;
b2Vec2(bigLongShapeDef.vertices[0]).Set(0 / Ratio, 0 / Ratio);
b2Vec2(bigLongShapeDef.vertices[1]).Set(550 / Ratio, 0 / Ratio);
b2Vec2(bigLongShapeDef.vertices[2]).Set(550 / Ratio, 10 / Ratio);
b2Vec2(bigLongShapeDef.vertices[0]).Set(0 / Ratio, 10 / Ratio);
bigLongShapeDef.friction = 0.5;
bigLongShapeDef.restitution = 0.3;
bigLongShapeDef.density = 0.0;
//////////////////////////////////////////
var floorBodyDef:b2BodyDef = new b2BodyDef();
floorBodyDef.position.Set(0 / Ratio, 390 / Ratio);
////////////////////////////////////////////
var floorBody:b2Body = _world.CreateBody(floorBodyDef);
////////////////////////////////////////////////
floorBody.CreateShape(bigLongShapeDef);
floorBody.SetMassFromShapes();
trace("my world now has " + _world.GetBodyCount() + " wow" );
var tellSkinnyShapeDef:b2PolygonDef = new b2PolygonDef();
tellSkinnyShapeDef.SetAsBox(5 / Ratio, 195 / Ratio);
tellSkinnyShapeDef.friction = 0.5;
tellSkinnyShapeDef.restitution = 0.3;
tellSkinnyShapeDef.density = 0.0;
var wallBodyDef:b2BodyDef = new b2BodyDef();
wallBodyDef.position.Set(5 / Ratio, 195 / Ratio);
var leftWall:b2Body = _world.CreateBody(wallBodyDef);
leftWall.CreateShape(tellSkinnyShapeDef);
leftWall.SetMassFromShapes();
wallBodyDef.position.Set(545 / Ratio, 195 / Ratio);
var rightWall:b2Body = _world.CreateBody(wallBodyDef);
rightWall.CreateShape(tellSkinnyShapeDef);
rightWall.SetMassFromShapes();
}
private function addACrate():void
{
var fallCrateDef:b2PolygonDef = new b2PolygonDef();
fallCrateDef.SetAsBox(25 / Ratio, 25 / Ratio);
fallCrateDef.friction = 0.8;
fallCrateDef.restitution = 0.3;
fallCrateDef.density = 0.7;
var fallCrateBodyDef:b2BodyDef = new b2BodyDef();
fallCrateBodyDef.position.Set(250 / Ratio, -30 / Ratio);
fallCrateBodyDef.angle = 30 * Math.PI / 180;
_fallingCrate = _world.CreateBody(fallCrateBodyDef);
_fallingCrate.CreateShape(fallCrateDef);
_fallingCrate.SetMassFromShapes();
}
}
}
//////////////////////////////////////////////////////////////////