简体   繁体   中英

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.

You can simplify it a bit by taking modulo 90 degrees:

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...
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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