[英]Calculate relative rotation
我正在開發一個簡單的“停車類型”游戲,用戶正在汽車中行駛,並且必須將其停在指定位置。
當它實際工作時,唯一的問題是我需要找出汽車停放的方向。 我不希望用戶只是隨意停放汽車,但汽車應該朝上或朝下。
我嘗試使用此檢查來查看汽車的旋轉角度,但這似乎有點復雜
var relativeRot = this.rotation % 360;
if((this._speed <= 0.02 && this._speed >= -0.02) && ((relativeRot <= 5 && relativeRot >= 355) || (relativeRot >= 175 && relativeRot <= 185) || (relativeRot <= -175 && relativeRot >= -185) || (relativeRot <= -5 && relativeRot >= -355))) {
有沒有更簡單的方法來檢查這一點? 應該有一個5度的小余量,因為它不一定是完美的。
您可以通過取模90度來稍微簡化一下:
var relativeRot = this.rotation % 360;
if (Math.abs(this._speed) <= 0.02) {
var cornerRot = (relativeRot + 360) % 90; // should be positive
if (Math.abs(cornerRot - 45) >= 40) {
// consider car parked...
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.