【9RIA—ladeng6666】—【Box2D系列教程 11】给圆角刚体穿上上衣
转载:9RIA游戏开发者社区(天地会)作者:ladeng6666(拉登大叔)
作者博客:http://www.ladeng6666.com/blog/
【Box2D系列教程-导航帖】—拉登大叔出品(总贴)
在创建圆角刚体里,我们学会了如何用组合法创建一个自定义圆角刚体,同时我还给自己留了个作业——给圆角刚体穿上上衣。今天我们就来完成这个作业。
其实,这并不是一个新的话题,在刚体的上衣——b2BodyDef.userData里我们就学习了自定义"上衣"的方法,很简单,将一个DisplayObject或子类对象赋值给b2bodyDef需求.
//1.创建刚体需求b2BodyDef
var bodyRequest:b2BodyDef = new b2BodyDef();
bodyRequest.position.Set(posX / 30, posY / 30);//记得米和像素的转换关系
//通过bodyRequest需求是userData属性设置"上衣"
bodyRequest.userData = createRoundFace(width, height, radius);
然后在loop函数里已经刚体的坐标、角度实时更新"上衣"的坐标、角度。
private function loop(e:Event):void
{
world.Step(1 / 30, 10);
//遍历所有的刚体,如果刚体的m_userData属性不为空
for ( var bodyIndex:b2Body=world.GetBodyList(); bodyIndex; bodyIndex = bodyIndex.GetNext()) {
if (bodyIndex.m_userData != null) {
//更新对应"上衣"的坐标、角度
bodyIndex.m_userData.x = bodyIndex.GetPosition().x * 30;
bodyIndex.m_userData.y = bodyIndex.GetPosition().y * 30;
bodyIndex.m_userData.rotation = bodyIndex.GetAngle() * 180 / Math.PI;
}
}
}
效果如下:
attach://787.swf
下载:
如果你对刚体的创建不是很熟悉,请参考掉落的苹果——b2Body刚体。真正的游戏中,不会使用Box2D的Debug图,所以大家一定要牢牢掌握好m_userData的用法,今后的学习中,我们还经常会用到这个知识。
2.1a版
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.Shape;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
public class YuanJiaoJuXingGangTi extends Sprite {
private var world:b2World;
private var body:b2Body;
private const WORLDSCALE:int = 30;
public function YuanJiaoJuXingGangTi() {
createWorld();
createGround();
createDebug();
addEventListener(Event.ENTER_FRAME, loop);
stage.addEventListener(MouseEvent.MOUSE_DOWN, onStageMouseDown);
}
private function createWorld():void {
var gravity:b2Vec2 = new b2Vec2(0, 10);
var doSleep:Boolean = true;
world = new b2World(gravity, doSleep);
}
private function createGround():void {
var bodyDef:b2BodyDef = new b2BodyDef();
bodyDef.type = b2Body.b2_staticBody;
bodyDef.position.Set(stage.stageWidth / 2 / WORLDSCALE, stage.stageHeight / WORLDSCALE);
body = world.CreateBody(bodyDef);
var polygonShape:b2PolygonShape = new b2PolygonShape();
polygonShape.SetAsBox(stage.stageWidth / WORLDSCALE, 0.5 / WORLDSCALE);
var fixtureDef:b2FixtureDef = new b2FixtureDef();
fixtureDef.density = 0;
fixtureDef.friction = 0.3;
fixtureDef.restitution = 0.2;
fixtureDef.shape = polygonShape;
body.CreateFixture(fixtureDef);
}
private function createDebug():void {
var debugSprite:Sprite = new Sprite();
addChild(debugSprite);
var debugDraw:b2DebugDraw = new b2DebugDraw();
debugDraw.SetSprite(debugSprite);
debugDraw.SetDrawScale(30);
debugDraw.SetAlpha(0.5);
debugDraw.SetLineThickness(1);
debugDraw.SetFlags(b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit);
world.SetDebugDraw(debugDraw);
}
private function createRoundPolygon(posX:Number, posY:Number, w:Number, h:Number, r:Number):void {
var offsetX:Number = w / 2 / WORLDSCALE - r / 2 / WORLDSCALE;
var offsetY:Number = h / 2 / WORLDSCALE - r / 2 / WORLDSCALE;
var bodyDef:b2BodyDef = new b2BodyDef();
bodyDef.type = b2Body.b2_dynamicBody;
bodyDef.position.Set(posX / WORLDSCALE, posY / WORLDSCALE);
bodyDef.userData = getARoundRectangle(w, h, r);
addChild(bodyDef.userData);
body = world.CreateBody(bodyDef);
var circleShape:b2CircleShape = new b2CircleShape();
circleShape.SetRadius(r / 2 / WORLDSCALE);
var polygonShape:b2PolygonShape = new b2PolygonShape();
var fixtureDef:b2FixtureDef = new b2FixtureDef();
fixtureDef.density = 3;
fixtureDef.friction = 0.3;
fixtureDef.restitution = 0.3;
fixtureDef.shape = polygonShape;
polygonShape.SetAsOrientedBox(w / 2 / WORLDSCALE, h / 2 / WORLDSCALE - r / 2 / WORLDSCALE, new b2Vec2());
body.CreateFixture(fixtureDef);
polygonShape.SetAsOrientedBox(w / 2 / WORLDSCALE - r / 2 / WORLDSCALE, h / 2 / WORLDSCALE, new b2Vec2());
body.CreateFixture(fixtureDef);
fixtureDef.shape = circleShape;
circleShape.SetLocalPosition(new b2Vec2(-offsetX, -offsetY));
body.CreateFixture(fixtureDef);
circleShape.SetLocalPosition(new b2Vec2(offsetX, -offsetY));
body.CreateFixture(fixtureDef);
circleShape.SetLocalPosition(new b2Vec2(-offsetX, offsetY));
body.CreateFixture(fixtureDef);
circleShape.SetLocalPosition(new b2Vec2(offsetX, offsetY));
body.CreateFixture(fixtureDef);
}
private function onStageMouseDown(e:MouseEvent):void {
createRoundPolygon(mouseX, mouseY, 40, 40, 20);
}
private function getARoundRectangle(width:Number, height:Number, radius:Number):Shape {
var s:Shape = new Shape();
s.graphics.beginFill(0xFFFFFF * Math.random());
s.graphics.drawRoundRect(-width / 2, -height / 2, width, height, radius, radius);
s.graphics.endFill();
return s;
}
private function loop(e:Event):void {
world.Step(1 / 30, 10, 10);
world.ClearForces();
world.DrawDebugData();
for (var body:b2Body = world.GetBodyList(); body; body = body.GetNext()) {
if (body.GetUserData() != null) {
body.GetUserData().x = body.GetPosition().x * WORLDSCALE;
body.GetUserData().y = body.GetPosition().y * WORLDSCALE;
body.GetUserData().rotation = body.GetAngle() * 180 / Math.PI;
}
}
}
}
}
新手来学习学习了 新手来学习学习,感谢大佬指导
页:
[1]