Actionscript 3 为box2D世界对象创建边界
我用box2D制作了物理效果,下面是我的代码集Actionscript 3 为box2D世界对象创建边界,actionscript-3,apache-flex,flex4,box2d,Actionscript 3,Apache Flex,Flex4,Box2d,我用box2D制作了物理效果,下面是我的代码集 public class Main extends SpriteVisualElement { public var world:b2World; public var wheelArray:Array; public var stepTimer:Timer; public var scaleFactor:Number = 20; public f
public class Main extends SpriteVisualElement
{
public var world:b2World;
public var wheelArray:Array;
public var stepTimer:Timer;
public var scaleFactor:Number = 20;
public function Main()
{
if (stage)
init();
else
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
getStarted();
}
private function getStarted():void
{
var gravity:b2Vec2 = new b2Vec2(0, 10);
world = new b2World(gravity, true);
wheelArray = new Array();
for (var i:int = 0; i < 20; i++)
{
createWheel(
Math.random() * 0.5,
Math.random() * (stage.stageWidth - 20) + 10,
Math.random() * (stage.stageHeight - 20) + 10,
(Math.random() * 100) - 50,
0
);
}
createBoundaries();
stepTimer = new Timer(0.025 * 1000);
stepTimer.addEventListener(TimerEvent.TIMER, onTick);
graphics.lineStyle(3, 0xff0000);
stepTimer.start();
}
private function createBoundaries():void
{
trace(this.height,this.width,stage.height,stage.width);
// need some code here
}
protected function onTick(event:TimerEvent):void
{
graphics.clear();
graphics.lineStyle(3, 0xff0000);
world.Step(0.025, 10, 10);
for each (var wheelBody:b2Body in wheelArray)
{
graphics.drawCircle(
wheelBody.GetPosition().x * scaleFactor,
wheelBody.GetPosition().y * scaleFactor,
(wheelBody.GetFixtureList().GetShape() as b2CircleShape).GetRadius() * scaleFactor
);
}
}
private function createWheel(radius:Number, startX:Number, startY:Number, velocityX:Number, velocityY:Number):void
{
var wheelBodyDef:b2BodyDef = new b2BodyDef();
wheelBodyDef.type = b2Body.b2_dynamicBody;
wheelBodyDef.position.Set(startX / scaleFactor, startY / scaleFactor);
var wheelBody:b2Body = world.CreateBody(wheelBodyDef);
var circleShape:b2CircleShape = new b2CircleShape(radius);
var wheelFixtureDef:b2FixtureDef = new b2FixtureDef();
wheelFixtureDef.shape = circleShape;
wheelFixtureDef.restitution = (Math.random() * 0.5) + 0.5;
wheelFixtureDef.friction = (Math.random() * 1.0);
wheelFixtureDef.density = Math.random() * 20;
var wheelFixture:b2Fixture = wheelBody.CreateFixture(wheelFixtureDef);
var startingVelocity:b2Vec2 = new b2Vec2(velocityX, velocityY);
wheelBody.SetLinearVelocity(startingVelocity);
wheelArray.push(wheelBody);
}
}
}
它的工作,但在检测阶段的边界问题。
目标:我想设置边界,因此任何帮助…Box2D没有“边界”系统,因此,您必须
- 创建实体/实体并将其放置在需要边界的位置
- 另一件你必须注意的事情是隧道挖掘。你必须正确处理它,这样就不会有物体通过边界。您可以做的一件事是将快速移动对象的
-标志设置为truebullet
var main:Main = new Main();
this.addElement(main);