[英]Decelerate while moving in direction
以下是您將看到的一些方法的快速描述:
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)。 此外,當它變得非常慢時,它只會真正向目標方向“漂移”。
關於我如何做到這一點的任何想法? 我就是想不通。 這是一個很大的問題,非常感謝。
這是使用我制作的一個大圖書館,所以如果您對某些東西有任何疑問,請詢問,但我想我已經涵蓋了大部分內容。
你的interpolate
函數做了奇怪的事情。 為什么不使用簡單的物理模型? 例如:
身體有位置(px, py)
和速度(vx, vy)
,單位方向向量(dx, dy)
也很方便
在(小)時間間隔 dt 之后,速度根據加速度而變化
vx = vx + ax * dt
vy = vy + ay * dt
任何外力(馬達)都會引起加速度並改變速度
ax = forcex / mass //mass might be 1
ay = forcey / mass
干摩擦力大小與速度無關,方向與速度相反
ax = c * mass * (-dx)
ay = c * mass * (-dy)
液體摩擦力的大小取決於速度(不同情況有許多不同的方程),它的方向與速度相反
ax = k * Vmagnitude * (-dx)
ay = k * Vmagnitude * (-dy)
因此,對於每個時間間隔添加所有力,找到產生的加速度,找到產生的速度,相應地改變位置
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.