給你的對象/實體/船的位置(的Vector3)和旋轉(矩陣),然後就可以使用下面的代碼(在這個答案底部的樣品)來移動船周圍。
例如移動船前進5個單位:
Entity myShip = new Entity();
myShip.GoForward(5.0f);
爲了讓你的船,桶滾90度
myShip.Roll(MathHelper.PiOver2);
而這裏的示例代碼
public class Entity
{
Vector3 position = Vector3.Zero;
Matrix rotation = Matrix.Identity;
public void Yaw(float amount)
{
this.rotation *= Matrix.CreateFromAxisAngle(this.rotation.Up, amount);
}
public void YawAroundWorldUp(float amount)
{
this.rotation *= Matrix.CreateRotationY(amount);
}
public void Pitch(float amount)
{
this.rotation *= Matrix.CreateFromAxisAngle(this.rotation.Right, amount);
}
public void Roll(float amount)
{
this.rotation *= Matrix.CreateFromAxisAngle(this.rotation.Forward, amount);
}
public void Strafe(float amount)
{
this.position += this.rotation.Right * amount;
}
public void GoForward(float amount)
{
this.position += this.rotation.Forward * amount;
}
public void Jump(float amount)
{
this.position += this.rotation.Up * amount;
}
public void Rise(float amount)
{
this.position += Vector3.Up * amount;
}
}
就積分速度/加速度而言,在恆定加速度a下,牛頓運動隨時間dt的正確公式爲:起動速度v0和起始位置x0爲:x = x0 + v0 * dt + 0.5 * a * dt * dt。新的速度是v = v0 + a * dt。這些方程適用於三維矢量(速度和加速度本身就是矢量)。 –
感謝您的回覆,使用這個公式我仍然需要維護一個世界矩陣,以便船舶轉變爲世界空間或者這不是必要的嗎? –
通常,您基於演員的位置和方向(在這種情況下爲ship)構造世界矩陣。假設你跟蹤的位置是世界位置,你只需將世界矩陣的翻譯部分替換爲演員的位置即可。 –