简体   繁体   English

计算相对旋转

[英]Calculate relative rotation

I'm working on a simple "parking type" of game where the user is driving around in a car and has to park it a specified spot. 我正在开发一个简单的“停车类型”游戏,用户正在汽车中行驶,并且必须将其停在指定位置。

While it was actually working, the only problem is that I need to find out in what direction the car has been parked. 当它实际工作时,唯一的问题是我需要找出汽车停放的方向。 I don't want the user to just randomly park the car, but the car should be facing upwards or downwards. 我不希望用户只是随意停放汽车,但汽车应该朝上或朝下。

I tried using this check to see what rotation the car had, but this seems a bit too complex 我尝试使用此检查来查看汽车的旋转角度,但这似乎有点复杂

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))) {

Would there be an easier way to check this? 有没有更简单的方法来检查这一点? There should be a small margin of 5 degrees because it doesn't have to be perfect. 应该有一个5度的小余量,因为它不一定是完美的。

You can simplify it a bit by taking modulo 90 degrees: 您可以通过取模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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM