0
我在Andengine.B中有box2d物體,我想一次將這個物體從(0,0)移動到(100,100)。怎麼可能?我試過這段代碼:this.body.setLinearVelocity(new Vector2(1,0));但它正在不停地移動。恆定的box2d物體一次移動指向
我在Andengine.B中有box2d物體,我想一次將這個物體從(0,0)移動到(100,100)。怎麼可能?我試過這段代碼:this.body.setLinearVelocity(new Vector2(1,0));但它正在不停地移動。恆定的box2d物體一次移動指向
我想沿預定義路徑移動的最簡單方法是使用Body.setTransform(...)
。這樣,我們基本上忽略了所有的力量,摩擦,扭矩,碰撞等,並直接設定身體的位置。
我不知道Andengine,所以這只是僞代碼:
public void updateGameLoop(float deltaTime) {
Vector2 current = body.getPosition();
Vector2 target = new Vector2(100, 100);
if (!current.equals(target)) {
float speed = 20f;
Vector2 direction = target.sub(current);
float distanceToTarget = direction.len();
float travelDistance = speed * deltaTime;
// the target is very close, so we set the position to the target directly
if (distanceToTarget <= travelDistance) {
body.setTransform(target, body.getAngle());
} else {
direction.nor();
// move a bit in the target direction
body.setTransform(current.add(direction.mul(travelDistance)), body.getAngle());
}
}
}
使用'setTransform()'移動對象會導致模擬不能最佳執行,並可能導致錯誤。 – dimitris93
你的問題是不明確的。你想讓它像瞬間移動一樣瞬間移動,還是以恆定速度移動?身體移動不停的問題是什麼?你想讓它停止在兩者之間移動嗎?你有重力嗎?也可以使用新的Vector2(1,1)來代替... – noone
我有一個body.I希望它在(0,0)px和(100,100)px之間移動,不像傳送端。我希望它在5秒內移動( V速度或2V速度)。不傳送。 – immyth
假設你沒有任何重力和線性阻尼的身體設置爲0,你的像素與米比例是1,一個簡單的'body.setLinearVelocity(new Vector2(20,20))'應該做這項工作。但是如果沒有更多關於你的scenerio的信息,沒有人能夠幫助你。 – noone