본문 바로가기

Random Thoughts

Nape 를 사용해 보았다.

앵그리 버드의 유리벽과 같이 일정수준 이상의 에너지가 가해지면 깨지는 판정을 구현해 보았습니다.
Nape의 충돌시 에너지 계산을 이용해 보았습니다.
totalContactsImpulse를 이용해 충격량을 얻어낼 수 있고, 이것이 일정 수준 이상이면 반응하게 하면 됩니다.

import nape.space.Space;
import nape.geom.Vec2;
import nape.phys.Body;
import nape.phys.BodyType;
import nape.shape.Polygon;
import flash.events.Event;
import nape.phys.Material;
import nape.geom.Vec3;
import flash.display.MovieClip;
import nape.constraint.PivotJoint;
import flash.events.MouseEvent;
import flash.display.Sprite;

const r2d:Number=180/Math.PI;
var space:Space=new Space()
var boom:Sprite=new Sprite();
var box:Body=new Body();
var btn:MovieClip=new MovieClip();
var floor:Body=getBorder(550,400);
var debug:BitmapDebug;
var hand:PivotJoint=new PivotJoint(space.world,box,Vec2.weak(),Vec2.weak())
btn.graphics.beginFill(0);
btn.graphics.drawRect(-50,-50,100,100);
boom.graphics.beginFill(0xFF0000);
boom.graphics.drawRect(0,0,550,400);
addChild(boom)
addChild(btn)
hand.active=false;
hand.stiff=false
hand.space=space
hand.maxForce=10000
floor.space=space;
box.shapes.add(new Polygon(Polygon.box(100,100)));
box.position.setxy(275,200);
box.velocity.setxy(200,200)
box.angularVel=4
box.space=space;
function step(e:Event):void{
space.step(1/24)
var imp:Vec3=box.totalContactsImpulse()
imp.z=0
boom.alpha*=.8
if(imp.length>12000){
boom.alpha=1
}
btn.x=box.position.x;
btn.y=box.position.y;
btn.rotation=box.rotation*r2d
if(hand.active){
hand.anchor1.setxy(mouseX,mouseY);
}
}
addEventListener(Event.ENTER_FRAME,step);
btn.addEventListener(MouseEvent.MOUSE_DOWN,down);
stage.addEventListener(MouseEvent.MOUSE_UP,up);
btn.buttonMode=true;
function down(e:MouseEvent):void{
hand.active=true;
hand.anchor2.setxy(e.localX,e.localY);
}
function up(e:MouseEvent):void{
hand.active=false;
}
//
function getBorder(w:Number,h:Number):Body{
var border:Body = new Body(BodyType.STATIC);
var m:Material=Material.rubber();
border.shapes.add(new Polygon(Polygon.rect(0, 0, w, -1),m));
border.shapes.add(new Polygon(Polygon.rect(0, h, w, 1),m));
border.shapes.add(new Polygon(Polygon.rect(0, 0, -1, h),m));      
border.shapes.add(new Polygon(Polygon.rect(w, 0, 1, h),m));
return border;
}

뭔가 세게 박으면 빨개지는 플래시 



  

물리엔진이 꽤나 쓸만하네요
 AS3 물리엔진으로 추천함.


'Random Thoughts' 카테고리의 다른 글

트위스티드 노조미  (0) 2016.07.17
기자스핀  (0) 2014.03.31
Hello Tistory  (0) 2014.01.17