hello!
I'm trying to create a set of box2d component for flash cs3. but now i'm in trouble... basically i create a single shape component for every type, but now i want to aggregate the box, circle and poly.
I was able to make working box and circle, but i've problem with triangle... Maybe because i'm not a programmer. 😳
here the code of the body component:
there is somebody so kindle to explane me what it's wrong?
maybe it's difficult understand 300 line of inuseful code, but i think that the problem is located in the function initPhysBody
in the switch case TRIANGLE.
and so in the update method.
I don't understad why the box and circle work and the triangle s'not...
confused 😕 😕
I'm trying to create a set of box2d component for flash cs3. but now i'm in trouble... basically i create a single shape component for every type, but now i want to aggregate the box, circle and poly.
I was able to make working box and circle, but i've problem with triangle... Maybe because i'm not a programmer. 😳
here the code of the body component:
there is somebody so kindle to explane me what it's wrong?
Code:
package FrameworkObject{
import fl.core.UIComponent;
import FrameworkObject.DynamicMovieComponent;
import FrameworkObject.ObjPerimeter;
import FrameworkObject.TraceUtil;
import flash.display.Shape;
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.geom.Rectangle;
import flash.geom.Point;
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Common.Math.*;
public class PhysBox extends DynamicMovieComponent{
private var bodyDef:b2BodyDef;
private var shapeDef:*;//can be b2sBoxDef, b2CircleDef, b2PolyDef;
protected var _world:b2World;
protected var _body:b2Body;
protected var _bodyType:String = "CIRCLE";
protected var _skinName:String = "BODY_SKIN";
protected var _density:Number = 0;
protected var _friction:Number = 0.2;
protected var _restitution:Number = 0;
protected var _linearVelocity:Object = {x:0,y:0};
protected var _angularVelocity:Number = 0;
protected var _linearDamping:Number = 0;
protected var _angularDamping:Number = 0;
protected var _allowSleep:Boolean = true;
protected var _isSleeping:Boolean = false;
// protected var _isBullet:Boolean = false;
private var skin:MovieClip;
public function PhysBox()
{
super();
// Add ground body
bodyDef = new b2BodyDef();
};
protected override function draw():void
{
super.draw();
if(isLivePreview)
{
(skinName != "BODY_SKIN")?addSkin(bodyType+"_USER"):addSkin(bodyType);
}
else
{
addSkin(skinName)
}
if(bodyType == "CIRCLE")
{
skin.y = (height-width)/2;
skin.height = width;
}
else
{
skin.height = height;
}
skin.width = width;
if(!isLivePreview && world!=null)initPhysBody();
}
public function get world():b2World
{
return _world;
}
public function set world(world_:b2World):void
{
if(world_ == _world){ return; }
_world = world_;
}
public function get body():b2Body
{
return _body;
}
public function set body(body_:b2Body):void
{
if(body_ == _body){ return; }
_body = body_;
}
[Inspectable(type="List", enumeration="CIRCLE,BOX,TRIANGLE,AUTOPOLYGON", defaultValue="CIRCLE", name="bodyType")]
public function get bodyType():String
{
return _bodyType;
}
public function set bodyType(bodyType_:String):void
{
if(bodyType_ == _bodyType){ return; }
_bodyType = bodyType_;
}
[Inspectable(defaultValue="0",type="Number", name="density")]
public function get density():Number
{
return _density;
}
public function set density(density_:Number):void
{
_density = density_;
if(shapeDef == undefined || shapeDef.density == density_){ return; }
shapeDef.density = density_;
}
[Inspectable(defaultValue="0.2",type="Number", name="friction")]
public function get friction():Number
{
return _friction;
}
public function set friction(friction_:Number):void
{
_friction = friction_;
if(shapeDef == undefined || shapeDef.friction == friction_){ return; }
shapeDef.friction = friction_;
}
[Inspectable(defaultValue="0",type="Number", name="restitution")]
public function get restitution():Number
{
return _restitution;
}
public function set restitution(restitution_:Number):void
{
_restitution = restitution_;
if(shapeDef == undefined || shapeDef.restitution == restitution_){ return; }
shapeDef.restitution = restitution_;
}
[Inspectable(defaultValue="x:0,y:0",type="Object", name="linearVelocity")]
public function get linearVelocity():Object
{
return _linearVelocity;
}
public function set linearVelocity(linearVelocity_:Object):void
{
if(bodyDef.linearVelocity == new b2Vec2(linearVelocity_.x, linearVelocity_.y)){ return; }
bodyDef.linearVelocity = new b2Vec2(linearVelocity_.x, linearVelocity_.y);
_linearVelocity = linearVelocity_;
}
[Inspectable(defaultValue="0",type="Number", name="angularVelocity")]
public function get angularVelocity():Number
{
return _angularVelocity;
}
public function set angularVelocity(angularVelocity_:Number):void
{
if(bodyDef.angularVelocity == angularVelocity_){ return; }
bodyDef.angularVelocity = angularVelocity_;
_angularVelocity = angularVelocity_;
}
[Inspectable(defaultValue="0",type="Number", name="linearDamping")]
public function get linearDamping():Number
{
return _linearDamping;
}
public function set linearDamping(linearDamping_:Number):void
{
if(bodyDef.linearDamping == linearDamping_){ return; }
bodyDef.linearDamping = linearDamping_;
_linearDamping = linearDamping_;
}
[Inspectable(defaultValue="0",type="Number", name="angularDamping")]
public function get angularDamping():Number
{
return _angularDamping;
}
public function set angularDamping(angularDamping_:Number):void
{
if(bodyDef.angularDamping == angularDamping_){ return; }
bodyDef.angularDamping = angularDamping_;
_angularDamping = angularDamping_;
}
[Inspectable(defaultValue="true",type="Boolean", name="allowSleep")]
public function get allowSleep():Boolean
{
return _allowSleep;
}
public function set allowSleep(allowSleep_:Boolean):void
{
if(bodyDef.allowSleep == allowSleep_){ return; }
bodyDef.allowSleep = allowSleep_;
_allowSleep = allowSleep_;
}
[Inspectable(defaultValue="false",type="Boolean", name="isSleeping")]
public function get isSleeping():Boolean
{
return _isSleeping;
}
public function set isSleeping(isSleeping_:Boolean):void
{
if(bodyDef.isSleeping == isSleeping_){ return; }
bodyDef.isSleeping = isSleeping_;
_isSleeping = isSleeping_;
}
/*
[Inspectable(defaultValue="false",type="Boolean", name="isBullet")]
public function get isBullet():Boolean
{
return _isBullet;
}
public function set isBullet(isBullet_:Boolean):void
{
if(isBullet_ == _isBullet){ return; }
shapeDef.isBullet = isBullet_;
_isBullet = isBullet_;
}*/
[Inspectable(defaultValue="BODY_SKIN", type=String, name="skinName")]
public function get skinName():String
{
return _skinName;
}
public function set skinName(skinName_:String):void
{
if(skinName_ == _skinName){ return; }
_skinName = skinName_;
}
public function initPhysBody():void
{
switch(bodyType)
{
case "CIRCLE":
setRegistration(width/2, height/2);//corresponding on the baricenter (center of mass);
shapeDef = new b2CircleDef();
shapeDef.radius = width/2;
break
case "BOX":
setRegistration(width/2, height/2);
shapeDef = new b2BoxDef();
shapeDef.extents.Set(width/2, height/2);
break
case "TRIANGLE":
setRegistration(width/2,(height*2)/3);
shapeDef = new b2PolyDef();
shapeDef.vertexCount = 3;
shapeDef.vertices[0].Set(width,height);
shapeDef.vertices[1].Set(width/2,0);
shapeDef.vertices[2].Set(0,height);
break
case "AUTOPOLYGON":
shapeDef = new b2PolyDef();
//shapeDef.type = b2Shape.e_polyShape
//shapeDef.vertexCount = b2Settings.b2_maxPolyVertices;
//var vertices:Array = ObjPerimeter.simplifyArray(ObjPerimeter.arrayPerimeter(this,200,2),shapeDef.vertexCount);
shapeDef.vertexCount = 7;
var vertices:Array = ObjPerimeter.simplifyArray(ObjPerimeter.arrayPerimeter(this,200,2),7);
for(var j = 0; j < 7; j++)
{
shapeDef.vertices[j].Set(vertices[j].x,vertices[j].y);
}
//bodyDef.position.Set(shapeDef.vertices[0].x,shapeDef.vertices[0].y);
break
}
addShapeParam();
bodyDef.position.Set(x2,y2);
bodyDef.rotation = rotation / (180/Math.PI);
bodyDef.AddShape(shapeDef);
bodyDef.userData = this;
body = world.CreateBody(bodyDef);
}
public function update():void
{
x2 = body.m_position.x;
y2 = body.m_position.y;
rotation2 = body.m_rotation * (180/Math.PI);
if(bodyType == "TRIANGLE")
{
var drw:Array = new Array(shapeDef.vertices[0],shapeDef.vertices[1],shapeDef.vertices[2]);
TraceUtil.traceArrayPoint(parent,drw,0xff0000)
}
};
private function addShapeParam():void
{
density = density;
friction = friction;
restitution = restitution;
};
private function addSkin(skinName:String):void
{
var classDef;
if(numChildren > 0)
{
if(skin.name == skinName)
return
skin = null;
removeChildAt(0);
}
try
{
classDef=this.loaderInfo.applicationDomain.getDefinition(skinName);
}
catch (e:ReferenceError)
{
try
{
classDef=this.loaderInfo.applicationDomain.getDefinition(bodyType);
}
catch (e:ReferenceError)
{
return;
}
}
skin = new classDef as MovieClip;
addChild(skin);
};
}
}
import fl.core.UIComponent;
import FrameworkObject.DynamicMovieComponent;
import FrameworkObject.ObjPerimeter;
import FrameworkObject.TraceUtil;
import flash.display.Shape;
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.geom.Rectangle;
import flash.geom.Point;
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Common.Math.*;
public class PhysBox extends DynamicMovieComponent{
private var bodyDef:b2BodyDef;
private var shapeDef:*;//can be b2sBoxDef, b2CircleDef, b2PolyDef;
protected var _world:b2World;
protected var _body:b2Body;
protected var _bodyType:String = "CIRCLE";
protected var _skinName:String = "BODY_SKIN";
protected var _density:Number = 0;
protected var _friction:Number = 0.2;
protected var _restitution:Number = 0;
protected var _linearVelocity:Object = {x:0,y:0};
protected var _angularVelocity:Number = 0;
protected var _linearDamping:Number = 0;
protected var _angularDamping:Number = 0;
protected var _allowSleep:Boolean = true;
protected var _isSleeping:Boolean = false;
// protected var _isBullet:Boolean = false;
private var skin:MovieClip;
public function PhysBox()
{
super();
// Add ground body
bodyDef = new b2BodyDef();
};
protected override function draw():void
{
super.draw();
if(isLivePreview)
{
(skinName != "BODY_SKIN")?addSkin(bodyType+"_USER"):addSkin(bodyType);
}
else
{
addSkin(skinName)
}
if(bodyType == "CIRCLE")
{
skin.y = (height-width)/2;
skin.height = width;
}
else
{
skin.height = height;
}
skin.width = width;
if(!isLivePreview && world!=null)initPhysBody();
}
public function get world():b2World
{
return _world;
}
public function set world(world_:b2World):void
{
if(world_ == _world){ return; }
_world = world_;
}
public function get body():b2Body
{
return _body;
}
public function set body(body_:b2Body):void
{
if(body_ == _body){ return; }
_body = body_;
}
[Inspectable(type="List", enumeration="CIRCLE,BOX,TRIANGLE,AUTOPOLYGON", defaultValue="CIRCLE", name="bodyType")]
public function get bodyType():String
{
return _bodyType;
}
public function set bodyType(bodyType_:String):void
{
if(bodyType_ == _bodyType){ return; }
_bodyType = bodyType_;
}
[Inspectable(defaultValue="0",type="Number", name="density")]
public function get density():Number
{
return _density;
}
public function set density(density_:Number):void
{
_density = density_;
if(shapeDef == undefined || shapeDef.density == density_){ return; }
shapeDef.density = density_;
}
[Inspectable(defaultValue="0.2",type="Number", name="friction")]
public function get friction():Number
{
return _friction;
}
public function set friction(friction_:Number):void
{
_friction = friction_;
if(shapeDef == undefined || shapeDef.friction == friction_){ return; }
shapeDef.friction = friction_;
}
[Inspectable(defaultValue="0",type="Number", name="restitution")]
public function get restitution():Number
{
return _restitution;
}
public function set restitution(restitution_:Number):void
{
_restitution = restitution_;
if(shapeDef == undefined || shapeDef.restitution == restitution_){ return; }
shapeDef.restitution = restitution_;
}
[Inspectable(defaultValue="x:0,y:0",type="Object", name="linearVelocity")]
public function get linearVelocity():Object
{
return _linearVelocity;
}
public function set linearVelocity(linearVelocity_:Object):void
{
if(bodyDef.linearVelocity == new b2Vec2(linearVelocity_.x, linearVelocity_.y)){ return; }
bodyDef.linearVelocity = new b2Vec2(linearVelocity_.x, linearVelocity_.y);
_linearVelocity = linearVelocity_;
}
[Inspectable(defaultValue="0",type="Number", name="angularVelocity")]
public function get angularVelocity():Number
{
return _angularVelocity;
}
public function set angularVelocity(angularVelocity_:Number):void
{
if(bodyDef.angularVelocity == angularVelocity_){ return; }
bodyDef.angularVelocity = angularVelocity_;
_angularVelocity = angularVelocity_;
}
[Inspectable(defaultValue="0",type="Number", name="linearDamping")]
public function get linearDamping():Number
{
return _linearDamping;
}
public function set linearDamping(linearDamping_:Number):void
{
if(bodyDef.linearDamping == linearDamping_){ return; }
bodyDef.linearDamping = linearDamping_;
_linearDamping = linearDamping_;
}
[Inspectable(defaultValue="0",type="Number", name="angularDamping")]
public function get angularDamping():Number
{
return _angularDamping;
}
public function set angularDamping(angularDamping_:Number):void
{
if(bodyDef.angularDamping == angularDamping_){ return; }
bodyDef.angularDamping = angularDamping_;
_angularDamping = angularDamping_;
}
[Inspectable(defaultValue="true",type="Boolean", name="allowSleep")]
public function get allowSleep():Boolean
{
return _allowSleep;
}
public function set allowSleep(allowSleep_:Boolean):void
{
if(bodyDef.allowSleep == allowSleep_){ return; }
bodyDef.allowSleep = allowSleep_;
_allowSleep = allowSleep_;
}
[Inspectable(defaultValue="false",type="Boolean", name="isSleeping")]
public function get isSleeping():Boolean
{
return _isSleeping;
}
public function set isSleeping(isSleeping_:Boolean):void
{
if(bodyDef.isSleeping == isSleeping_){ return; }
bodyDef.isSleeping = isSleeping_;
_isSleeping = isSleeping_;
}
/*
[Inspectable(defaultValue="false",type="Boolean", name="isBullet")]
public function get isBullet():Boolean
{
return _isBullet;
}
public function set isBullet(isBullet_:Boolean):void
{
if(isBullet_ == _isBullet){ return; }
shapeDef.isBullet = isBullet_;
_isBullet = isBullet_;
}*/
[Inspectable(defaultValue="BODY_SKIN", type=String, name="skinName")]
public function get skinName():String
{
return _skinName;
}
public function set skinName(skinName_:String):void
{
if(skinName_ == _skinName){ return; }
_skinName = skinName_;
}
public function initPhysBody():void
{
switch(bodyType)
{
case "CIRCLE":
setRegistration(width/2, height/2);//corresponding on the baricenter (center of mass);
shapeDef = new b2CircleDef();
shapeDef.radius = width/2;
break
case "BOX":
setRegistration(width/2, height/2);
shapeDef = new b2BoxDef();
shapeDef.extents.Set(width/2, height/2);
break
case "TRIANGLE":
setRegistration(width/2,(height*2)/3);
shapeDef = new b2PolyDef();
shapeDef.vertexCount = 3;
shapeDef.vertices[0].Set(width,height);
shapeDef.vertices[1].Set(width/2,0);
shapeDef.vertices[2].Set(0,height);
break
case "AUTOPOLYGON":
shapeDef = new b2PolyDef();
//shapeDef.type = b2Shape.e_polyShape
//shapeDef.vertexCount = b2Settings.b2_maxPolyVertices;
//var vertices:Array = ObjPerimeter.simplifyArray(ObjPerimeter.arrayPerimeter(this,200,2),shapeDef.vertexCount);
shapeDef.vertexCount = 7;
var vertices:Array = ObjPerimeter.simplifyArray(ObjPerimeter.arrayPerimeter(this,200,2),7);
for(var j = 0; j < 7; j++)
{
shapeDef.vertices[j].Set(vertices[j].x,vertices[j].y);
}
//bodyDef.position.Set(shapeDef.vertices[0].x,shapeDef.vertices[0].y);
break
}
addShapeParam();
bodyDef.position.Set(x2,y2);
bodyDef.rotation = rotation / (180/Math.PI);
bodyDef.AddShape(shapeDef);
bodyDef.userData = this;
body = world.CreateBody(bodyDef);
}
public function update():void
{
x2 = body.m_position.x;
y2 = body.m_position.y;
rotation2 = body.m_rotation * (180/Math.PI);
if(bodyType == "TRIANGLE")
{
var drw:Array = new Array(shapeDef.vertices[0],shapeDef.vertices[1],shapeDef.vertices[2]);
TraceUtil.traceArrayPoint(parent,drw,0xff0000)
}
};
private function addShapeParam():void
{
density = density;
friction = friction;
restitution = restitution;
};
private function addSkin(skinName:String):void
{
var classDef;
if(numChildren > 0)
{
if(skin.name == skinName)
return
skin = null;
removeChildAt(0);
}
try
{
classDef=this.loaderInfo.applicationDomain.getDefinition(skinName);
}
catch (e:ReferenceError)
{
try
{
classDef=this.loaderInfo.applicationDomain.getDefinition(bodyType);
}
catch (e:ReferenceError)
{
return;
}
}
skin = new classDef as MovieClip;
addChild(skin);
};
}
}
maybe it's difficult understand 300 line of inuseful code, but i think that the problem is located in the function initPhysBody
in the switch case TRIANGLE.
and so in the update method.
I don't understad why the box and circle work and the triangle s'not...
confused 😕 😕