box2d的碰撞控制FilterData

/*
FilterData
补漏:
filterData.categoryBits指自己所属的碰撞种类
filterData.maskBits指与其碰撞的种类
 
 
 注意  filterData.categoryBits 的值必须是2的倍数
 
 假如有两个body 他们的 filterData.categoryBits分别为2,4
 则另一个body要想与他们两个都发生碰撞 则其filterData.maskBits应该为: 2+4= 6;
 
 对于filterData.groupIndex 属性:
 当其为负值时,表示该组的对象不发生碰撞
 为正值则表示该组的对象发生碰撞
    另外 要使两个对象不发生碰撞,则: groupIndex 属性必须为负, 并且groupIndex 的值一样,这样才能不发生碰撞 其他情况都会发生碰撞(即两个不相同的负值的body也会发生碰撞)
 
 由上述可知:控制body是否发生碰撞有两种方法:
 法1: 用filterData.categoryBits(自己所属的碰撞种类)和filterData.maskBits(指定碰撞种类)
 法2:直接使用filterData.groupIndex即可
 
 当上述两种情况都使用的话,则由filterData.groupIndex决定  因为filterData.groupIndex的优先级高些 。

*/

 

package {
 import flash.display.Sprite;
 import flash.events.Event;
 import Box2D.Dynamics.*;
 import Box2D.Collision.*;
 import Box2D.Collision.Shapes.*;
 import Box2D.Common.Math.*;
 public class bound extends Sprite {
  var body:b2Body;
  public var m_world:b2World;
  public var m_iterations:int=10;
  public var m_timeStep:Number=1.0/30.0;
  var m_boundaryListener=new b2BoundaryListener();
  var bodyDef:b2BodyDef;
  var boxDef:b2PolygonDef;
  var circleDef:b2CircleDef;
  public function bound() {
   addEventListener(Event.ENTER_FRAME, Update, false, 0, true);
   var worldAABB:b2AABB = new b2AABB();
   worldAABB.lowerBound.Set(-100.0, -100.0);
   worldAABB.upperBound.Set(100.0, 100.0);
   var gravity:b2Vec2=new b2Vec2(0.0,10.0);
   var doSleep:Boolean=true;
   m_world=new b2World(worldAABB,gravity,doSleep);
   m_world.SetBoundaryListener(m_boundaryListener);
   // debug draw start
   var m_sprite:Sprite;
   m_sprite = new Sprite();
   addChild(m_sprite);
   var dbgDraw:b2DebugDraw = new b2DebugDraw();
   var dbgSprite:Sprite = new Sprite();
   m_sprite.addChild(dbgSprite);
   dbgDraw.m_sprite=m_sprite;
   dbgDraw.m_drawScale=30;
   dbgDraw.m_alpha=1;
   dbgDraw.m_fillAlpha=0.5;
   dbgDraw.m_lineThickness=1;
   dbgDraw.m_drawFlags=b2DebugDraw.e_shapeBit;
   m_world.SetDebugDraw(dbgDraw);
   // ground
   bodyDef = new b2BodyDef();
   bodyDef.position.Set(4, 12);
   boxDef = new b2PolygonDef();
   boxDef.filter.categoryBits=2;
   boxDef.filter.groupIndex =2
   boxDef.SetAsOrientedBox(10, 1,new b2Vec2(5, 1.5), Math.PI/32);
   boxDef.friction=0.3;
   boxDef.density=0;
   body=m_world.CreateBody(bodyDef);
   body.CreateShape(boxDef);
   body.SetMassFromShapes();
   // another object
   bodyDef = new b2BodyDef();
   bodyDef.position.Set(8, 12);
   boxDef = new b2PolygonDef();
   boxDef.filter.categoryBits=4;
   boxDef.filter.groupIndex =-3;
   boxDef.SetAsBox(1, 3);
   boxDef.friction=0.3;
   boxDef.density=0;
   body=m_world.CreateBody(bodyDef);
   body.CreateShape(boxDef);
   body.SetMassFromShapes();
   // circle
   create_circle();
  }
  public function create_circle() {
   bodyDef = new b2BodyDef();
   bodyDef.position.x=6;
   bodyDef.position.y=2;
   circleDef = new b2CircleDef();
//   circleDef.filter.maskBits=Math.ceil(Math.random()*3)*2;
   circleDef.filter.groupIndex =-3;
   circleDef.radius=2;
   circleDef.density=1.0;
   circleDef.friction=0.5;
   circleDef.restitution=0.2;
   body=m_world.CreateBody(bodyDef);
   body.CreateShape(circleDef);
   body.SetMassFromShapes();
  }
  public function Update(e:Event):void {
   m_world.Step(m_timeStep, m_iterations);
   //if (m_boundaryListener.get_contact()) {
//    m_boundaryListener.no_contact();
//    m_world.DestroyBody(body);
//    bodyDef = new b2BodyDef();
//    create_circle();
//   }
  }
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值