motor2 基于Box2d的AS3(Player 10)刚体引擎
作者主页:http://lab.polygonal.de/motor_physics/
APE 最简单
http://www.cove.org/ape/
FOAM - 2D Rigid Body Physics Engine
http://code.google.com/p/foam-as3
glaze - 2D Rigid Body Dynamics & Game Engine for Actionsctipt 3
http://code.google.com/p/glaze/
Fisix Engine 收费的
http://www.fisixengine.com/default.asp
PopShapes
http://lab.andre-michelle.com/physics-engine/
Box2d 复杂,无api
http://box2dflash.sourceforge.net/ c++版
http://sourceforge.net/projects/box2d/ as版
http://wiki.slembcke.net/main/published/Chipmunk 基于box2d的c版,比box2d性能高
基于Box2d的游戏:
魔笔,只能在线玩(as)
http://www.miniclip.com/games/magic-pen/cn/magicpen.swf
怪物卡车,可下载玩,且有3d版的(as)
http://www.miniclip.com/games/monster-trucks-nitro/cn/
蜡笔物理学 (c++)
http://www.crayonphysics.com/
下载地址:http://www.kloonigames.com/blog/games/crayon
作者:http://www.kloonigames.com/blog/
box2d的碰撞检测采用AABB(axially aligned bounding box)(Box2D.Collision.b2AABB类)这种最简单的方式,采用一个描述用的立方体或者球形体包裹住物体对象的整体(或者是主要部 分),之后根据包装盒的距离、位置等信息来计算是否发生碰撞.
2D游戏中有许多图片都是方方正正的,所以我们不必把碰撞的范围画成一个圆的,而是画成一个方的。这个正方形,或者说是一个四边形和坐标轴是对齐的,所以运用数学上的一些方法,比如距离计算等还是比较方便的。这个检测方法就叫AABB碰撞检测,
游戏中已经运用的非常广泛了,因为其速度快,效率高,计算起来非常方便,精确度也是可以忍受的。 做到这一步,许多游戏的需求都已经满足了。但是,总是 有人希望近一步优化,而且方法也是非常陈旧的:继续对物体的各个部分进行细分,对每个部件做AABB的矩形,那这个优化以后的系统就叫做OBB系统 (Box2D.Collision.b2OBB类)。
使用流程:
1.创建world
2.定义刚体,定义形状,添加到世界
3.world.step()模拟计算
package com.mop{
import Box2D.Collision.Shapes.b2PolygonDef;
import Box2D.Collision.b2AABB;
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;
public class BoxTest extends Sprite {
public var worldAABB:b2AABB = new b2AABB();
public var gravity:b2Vec2 = new b2Vec2(0,10); //重力
public var doSleep:Boolean = true; //无互交的刚体不参与模拟计算 提高性能
public var world:b2World;
private var pixelsToMeter:Number = 30;//box2d中 1m = 30px
public function BoxTest() {
//包围体定义,物理世界模拟的范围
worldAABB.lowerBound.Set(-100,-100);
worldAABB.upperBound.Set(100,100);
//物理世界
world = new b2World(worldAABB,gravity,doSleep);
//刚体定义
var bodyDef:b2BodyDef = new b2BodyDef();
bodyDef.position.Set(8,11);//位置定义
bodyDef.userData = new PhysGround();
var half_groudNumber = 30;//物体的一半宽,单位m
var half_grounheight:Number = 3;
bodyDef.userData.width = pixelsToMeter*half_groudwidth*2;
bodyDef.userData.height = pixelsToMeter*half_grounheight*2;
addChild(bodyDef.userData);
//物理形定义
var boxDef:b2PolygonDef = new b2PolygonDef();
boxDef.SetAsBox(half_groudwidth,half_grounheight);
boxDef.density = 0; //密度为0,可漂浮
boxDef.friction = 0.3; //摩擦力
//根据刚体定义创建刚体实体
var body:b2Body = world.CreateBody(bodyDef);
//根据物理形定义设置刚体
body.CreateShape(boxDef);
//依据形计算质量
body.SetMassFromShapes();
//调试模式绘制
var dbgDraw:b2DebugDraw = new b2DebugDraw();
var dbgSprite:Sprite = new Sprite();
addChild(dbgSprite);
dbgDraw.m_sprite = dbgSprite;
dbgDraw.m_drawScale = 30.0;
dbgDraw.m_fillAlpha = 0.0;
dbgDraw.m_lineThickness = 1.0;
dbgDraw.m_drawFlags = 0xFFFFFFFF;
world.SetDebugDraw(dbgDraw);
//同理添加物体
for (var index:int = 0; index < 10; index++) {
bodyDef = new b2BodyDef();
bodyDef.position.x = Math.random() * 15 + 5;
bodyDef.position.y = Math.random() * 10;
var hW:Number = Math.random() + 0.5;
var hH:Number = Math.random() + 0.5;
boxDef = new b2PolygonDef();
boxDef.SetAsBox(hW, hH);
boxDef.density = 1.0;
boxDef.friction = 0.5;
boxDef.restitution = 0.2; //弹力
bodyDef.userData = new PhysBox();
bodyDef.userData.width = hW * 2 * pixelsToMeter;
bodyDef.userData.height = hH * 2 * pixelsToMeter;
body = world.CreateBody(bodyDef);
body.CreateShape(boxDef);
body.SetMassFromShapes();
addChild(bodyDef.userData);
}
addEventListener(Event.ENTER_FRAME, Update, false, 0, true);
}
public function Update(e:Event):void{
world.Step(1/30, 10);
for (var bb:b2Body = world.m_bodyList; bb; bb = bb.m_next){
if (bb.m_userData is Sprite){
bb.m_userData.x = bb.GetPosition().x * 30;
bb.m_userData.y = bb.GetPosition().y * 30;
bb.m_userData.rotation = bb.GetAngle() * (180/Math.PI);
}
}
}
}
}
import Box2D.Collision.Shapes.b2PolygonDef;
import Box2D.Collision.b2AABB;
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;
public class BoxTest extends Sprite {
public var worldAABB:b2AABB = new b2AABB();
public var gravity:b2Vec2 = new b2Vec2(0,10); //重力
public var doSleep:Boolean = true; //无互交的刚体不参与模拟计算 提高性能
public var world:b2World;
private var pixelsToMeter:Number = 30;//box2d中 1m = 30px
public function BoxTest() {
//包围体定义,物理世界模拟的范围
worldAABB.lowerBound.Set(-100,-100);
worldAABB.upperBound.Set(100,100);
//物理世界
world = new b2World(worldAABB,gravity,doSleep);
//刚体定义
var bodyDef:b2BodyDef = new b2BodyDef();
bodyDef.position.Set(8,11);//位置定义
bodyDef.userData = new PhysGround();
var half_groudNumber = 30;//物体的一半宽,单位m
var half_grounheight:Number = 3;
bodyDef.userData.width = pixelsToMeter*half_groudwidth*2;
bodyDef.userData.height = pixelsToMeter*half_grounheight*2;
addChild(bodyDef.userData);
//物理形定义
var boxDef:b2PolygonDef = new b2PolygonDef();
boxDef.SetAsBox(half_groudwidth,half_grounheight);
boxDef.density = 0; //密度为0,可漂浮
boxDef.friction = 0.3; //摩擦力
//根据刚体定义创建刚体实体
var body:b2Body = world.CreateBody(bodyDef);
//根据物理形定义设置刚体
body.CreateShape(boxDef);
//依据形计算质量
body.SetMassFromShapes();
//调试模式绘制
var dbgDraw:b2DebugDraw = new b2DebugDraw();
var dbgSprite:Sprite = new Sprite();
addChild(dbgSprite);
dbgDraw.m_sprite = dbgSprite;
dbgDraw.m_drawScale = 30.0;
dbgDraw.m_fillAlpha = 0.0;
dbgDraw.m_lineThickness = 1.0;
dbgDraw.m_drawFlags = 0xFFFFFFFF;
world.SetDebugDraw(dbgDraw);
//同理添加物体
for (var index:int = 0; index < 10; index++) {
bodyDef = new b2BodyDef();
bodyDef.position.x = Math.random() * 15 + 5;
bodyDef.position.y = Math.random() * 10;
var hW:Number = Math.random() + 0.5;
var hH:Number = Math.random() + 0.5;
boxDef = new b2PolygonDef();
boxDef.SetAsBox(hW, hH);
boxDef.density = 1.0;
boxDef.friction = 0.5;
boxDef.restitution = 0.2; //弹力
bodyDef.userData = new PhysBox();
bodyDef.userData.width = hW * 2 * pixelsToMeter;
bodyDef.userData.height = hH * 2 * pixelsToMeter;
body = world.CreateBody(bodyDef);
body.CreateShape(boxDef);
body.SetMassFromShapes();
addChild(bodyDef.userData);
}
addEventListener(Event.ENTER_FRAME, Update, false, 0, true);
}
public function Update(e:Event):void{
world.Step(1/30, 10);
for (var bb:b2Body = world.m_bodyList; bb; bb = bb.m_next){
if (bb.m_userData is Sprite){
bb.m_userData.x = bb.GetPosition().x * 30;
bb.m_userData.y = bb.GetPosition().y * 30;
bb.m_userData.rotation = bb.GetAngle() * (180/Math.PI);
}
}
}
}
}
原文地址:http://71mao.com/article/64.htm