package{
import Box2D.Collision.Shapes.b2CircleShape;
import Box2D.Collision.Shapes.b2PolygonShape;
import Box2D.Common.Math.b2Vec2;
import Box2D.Dynamics.b2Body;
import Box2D.Dynamics.b2BodyDef;
import Box2D.Dynamics.b2DebugDraw;
import Box2D.Dynamics.b2FixtureDef;
import Box2D.Dynamics.b2World;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.utils.Timer;
[SWF(width="800",height="600",frameRate="32",backgroundColor="0x000000")]
public class CollisionFilter extends Sprite{
private var world:b2World;
private var timeStep:Number;
private var iterations:uint;
private var pixelsPerMeter:Number = 30;
public function CollisionFilter(){
createWorld();
makeDebugDraw();
makeGround();
addEventListener(Event.ENTER_FRAME,onEnterframe);
var timer:Timer = new Timer(2000);
timer.addEventListener(TimerEvent.TIMER,onTimer);
timer.start();
}
private function createWorld():void{
var gravity:b2Vec2 = new b2Vec2(0.0,9.8);
var doSleep:Boolean = true;
world = new b2World(gravity,doSleep);
world.SetWarmStarting(true);
timeStep = 1.0/30.0;
iterations = 10;
}
private function makeDebugDraw():void{
var debugDraw:b2DebugDraw = new b2DebugDraw();
var debugSprite:Sprite = new Sprite();
addChild(debugSprite);
debugDraw.SetSprite(debugSprite);
debugDraw.SetDrawScale(30.0);
debugDraw.SetFillAlpha(0.5);
debugDraw.SetLineThickness(1.0);
debugDraw.SetFlags(b2DebugDraw.e_shapeBit);
world.SetDebugDraw(debugDraw);
}
private function makeGround():void{
var bodyDef:b2BodyDef = new b2BodyDef();
bodyDef.position.Set(250/pixelsPerMeter,480/pixelsPerMeter);
var box:b2PolygonShape = new b2PolygonShape();
box.SetAsOrientedBox(400/pixelsPerMeter,30/pixelsPerMeter,new b2Vec2(150/pixelsPerMeter,45/pixelsPerMeter),Math.PI/32);
var fixtureDef:b2FixtureDef = new b2FixtureDef();
fixtureDef.shape = box;
fixtureDef.density = 0;
fixtureDef.friction = 0.3;
fixtureDef.filter.categoryBits = 2;
var body:b2Body = world.CreateBody(bodyDef);
body.CreateFixture(fixtureDef);
bodyDef.position.Set(240/pixelsPerMeter,480/pixelsPerMeter);
box.SetAsBox(30/pixelsPerMeter,90/pixelsPerMeter);
fixtureDef.shape = box;
fixtureDef.density = 0;
fixtureDef.friction = 0.3;
fixtureDef.filter.categoryBits = 4;
body = world.CreateBody(bodyDef);
body.CreateFixture(fixtureDef);
}
private function onTimer(e:TimerEvent = null):void{
var bodyDef:b2BodyDef = new b2BodyDef();
bodyDef.type = b2Body.b2_dynamicBody;
bodyDef.position.Set(180/pixelsPerMeter,60/pixelsPerMeter);
var circle:b2CircleShape = new b2CircleShape(60/pixelsPerMeter);
var fixtureDef:b2FixtureDef = new b2FixtureDef();
fixtureDef.shape = circle;
fixtureDef.density = 1.0;
fixtureDef.friction = 0.5;
fixtureDef.restitution = 0.2;
fixtureDef.filter.maskBits = Math.ceil(Math.random()*3)*2;//2,4,6
var body:b2Body = world.CreateBody(bodyDef);
body.CreateFixture(fixtureDef);
}
private function onEnterframe(e:Event):void{
world.Step(timeStep,iterations,iterations);
world.ClearForces();
world.DrawDebugData();
}
}
}
转载请注明:陈童的博客 » box2d碰撞过滤