package {
import nape.phys.Body;
import nape.shape.Shape;
import nape.shape.Circle;
import flash.display.MovieClip;
import flash.utils.getDefinitionByName;
import nape.callbacks.CbType;
import nape.callbacks.PreListener;
import nape.callbacks.InteractionType;
import nape.callbacks.PreCallback;
import nape.dynamics.CollisionArbiter;
import nape.geom.Vec2;
import nape.callbacks.InteractionListener;
import nape.callbacks.CbEvent;
import nape.callbacks.InteractionCallback;
import nape.phys.BodyType;
import nape.shape.Polygon;
import nape.dynamics.Contact;
import nape.dynamics.ContactList; public class Main extends BaseMain { public function Main() {
super({gravity:{x:0,y:0}});
} private var _cCbType:CbType=new CbType();
private var _rCbType:CbType=new CbType();
override protected function createBodies():void {
//一些用于碰撞的刚体
for(var i:int=0;i<20;i++){
var b:Body;
if(Math.random()>0.5)b=createBox(50,2,stage.stageWidth*Math.random(),stage.stageHeight*Math.random());
else b=createCircle(25,stage.stageWidth*Math.random(),stage.stageHeight*Math.random());
b.rotation = 2*Math.PI*Math.random();
b.type = BodyType.STATIC;
b.space = _space;
} //圆形
var c:Body = createCircle(20,stage.stageWidth>>1,stage.stageHeight>>1);
c.cbTypes.add(_cCbType);
c.space = _space;
c.velocity.setxy(500,500);
_space.listeners.add(new InteractionListener(CbEvent.BEGIN,InteractionType.COLLISION,_cCbType,CbType.ANY_BODY,circleHitBeginHandler)); //矩形
var r:Body = createBox(40,40,stage.stageWidth>>1-100,stage.stageHeight>>1);
r.cbTypes.add(_rCbType);
r.space = _space;
r.velocity.setxy(-500,-500);
_space.listeners.add(new InteractionListener(CbEvent.BEGIN,InteractionType.COLLISION,_rCbType,CbType.ANY_BODY,boxHitBeginHandler));
} private function circleHitBeginHandler(cb:InteractionCallback):void{
var colArb:CollisionArbiter = cb.arbiters.length>0?cb.arbiters.at(0) as CollisionArbiter:null;
if(!colArb)return;
//trace("圆形碰撞点数:"+colArb.contacts.length);//始终只有一个碰撞点
var hitPos:Vec2 = colArb.contacts.at(0).position;
addEffect(hitPos.x,hitPos.y);
//反弹
var normal:Vec2 = colArb.normal;
cb.int1.castBody.velocity.set(normal.mul(500));
} private function boxHitBeginHandler(cb:InteractionCallback):void{
var colArb:CollisionArbiter = cb.arbiters.length>0?cb.arbiters.at(0) as CollisionArbiter:null;
if(!colArb)return;
//trace("矩形碰撞点数:"+colArb.contacts.length);//会出现多个碰撞点
var shape2:nape.shape.Shape = colArb.shape2.body == cb.int2.castBody?colArb.shape2:colArb.shape1;
//查找出真正碰撞的点
var contact:Contact = getHitShapeContact(colArb.contacts,shape2);
if(contact){
var hitPos:Vec2 = contact.position;
if(hitPos) addEffect(hitPos.x,hitPos.y);
}
//反弹
var normal:Vec2 = colArb.normal;
cb.int1.castBody.velocity.set(normal.mul(500));
} private function getHitShapeContact(contacts:ContactList,shape:nape.shape.Shape):Contact{
var i:int = contacts.length;
while(--i>=0){
if(shape.contains(contacts.at(i).position))return contacts.at(i);
}
return null;
} private function addEffect(x:Number,y:Number):void{
var _Class:Class = flash.utils.getDefinitionByName("Effect") as Class;
var eff:MovieClip = (new _Class()) as MovieClip;
eff.x = x;
eff.y = y;
this.addChild(eff);
}
} }
源码下载地址:
http://yunpan.cn/cd8GasJG5rkUp 访问密码 6873