I have a project using Box2D and Flash Action Script 0.3. I have lot of movie clips calling from action script And the shape with their physic in box2D. How can I link the movie clips and the object of box2D together.
I’m also having problems with the coalitions not happening as it should the objects are over lapping each other.
I have a lot of object’s in my scene so It’s too slow and its bugging.
Can anyone help me? Please!
Here is my code.
I’m also having problems with the coalitions not happening as it should the objects are over lapping each other.
I have a lot of object’s in my scene so It’s too slow and its bugging.
Can anyone help me? Please!
Here is my code.
Code:
package
{
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Common.Math.*;
import Box2D.Dynamics.*;
import Box2D.Dynamics.Joints.*;
import flash.display.Sprite;
import flash.events.Event;
public class particles extends Sprite
{
public var world:b2World;
public var body:b2Body;
public var body1:b2Body;
public var ratio:Number=30;
public var num:Number=-500;
//
public var fixtureDef:b2FixtureDef=new b2FixtureDef();
public var handBodyDef:b2BodyDef;
public function particles():void
{
world=new b2World(new b2Vec2(0, 10.0), true);
debug_draw();
// variables
var bodyDef:b2BodyDef=new b2BodyDef();
// adding the ground
var boxDef:b2PolygonShape=new b2PolygonShape();
bodyDef.position.Set(364 / ratio, 90 / ratio);
boxDef.SetAsBox(365 / ratio, 10 / ratio);
fixtureDef.shape=boxDef;
fixtureDef.friction=1;
fixtureDef.density=1;
body=world.CreateBody(bodyDef);
body.CreateFixture(fixtureDef);
// adding the hand
handBodyDef=new b2BodyDef();
var handeDef:b2PolygonShape=new b2PolygonShape();
handBodyDef.position.Set(10/ratio,30/ratio);
handBodyDef.type = b2Body.b2_kinematicBody;
handBodyDef.allowSleep = false;
handBodyDef.active = true;
handBodyDef.angle = ratio * Math.PI / 180;
handeDef.SetAsBox(100 / ratio, 200 / ratio);
fixtureDef.shape=handeDef;
fixtureDef.friction=0;
fixtureDef.density=50000;
body1=world.CreateBody(handBodyDef);
body1.CreateFixture(fixtureDef);
var increX:Number = 0;
for (var i:Number=0; i < 50; i++)
{
// add circle Def
var circleDef:b2CircleShape=new b2CircleShape(10 / ratio);
// front wheel
bodyDef.position.Set(increX / ratio, 11 / ratio);
bodyDef.type=b2Body.b2_dynamicBody;
fixtureDef.shape=circleDef;
fixtureDef.density=0.1;
fixtureDef.friction=1;
fixtureDef.restitution=0.5;
body=world.CreateBody(bodyDef);
body.CreateFixture(fixtureDef);
increX += 5;
}
//
addEventListener(Event.ENTER_FRAME, update);
}
public function debug_draw():void
{
var m_sprite:Sprite;
m_sprite=new Sprite();
addChild(m_sprite);
var dbgDraw:b2DebugDraw=new b2DebugDraw();
dbgDraw.SetSprite(m_sprite);
dbgDraw.SetDrawScale(ratio);
dbgDraw.SetFillAlpha(0.5);
dbgDraw.SetLineThickness(1);
dbgDraw.SetFlags(b2DebugDraw.e_shapeBit);
world.SetDebugDraw(dbgDraw);
}
public function update(e:Event):void
{
num+=10;
world.Step(1 / ratio, 10, 10);
world.ClearForces()
world.DrawDebugData();
body1.SetPosition(new b2Vec2(num/ratio,30/ratio));
}
}
}
{
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Common.Math.*;
import Box2D.Dynamics.*;
import Box2D.Dynamics.Joints.*;
import flash.display.Sprite;
import flash.events.Event;
public class particles extends Sprite
{
public var world:b2World;
public var body:b2Body;
public var body1:b2Body;
public var ratio:Number=30;
public var num:Number=-500;
//
public var fixtureDef:b2FixtureDef=new b2FixtureDef();
public var handBodyDef:b2BodyDef;
public function particles():void
{
world=new b2World(new b2Vec2(0, 10.0), true);
debug_draw();
// variables
var bodyDef:b2BodyDef=new b2BodyDef();
// adding the ground
var boxDef:b2PolygonShape=new b2PolygonShape();
bodyDef.position.Set(364 / ratio, 90 / ratio);
boxDef.SetAsBox(365 / ratio, 10 / ratio);
fixtureDef.shape=boxDef;
fixtureDef.friction=1;
fixtureDef.density=1;
body=world.CreateBody(bodyDef);
body.CreateFixture(fixtureDef);
// adding the hand
handBodyDef=new b2BodyDef();
var handeDef:b2PolygonShape=new b2PolygonShape();
handBodyDef.position.Set(10/ratio,30/ratio);
handBodyDef.type = b2Body.b2_kinematicBody;
handBodyDef.allowSleep = false;
handBodyDef.active = true;
handBodyDef.angle = ratio * Math.PI / 180;
handeDef.SetAsBox(100 / ratio, 200 / ratio);
fixtureDef.shape=handeDef;
fixtureDef.friction=0;
fixtureDef.density=50000;
body1=world.CreateBody(handBodyDef);
body1.CreateFixture(fixtureDef);
var increX:Number = 0;
for (var i:Number=0; i < 50; i++)
{
// add circle Def
var circleDef:b2CircleShape=new b2CircleShape(10 / ratio);
// front wheel
bodyDef.position.Set(increX / ratio, 11 / ratio);
bodyDef.type=b2Body.b2_dynamicBody;
fixtureDef.shape=circleDef;
fixtureDef.density=0.1;
fixtureDef.friction=1;
fixtureDef.restitution=0.5;
body=world.CreateBody(bodyDef);
body.CreateFixture(fixtureDef);
increX += 5;
}
//
addEventListener(Event.ENTER_FRAME, update);
}
public function debug_draw():void
{
var m_sprite:Sprite;
m_sprite=new Sprite();
addChild(m_sprite);
var dbgDraw:b2DebugDraw=new b2DebugDraw();
dbgDraw.SetSprite(m_sprite);
dbgDraw.SetDrawScale(ratio);
dbgDraw.SetFillAlpha(0.5);
dbgDraw.SetLineThickness(1);
dbgDraw.SetFlags(b2DebugDraw.e_shapeBit);
world.SetDebugDraw(dbgDraw);
}
public function update(e:Event):void
{
num+=10;
world.Step(1 / ratio, 10, 10);
world.ClearForces()
world.DrawDebugData();
body1.SetPosition(new b2Vec2(num/ratio,30/ratio));
}
}
}