【游客模式】——注册会员,加入11RIA 闪客社区吧!一起见证Flash的再次辉煌……
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
转载:9RIA游戏开发者社区(天地会)
作者:ladeng6666(拉登大叔)
作者博客:http://www.ladeng6666.com/blog/
【Box2D系列教程-导航帖】—拉登大叔出品(总贴)
在"掉落的苹果——b2Body"中,有一个地面,它是静止不动的,不随刚体下落(废话,地面掉落那是地震!)。我想特别说明一下静止固定刚体的创建方法。
在Box2D中,刚体是否为静止不动,由"形状需求"b2ShapeDef决定。具体的讲,质量density为0的刚体就是静止不动的。注意:我用的是Box2D 2.0.1
[Actionscript3] 纯文本查看 复制代码 var shapeRequest:b2PolygonDef = new b2PolygonDef();
//详细说明我们的需求
//---------------------------------
//设置密度density为0,即表示该刚体是静止不动的
shapeRequest.density = 0;
//---------------------------------
shapeRequest.friction = 0.3;
shapeRequest.restitution = 0.2;
shapeRequest.SetAsBox(w/30, h/30);
下面的例子中,掉落的小球会被固定的挡板挡住,然后一个个掉进下面的小槽内(你可以在"创建圆形刚体"一章回顾一下小球的创建方法)
[Actionscript3] 纯文本查看 复制代码 package
{
import Box2D.Collision.b2AABB;
import Box2D.Collision.Shapes.b2CircleDef;
import Box2D.Collision.Shapes.b2PolygonDef;
import Box2D.Common.Math.b2Vec2;
import Box2D.Dynamics.b2Body;
import Box2D.Dynamics.b2BodyDef;
import Box2D.Dynamics.b2DebugDraw;
import Box2D.Dynamics.b2World;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
/**
* [url]http://www.ladeng6666.com[/url]
* @author ladeng6666
*/
public class Main extends Sprite
{
private var world:b2World;
private var body:b2Body;
private var life:Number = 0;
private var interval:Number = 20;
public function Main()
{
createWorld();
createDebug();
createBody(stage.stageWidth/2,0);
createWalls();
addEventListener(Event.ENTER_FRAME, loop);
stage.addEventListener(MouseEvent.MOUSE_DOWN, onStageMouseDown);
}
private function onStageMouseDown(e:MouseEvent):void
{
createBody(mouseX,mouseY);
}
private function loop(e:Event):void
{
world.Step(1 / 30, 10);
if (life < interval) {
life++;
}else {
createBody(120, 0);
life = 0;
}
}
private function createWorld():void
{
//1.创建一个环境
var environment:b2AABB = new b2AABB();
environment.lowerBound = new b2Vec2( -100, -100);
environment.upperBound = new b2Vec2(100, 100);
//2.声明重力
var gravity:b2Vec2 = new b2Vec2(0, 10);
//3.睡着的对象是否模拟
var doSleep:Boolean = true;
//4.创建b2World世界
world = new b2World(environment, gravity, doSleep);
}
private function createDebug():void
{
var debugSprite:Sprite = new Sprite();
addChild(debugSprite);
var debugDraw:b2DebugDraw = new b2DebugDraw();
debugDraw.m_sprite = debugSprite;
debugDraw.m_drawScale = 30.0;
debugDraw.m_fillAlpha = 0.5;
debugDraw.m_lineThickness = 1.0;
debugDraw.m_drawFlags = b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit;
world.SetDebugDraw(debugDraw);
}
private function createBody(posX:Number,posY:Number):void
{
//1.创建刚体需求b2BodyDef
var bodyRequest:b2BodyDef = new b2BodyDef();
bodyRequest.position.Set(posX / 30, posY / 30);//记得米和像素的转换关系
//2.Box2D世界工厂更具需求创建createBody()生产刚体
body=world.CreateBody(bodyRequest);
//3.创建敢提形状需求b2ShapeDef的子类
//定义形状需求是b2CircleDef圆形
var shapeRequest:b2CircleDef = new b2CircleDef();
//详细说明我们的需求
shapeRequest.density = 3;
shapeRequest.friction = 0.3;
shapeRequest.restitution = 0.2;
shapeRequest.radius = 0.5;
//4.b2Body刚体工厂根据需求createShape生产形状
body.CreateShape(shapeRequest);
body.SetMassFromShapes();
}
private function createWalls():void
{
createStaticBody(stage.stageWidth / 2, stage.stageHeight, stage.stageWidth, 30);
createStaticBody(120, 100, 80, 5, Math.PI / 8);
createStaticBody(240, 270, 5, 40);
createStaticBody(440, 270, 5, 40);
createStaticBody(340, 310, 104, 5);
}
private function createStaticBody(posX:Number, posY:Number, w:Number, h:Number,angle:Number=0):b2Body {
//1.创建刚体需求b2BodyDef
var bodyRequest:b2BodyDef = new b2BodyDef();
bodyRequest.position.Set(posX / 30, posY / 30);//记得米和像素的转换关系
bodyRequest.angle = angle;
//2.Box2D世界工厂更具需求创建createBody()生产刚体
var staticBody:b2Body=world.CreateBody(bodyRequest);
//3.创建敢提形状需求b2ShapeDef的子类
var shapeRequest:b2PolygonDef = new b2PolygonDef();
//详细说明我们的需求
//---------------------------------
//设置密度density为0,即表示该刚体是静止不动的
shapeRequest.density = 0;
//---------------------------------
shapeRequest.friction = 0.3;
shapeRequest.restitution = 0.2;
shapeRequest.SetAsBox(w/30, h/30);
//4.b2Body刚体工厂根据需求createShape生产形状
staticBody.CreateShape(shapeRequest);
staticBody.SetMassFromShapes();
return staticBody;
}
}
}
下载:
OK 2012-06-07 创建静止的刚体.rar
(438.38 KB, 下载次数: 3)
|