box2d 服务器性能,Box2d三种施加力的方法

package{

import Box2D.Collision.Shapes.b2PolygonShape;

import Box2D.Common.Math.b2Vec2;

import Box2D.Dynamics.Joints.b2RevoluteJoint;

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;

import flash.events.MouseEvent;

[SWF(width="800",height="600",frameRate="32",backgroundColor="0x000000")]

public class ApplicableForce extends Sprite{

private var world:b2World;

private var timeStep:Number;

private var iterations:uint;

private var pixelsPerMeter:Number = 30;

private var frontJoint:b2RevoluteJoint;

private var rearJoint:b2RevoluteJoint;

public function ApplicableForce(){

createWorld();

makeDebugDraw();

createBox(400,585,400,15,false,"ground");

createBox(100,40,10,10,true,"left");

createBox(400,40,10,10,true,"middle");

createBox(700,40,10,10,true,"right");

stage.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);

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 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 createBox(x:Number,y:Number,hx:Number,hy:Number,isDynamic:Boolean,data:String):void{

var bodyDef:b2BodyDef = new b2BodyDef();

bodyDef.position.Set(x/pixelsPerMeter,y/pixelsPerMeter);

if(isDynamic){

bodyDef.type = b2Body.b2_dynamicBody;

}

var box:b2PolygonShape = new b2PolygonShape();

box.SetAsBox(hx/pixelsPerMeter,hy/pixelsPerMeter);

var fixtureDef:b2FixtureDef = new b2FixtureDef();

fixtureDef.shape = box;

var body:b2Body = world.CreateBody(bodyDef);

body.SetUserData(data);

body.CreateFixture(fixtureDef);

}

private function onMouseDown(e:MouseEvent):void{

var force:b2Vec2;

for(var body:b2Body = world.GetBodyList();body;body = body.GetNext()){

switch(body.GetUserData()){

case "left":

force = new b2Vec2(0,-450);

body.ApplyForce(force,body.GetWorldCenter());

break;

case "middle":

force = new b2Vec2(0,-15);

body.ApplyImpulse(force,body.GetWorldCenter());

break;

case "right":

force = new b2Vec2(0,-15);

body.SetAwake(true);

body.SetLinearVelocity(force);

break;

}

}

}

private function onEnterframe(e:Event):void{

world.Step(timeStep,iterations,iterations);

world.ClearForces();

world.DrawDebugData();

}

}

}

ce1d00c6afb499fd198b4d9049ded088.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值