我正在使用box2d在flash中创建一个世界。我以前做过这个,我确信它很简单,但我无法识别它。
import Box2D.Common.Math.b2Vec2;
import Box2D.Dynamics.b2World;
import Box2D.Dynamics.b2BodyDef;
import Box2D.Dynamics.b2Body;
import Box2D.Collision.Shapes.b2CircleShape;
import Box2D.Dynamics.b2FixtureDef;
import Box2D.Dynamics.b2Fixture;
import flash.events.TimerEvent;
import flash.utils.Timer;
//variables
var startVelocity:b2Vec2 = new b2Vec2(50, 0);
//world defintion
var gravity:b2Vec2 = new b2Vec2(0,10);
var world:b2World = new b2World(gravity, true);
//object physics object
var objectBodyDef:b2BodyDef = new b2BodyDef();
objectBodyDef.type = b2Body.b2_dynamicBody;
var objectBody:b2Body = world.CreateBody(objectBodyDef);
objectBody.SetLinearVelocity(startVelocity);
//object shape definition. Change to box later
var objectShape:b2CircleShape = new b2CircleShape(5);
var objectFixtureDef:b2FixtureDef = new b2FixtureDef();
objectFixtureDef.shape = objectShape;
var objectFixture:b2Fixture = objectBody.CreateFixture(objectFixtureDef);
//outlines for objects
graphics.lineStyle(3, 0xff0000);
//Create world timing
var stepTimer:Timer = new Timer(0.025 * 1000);
stepTimer.addEventListener(TimerEvent.TIMER, onTick);
stepTimer.start();
function onTick(a_event:TimerEvent):void{
graphics.moveTo(objectBody.GetPosition().x, objectBody.GetPosition().y);
world.Step(0.25, 10, 10);
graphics.lineTo(objectBody.GetPosition().x, objectBody.GetPosition().y);
trace("objectBody x: " + objectBody.GetPosition().x +" objectBody y: " + objectBody.GetPosition().y);
}
我是从时间轴开始做的,所以这个问题与文档类无关。
上述代码在大多数情况下工作正常但是“对象”不像人们期望的那样在抛物线中移动。当x大约为12时,它会停止向右移动。无论我在起始速度向量的x分量之前使用什么值,都会发生这种情况。 我做错了什么?