How to fix this 90 or -90 rotation problem

hyillk :

I wrote this code used to create a plane in front of our character that we then use to check the position of enemies and know if they are inside the plane or not.

The code is in Java and is running on our server, and we use it in our game using Unity.

In the following code AreaWidth is the width of the plane we want to check, Area length is its length. Message is holding data about our player such as position and quaternion. bwNode is holding data about the current target that we are checking.

float AreaWidth = message.getWidth();
float AreaLength = message.getLength();

double Fi, cs, sn;

Fi = message.quaternion.toEulerAngles().getY();
cs = Math.cos(Fi);
sn = Math.sin(Fi);


int ptx, pty;
ptx = (int)(bwNode.getLoc().getX() - message.loc.getX());
pty = (int)(bwNode.getLoc().getZ() - message.loc.getZ());

double perplen, alonglen;
perplen = Math.abs(ptx * sn - pty * cs);
alonglen = ptx * cs + pty * sn;

if (perplen <= AreaWidth / 2)
{
    if (alonglen >= 0 && alonglen <= AreaLength)
    {
        //the target is inside the area
    }
}

So when debugging or actually using this code in game, it is working with 1 issue only, the plane is always facing either the right or left side of our player instead of always face the front/player facing direction.

Here is an image representing it for 0, 90, 180 and 270 degrees on our player Y rotation. (in this image the white cube is the player, the triangle shows where the player is facing, The sphere is the enemy, the red transparent box shows where our plane should be, and the colored lines forms the plane that is actually created.)

img

I need help figuring out what is causing it in my code.

Yuri Nudelman :

Well it is not a question about Unity as far as I understand, as it relates to your backend only.

Anyway, you are really mixing up the terms of target position and player position, pay attention that at your screens, the plane will always face the target, no matter where the player looks.

The way I would do it, is expressing the target in coordinate system of the player, something like this:

float AreaWidth = message.getWidth();
float AreaLength = message.getLength();

Fi = message.quaternion.toEulerAngles().getY();
cs = Math.cos(Fi);
sn = Math.sin(Fi);

// First, move the target from word axis origin to player axis origin
float tx = (bwNode.getLoc().getX() - message.loc.getX());
float tz = (bwNode.getLoc().getZ() - message.loc.getZ());

// Now rotate the target around it new word origin to apply player rotation
float ptx = cs * tx - sn * tz;
float ptz = sn * tx + cs * tz;

// Now ptx and ptz are in new word coordinates, simply test them agains area
if (-AreaWidth / 2 <= ptx && ptx <= AreaWidth / 2 && AreaLength => 0 && AreaLength <= l) {
    // It is inside
} else {
    // It is outside
}

P.S: It is probably much easier (and definitely more efficient) to pass player full transform matrix to the server instead of handling transform and rotation components separately.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=154031&siteId=1