I have a dynamic body and its position is perfect. I tried the same thing with a static one and its positionning has gone "wild".They both are in a sprite and both sprites have the same position and they are children of the same MovieClip.
Here is my Ball (dynamic body):
Please Remark that i do not even scale the body's position and it fits perectly.
public class Ball extends Actor
{
public static const PHYSIC_RADIUS:int=5;//in meters
public static const RADIUS:int=10;
public static const MASS:int=10;
//private var cam_:Camera;
public function Ball(color:uint)
{
super();
color_=color;
this.InitializeActor();
}
override protected function InitializeActor():void
{
this.DrawActor();
this.InitializePhysics();
//UpdatePosition();
}
override protected function DrawActor():void
{
shape_=new Shape();
this.addChild(shape_);
shape_.graphics.beginFill(color_);
shape_.graphics.drawCircle(0,0,RADIUS);
shape_.graphics.endFill();
shape_.x=75;
shape_.y=50;
}
override protected function InitializePhysics():void
{
var ball:b2CircleShape=new b2CircleShape(PHYSIC_RADIUS);
bp_=PhysicManager.GetInstance().CreateDynamicBody(ball,20,0.2,1,this);
bp_.body_.SetPosition(new b2Vec2(75,50));
debug_draw();
}
Here is the code for my Walls (static body):
public function Wall(isHorizontal:Boolean)
{
super();
isHorizontal_=isHorizontal;
if(isHorizontal)
{
height_=WIDTH;
width_=HEIGHT;
}
else
{
height_=HEIGHT;
width_=WIDTH;
}
color_= WallFabric.WALLCOLOR;
InitializeActor();
}
override protected function InitializeActor():void
{
this.DrawActor();
this.InitializePhysics();
}
override protected function DrawActor():void
{
shape_=new Shape();
this.addChild(shape_);
shape_.graphics.beginFill(color_);
if(!isHorizontal_)
height_-=65;
shape_.graphics.drawRect(0,0,width_,height_);
shape_.graphics.endFill();
}
override protected function InitializePhysics():void
{
var rect:b2PolygonShape=new b2PolygonShape();
if(isHorizontal_)
{
rect.SetAsBox(width_/(2*3),height_/(2*3));
}
else
rect.SetAsBox(width_/(2* 3),(height_+65)/(2*3.6));
bp_=PhysicManager.GetInstance().CreateStaticBody(rect,20,0.2,1,this);
}
public function SetPosition(p:Point):void
{
shape_.x=p.x;
shape_.y=p.y;
if(!isHorizontal_)
bp_.body_.SetPosition(new b2Vec2(p.x/3+3.5,p.y/3+55));
else
bp_.body_.SetPosition(new b2Vec2(p.x/3+68,p.y/3+3));
trace("p.x: "+p.x.toString()+"p.y: "+p.y.toString());
debug_draw();
//UpdatePosition();
}
The actual AS3 shape is Ok but when you come to the wall it's all messed up!
P.S. I understand b2PolygonShape.SetAsBox might be different from Draw rectangle but in that case i would have at least 2 sides ok.
Here is my Ball (dynamic body):
Please Remark that i do not even scale the body's position and it fits perectly.
Code:
public class Ball extends Actor
{
public static const PHYSIC_RADIUS:int=5;//in meters
public static const RADIUS:int=10;
public static const MASS:int=10;
//private var cam_:Camera;
public function Ball(color:uint)
{
super();
color_=color;
this.InitializeActor();
}
override protected function InitializeActor():void
{
this.DrawActor();
this.InitializePhysics();
//UpdatePosition();
}
override protected function DrawActor():void
{
shape_=new Shape();
this.addChild(shape_);
shape_.graphics.beginFill(color_);
shape_.graphics.drawCircle(0,0,RADIUS);
shape_.graphics.endFill();
shape_.x=75;
shape_.y=50;
}
override protected function InitializePhysics():void
{
var ball:b2CircleShape=new b2CircleShape(PHYSIC_RADIUS);
bp_=PhysicManager.GetInstance().CreateDynamicBody(ball,20,0.2,1,this);
bp_.body_.SetPosition(new b2Vec2(75,50));
debug_draw();
}
Here is the code for my Walls (static body):
Code:
public function Wall(isHorizontal:Boolean)
{
super();
isHorizontal_=isHorizontal;
if(isHorizontal)
{
height_=WIDTH;
width_=HEIGHT;
}
else
{
height_=HEIGHT;
width_=WIDTH;
}
color_= WallFabric.WALLCOLOR;
InitializeActor();
}
override protected function InitializeActor():void
{
this.DrawActor();
this.InitializePhysics();
}
override protected function DrawActor():void
{
shape_=new Shape();
this.addChild(shape_);
shape_.graphics.beginFill(color_);
if(!isHorizontal_)
height_-=65;
shape_.graphics.drawRect(0,0,width_,height_);
shape_.graphics.endFill();
}
override protected function InitializePhysics():void
{
var rect:b2PolygonShape=new b2PolygonShape();
if(isHorizontal_)
{
rect.SetAsBox(width_/(2*3),height_/(2*3));
}
else
rect.SetAsBox(width_/(2* 3),(height_+65)/(2*3.6));
bp_=PhysicManager.GetInstance().CreateStaticBody(rect,20,0.2,1,this);
}
public function SetPosition(p:Point):void
{
shape_.x=p.x;
shape_.y=p.y;
if(!isHorizontal_)
bp_.body_.SetPosition(new b2Vec2(p.x/3+3.5,p.y/3+55));
else
bp_.body_.SetPosition(new b2Vec2(p.x/3+68,p.y/3+3));
trace("p.x: "+p.x.toString()+"p.y: "+p.y.toString());
debug_draw();
//UpdatePosition();
}
The actual AS3 shape is Ok but when you come to the wall it's all messed up!
P.S. I understand b2PolygonShape.SetAsBox might be different from Draw rectangle but in that case i would have at least 2 sides ok.