package {
import flash.display.Sprite;
[SWF(width="500", height="400", framerate="30")]
public class MyFirstBox2D extends Sprite {
public function MyFirstBox2D() {
super();
}
}
}
private var world:b2World = new b2World(new b2Vec2(0, 20), true);
private var world_scale :Number = 30;
private function init():void {
var debug_draw:b2DebugDraw = new b2DebugDraw();
var debug_sprite:Sprite = new Sprite();
debug_draw.SetSprite(debug_sprite);
debug_draw.SetDrawScale(world_scale);
debug_draw.SetFlags(b2DebugDraw.e_shapeBit);
world.SetDebugDraw(debug_draw);
addChild(debug_sprite);
}
private function onEnter(event:Event):void {
world.Step(1/30, 10, 10);
world.ClearForces();
world.DrawDebugData();
}
private function createBox
(px:Number, py:Number, angle:Number,
w:Number, h:Number, isDynamic:Boolean):void
{
var _tempBody:b2Body;
var _tempBodyDef:b2BodyDef = newb2BodyDef();
var _tempFixtureDef:b2FixtureDef = newb2FixtureDef();
var _tempPolygonDef:b2PolygonShape = newb2PolygonShape();
_tempPolygonDef.SetAsBox(w/2/world_scale, h/2/world_scale);
_tempFixtureDef.shape = _tempPolygonDef;
_tempFixtureDef.friction = 0.5;
_tempFixtureDef.density = 1;
_tempBodyDef.position.Set(px / world_scale, py / world_scale);
_tempBodyDef.angle = angle / Math.PI * 2;
_tempBodyDef.type = isDynamic ? b2Body.b2_dynamicBody:b2Body.b2_staticBody;
_tempBody = world.CreateBody(_tempBodyDef);
_tempBody.CreateFixture(_tempFixtureDef);
}
private function onMove(event:MouseEvent):void {
createBox(stage.mouseX,stage.mouseY,Math.random()*90, Math.random() * 35 + 15, Math.random() * 35 + 15,true);
}
createBox(250,200,0,100,10,false);
createBox(250,390,0,500,10,false);
package {
import Box2D.Collision.*;
import Box2D.Common.Math.b2Vec2;
import Box2D.Dynamics.*;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
[SWF(width="500", height="400", framerate="30")]
public class MyFirstBox2D extends Sprite {
private var world:b2World = new b2World(new b2Vec2(0, 20), true);
private var world_scale:Number = 30;
public function MyFirstBox2D() {
super();
init();
stage.addEventListener(MouseEvent.CLICK, onMove);
stage.addEventListener(Event.ENTER_FRAME, onEnter);
}
private function init():void {
// debug draw setting
var debug_draw:b2DebugDraw = new b2DebugDraw();
var debug_sprite:Sprite = new Sprite();
debug_draw.SetSprite(debug_sprite);
debug_draw.SetDrawScale(world_scale);
debug_draw.SetFlags(b2DebugDraw.e_shapeBit);
world.SetDebugDraw(debug_draw);
addChild(debug_sprite);
//ground body (static)
createBox(250,200,0,100,10,false);
createBox(250,390,0,500,10,false);
}
private function onMove(event:MouseEvent):void {
createBox(stage.mouseX,stage.mouseY,Math.random()*90, Math.random() * 35 + 15, Math.random() * 35 + 15,true);
}
private function onEnter(event:Event):void {
world.Step(1/30, 10, 10);
world.ClearForces();
world.DrawDebugData();
}
private function createBox(px:Number, py:Number, angle:Number, w:Number, h:Number, isDynamic:Boolean):void {
var _tempBody:b2Body;
var _tempBodyDef:b2BodyDef = new b2BodyDef();
var _tempFixtureDef:b2FixtureDef = new b2FixtureDef();
var _tempPolygonDef:b2PolygonShape = new b2PolygonShape();
_tempPolygonDef.SetAsBox(w/2/world_scale, h/2/world_scale);
_tempFixtureDef.shape = _tempPolygonDef;
_tempFixtureDef.friction = 0.5;
_tempFixtureDef.density = 1;
_tempBodyDef.position.Set(px / world_scale, py / world_scale);
_tempBodyDef.angle = angle / Math.PI * 2;
_tempBodyDef.type = isDynamic ? b2Body.b2_dynamicBody:b2Body.b2_staticBody;
_tempBody = world.CreateBody(_tempBodyDef);
_tempBody.CreateFixture(_tempFixtureDef);
}
}
}
'programming > box2d docs' 카테고리의 다른 글
b2Revolute Joint 예제 (0) | 2011.01.30 |
---|---|
b2DistanceJoint 예제 (0) | 2011.01.28 |
Box2DFlash v2.1a Update Notes 비공식 한글문서 (0) | 2011.01.16 |
Box2DFlash v2.0.2 User Manual 비공식 한글문서 vol. 2 (0) | 2011.01.09 |
Box2DFlash v2.0.2 User Manual 비공식 한글문서 vol. 1 (2) | 2010.12.08 |
WRITTEN BY