Hi,
I have a drawArea, where I can add dynamically shapes (circle, rectangle, polygon). This shapes should interact with each other. it means, that the shapes should collide by a hit. But that doesnt work.
Anyone have an idea where my problem is??
thanks niwe
in the following you see how I add the shapes and try to give them correct physics. I draw a sprite and give it to one of this methods...
addCircle method
add rectangle method
add polygon method
Actor class
update and movements of actors
I have a drawArea, where I can add dynamically shapes (circle, rectangle, polygon). This shapes should interact with each other. it means, that the shapes should collide by a hit. But that doesnt work.
Anyone have an idea where my problem is??
thanks niwe
in the following you see how I add the shapes and try to give them correct physics. I draw a sprite and give it to one of this methods...
addCircle method
Code:
private function addCircle(tShape:Sprite):void
{
var tDynamicBody:b2Body;
var tCircle:b2CircleDef = new b2CircleDef();
tCircle.radius = (tShape.width/2)/pPhysScale;
tCircle.density = 1;
tCircle.friction = 0.3;
tCircle.restitution = 0.2
var tMyBodyDef:b2BodyDef = new b2BodyDef();
tMyBodyDef.position.Set(tShape.x / pPhysScale, tShape.y / pPhysScale);
tDynamicBody = pWorld.CreateBody(tMyBodyDef);
tDynamicBody.CreateShape(tCircle);
tDynamicBody.SetMassFromShapes();
pActor = new Actor(tDynamicBody, tShape);
tDynamicBody.m_userData = pActor;
pActors.push(pActor);
drawArea.addChild(tShape);
}
{
var tDynamicBody:b2Body;
var tCircle:b2CircleDef = new b2CircleDef();
tCircle.radius = (tShape.width/2)/pPhysScale;
tCircle.density = 1;
tCircle.friction = 0.3;
tCircle.restitution = 0.2
var tMyBodyDef:b2BodyDef = new b2BodyDef();
tMyBodyDef.position.Set(tShape.x / pPhysScale, tShape.y / pPhysScale);
tDynamicBody = pWorld.CreateBody(tMyBodyDef);
tDynamicBody.CreateShape(tCircle);
tDynamicBody.SetMassFromShapes();
pActor = new Actor(tDynamicBody, tShape);
tDynamicBody.m_userData = pActor;
pActors.push(pActor);
drawArea.addChild(tShape);
}
add rectangle method
Code:
private function addRectangle(tShape:Sprite):void
{
var tDynamicBody:b2Body;
var tRect:b2PolygonDef = new b2PolygonDef();
tRect.SetAsBox((tShape.width/2) / pPhysScale, (tShape.height/2) / pPhysScale);
tRect.friction = 0.3;
tRect.density = 1;
tRect.restitution = 0.2;
var tMyBodyDef:b2BodyDef = new b2BodyDef();
tMyBodyDef.position.Set(tShape.x / pPhysScale, tShape.y / pPhysScale);
tDynamicBody = pWorld.CreateBody(tMyBodyDef);
tDynamicBody.CreateShape(tRect);
tDynamicBody.SetMassFromShapes();
pActor = new Actor(tDynamicBody, tShape);
tDynamicBody.m_userData = pActor;
pActors.push(pActor);
drawArea.addChild(tShape);
}
{
var tDynamicBody:b2Body;
var tRect:b2PolygonDef = new b2PolygonDef();
tRect.SetAsBox((tShape.width/2) / pPhysScale, (tShape.height/2) / pPhysScale);
tRect.friction = 0.3;
tRect.density = 1;
tRect.restitution = 0.2;
var tMyBodyDef:b2BodyDef = new b2BodyDef();
tMyBodyDef.position.Set(tShape.x / pPhysScale, tShape.y / pPhysScale);
tDynamicBody = pWorld.CreateBody(tMyBodyDef);
tDynamicBody.CreateShape(tRect);
tDynamicBody.SetMassFromShapes();
pActor = new Actor(tDynamicBody, tShape);
tDynamicBody.m_userData = pActor;
pActors.push(pActor);
drawArea.addChild(tShape);
}
add polygon method
Code:
private function addConvexPolygon(tShape:Sprite, tVertices:Array):void
{
var tLong:int = tVertices.length;
var tDynamicBody:b2Body;
var tPoly:b2PolygonDef = new b2PolygonDef();
tPoly.vertexCount = tLong;
if(pDirection)
{
for(var i:int = 0; i<tLong;i++){
tPoly.vertices[i].Set(tVertices[i].x / pPhysScale, tVertices[i].y / pPhysScale);
}
}else{
for(var j:int = tLong; j>0;j--){
tPoly.vertices[tLong-j].Set(tVertices[j-1].x / pPhysScale, tVertices[j-1].y / pPhysScale);
}
}
tPoly.friction = 0.3;
tPoly.density = 1.0;
tPoly.restitution = 0.2;
var tMyBodyDef:b2BodyDef = new b2BodyDef();
tMyBodyDef.position.Set(tShape.x / pPhysScale, tShape.y / pPhysScale);
tDynamicBody = pWorld.CreateBody(tMyBodyDef);
tDynamicBody.CreateShape(tPoly);
tDynamicBody.SetMassFromShapes();
pActor = new Actor(tDynamicBody, tShape);
tDynamicBody.m_userData = pActor;
pActors.push(pActor);
drawArea.addChild(tShape);
}
{
var tLong:int = tVertices.length;
var tDynamicBody:b2Body;
var tPoly:b2PolygonDef = new b2PolygonDef();
tPoly.vertexCount = tLong;
if(pDirection)
{
for(var i:int = 0; i<tLong;i++){
tPoly.vertices[i].Set(tVertices[i].x / pPhysScale, tVertices[i].y / pPhysScale);
}
}else{
for(var j:int = tLong; j>0;j--){
tPoly.vertices[tLong-j].Set(tVertices[j-1].x / pPhysScale, tVertices[j-1].y / pPhysScale);
}
}
tPoly.friction = 0.3;
tPoly.density = 1.0;
tPoly.restitution = 0.2;
var tMyBodyDef:b2BodyDef = new b2BodyDef();
tMyBodyDef.position.Set(tShape.x / pPhysScale, tShape.y / pPhysScale);
tDynamicBody = pWorld.CreateBody(tMyBodyDef);
tDynamicBody.CreateShape(tPoly);
tDynamicBody.SetMassFromShapes();
pActor = new Actor(tDynamicBody, tShape);
tDynamicBody.m_userData = pActor;
pActors.push(pActor);
drawArea.addChild(tShape);
}
Actor class
Code:
package
{
import Box2D.Dynamics.b2Body;
import flash.display.Sprite;
public class Actor
{
private var pBody:b2Body;
private var pGraphic:Sprite;
public function Actor(b:b2Body, g:Sprite)
{
pGraphic = g;
pBody = b;
}
public function get body():b2Body{
return pBody;
}
public function get graphic():Sprite{
return pGraphic;
}
}
}
{
import Box2D.Dynamics.b2Body;
import flash.display.Sprite;
public class Actor
{
private var pBody:b2Body;
private var pGraphic:Sprite;
public function Actor(b:b2Body, g:Sprite)
{
pGraphic = g;
pBody = b;
}
public function get body():b2Body{
return pBody;
}
public function get graphic():Sprite{
return pGraphic;
}
}
}
update and movements of actors
Code:
private function update(ev:Event):void{
pFirstObjekt = false;
pWorld.Step(pTimeStep, pIterations);
moveActors();
}
private function moveActors():void
{
for each (var a:Actor in pActors)
{
var tBodyPosition:b2Vec2 = a.body.GetPosition();
var tBodyRotation:Number = a.body.GetAngle();
a.graphic.x = tBodyPosition.x * pPhysScale;
a.graphic.y = tBodyPosition.y * pPhysScale;
a.graphic.rotation = tBodyRotation * (180/Math.PI) % 360; //Don't forget to convert to degrees!
}
}
pFirstObjekt = false;
pWorld.Step(pTimeStep, pIterations);
moveActors();
}
private function moveActors():void
{
for each (var a:Actor in pActors)
{
var tBodyPosition:b2Vec2 = a.body.GetPosition();
var tBodyRotation:Number = a.body.GetAngle();
a.graphic.x = tBodyPosition.x * pPhysScale;
a.graphic.y = tBodyPosition.y * pPhysScale;
a.graphic.rotation = tBodyRotation * (180/Math.PI) % 360; //Don't forget to convert to degrees!
}
}