以下是對一些你會看到的方法做了簡單描述:減速而移動的方向
this.bounds
:返回船的邊界(矩形)this.bounds.getCenter()
:返回表示一個Vector2d
船的邊界的中心。getVelocity()
:甲Vector2d
表示該船舶的速度(添加到每個幀的位置)new Vector2d(angle)
:一個新的Vector2d
,歸一化的,給定角度時(以弧度表示)Vector2d#interpolate(Vector2d target, double amount)
:未線性插值!如果你想看到interpolate
代碼,這是(在Vector2d
類):public Vector2d interpolate(Vector2d target, double amount) { if (getDistanceSquared(target) < amount * amount) return target; return interpolate(getAngle(target), amount); } public Vector2d interpolate(double direction, double amount) { return this.add(new Vector2d(direction).multiply(amount)); }
當玩家不按按鍵,船應該只是減速。以下是我所做的:
public void drift() {
setVelocity(getVelocity().interpolate(Vector2d.ZERO, this.deceleration));
}
但是,現在我已經意識到我希望它在朝向目標移動的同時進行漂移。我已經試過這樣:
public void drift(Vector2d target) {
double angle = this.bounds.getCenter().getAngle(target);
setVelocity(getVelocity().interpolate(new Vector2d(angle), this.deceleration));
}
這當然永遠不會真正達到零速度(因爲我朝的角度矢量,其中有1的幅度插值)。而且,當它變得非常緩慢時,它只會在目標方向上「漂移」。
關於我如何完成此任何想法?我無法弄清楚。這是一個很大的問題,所以非常感謝。
這是使用我製作的大型圖書館,所以如果您有任何問題,請諮詢一些內容,但我想我已經介紹了大部分內容。
我不明白你想做什麼。如果你只是想減速,那就減少速度矢量的長度。如果您假設[Rayleigh drag](https://en.wikipedia.org/wiki/Drag_(physics)#Drag_at_high_velocity),則阻力以速度的二次方增長。在[Stokes drag](https://en.wikipedia.org/wiki/Drag_(physics)#Very_low_Reynolds_numbers:_Stokes.27_drag)的情況下,它會線性增長。假設一個恆定的船舶質量和一個不斷更新的時間框架,你可以這樣做:'dragForce = constant * v.length()(^ 2); v = v *(1 - dragForce/v.length())'。 –
順便說一句,如果它對你的問題至關重要,請不要鏈接到外部內容。據我所知,如果你輸入零向量(因爲它的長度小於'amount * amount'),你的插值函數將返回零向量。 –
@NicoSchertler對不起,我不得不張貼在pastebin上,因爲它沒有格式化在這裏,無論我嘗試了什麼。插值確實有效(我多次使用它)。此時,當沒有按鍵被按下時,船隻減速,直到它完全停止,而沿着它正在移動的最後方向。我希望這艘船能做到這一點,但是在某個方向上。之前,我可以將速度內插到零。但我不知道該怎麼做,如果我希望它能夠改變方向而放慢速度。如果它仍然很難理解,可能會發布一些圖片,如果這可能有幫助。 – MCMastery