box2d施加多个重力

Box2D everyinch 4122℃ 0评论

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;

[SWF(width=”800″,height=”600″,frameRate=”32″,backgroundColor=”0x000000″)]
public class MultiGravity extends Sprite{
private var world:b2World;
private var body:b2Body;
private var timeStep:Number;
private var iterations:uint;
private var pixelsPerMeter:Number = 30;

public function MultiGravity(){
createWorld();
makeDebugDraw();
createWall();
createBodies();
addEventListener(Event.ENTER_FRAME,onEnterframe);
}

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 createWall():void{
var bodyDef:b2BodyDef = new b2BodyDef();
bodyDef.position.Set(400/pixelsPerMeter,15/pixelsPerMeter);
body = world.CreateBody(bodyDef);
var shape:b2PolygonShape = new b2PolygonShape();
shape.SetAsBox(400/pixelsPerMeter,15/pixelsPerMeter);
body.CreateFixture2(shape);

bodyDef.position.Set(400/pixelsPerMeter,585/pixelsPerMeter);
body = world.CreateBody(bodyDef);
body.CreateFixture2(shape);
}

private function createBodies():void{
for(var i:int=0;i<10;i++){ var bodyDef:b2BodyDef = new b2BodyDef(); bodyDef.type = b2Body.b2_dynamicBody; bodyDef.position.x = (Math.random()*600+120)/pixelsPerMeter; bodyDef.position.y = (Math.random()*150)/pixelsPerMeter; var hx:Number = (Math.random()*30+15)/pixelsPerMeter; var hy:Number = (Math.random()*30+15)/pixelsPerMeter; var fixtureDef:b2FixtureDef = new b2FixtureDef(); fixtureDef.density = 1.0; fixtureDef.friction = 0.5; fixtureDef.restitution = 0.2; if(Math.random()<0.5){ bodyDef.userData = new Sprite(); bodyDef.userData.name ="square"; var square:b2PolygonShape = new b2PolygonShape(); square.SetAsBox(hx,hy); fixtureDef.shape = square; body = world.CreateBody(bodyDef); body.CreateFixture(fixtureDef); } else{ bodyDef.userData = new Sprite(); bodyDef.userData.name = "circle"; var circle:b2CircleShape = new b2CircleShape(); circle.SetRadius(hx); fixtureDef.shape = circle; body = world.CreateBody(bodyDef); body.CreateFixture(fixtureDef); } } } 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 onEnterframe(e:Event):void{ world.Step(timeStep,iterations,iterations); world.ClearForces(); world.DrawDebugData(); var reverseGravity:b2Vec2; for(var body:b2Body = world.GetBodyList();body;body = body.GetNext()){ if(body.GetUserData() != null){ if(body.GetUserData().name == "circle"){ reverseGravity = new b2Vec2(0.0,-20*body.GetMass()); //reverseGravity = new b2Vec2(0.0,-9.8*body.GetMass()); body.ApplyForce(reverseGravity,body.GetWorldCenter()); } } } } } } [/code]

分享&收藏

转载请注明:陈童的博客 » box2d施加多个重力

喜欢 (0)
发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
'; } if( dopt('d_footcode_b') ) echo dopt('d_footcode'); ?>