CyanPrime
CyanPrime

Reputation: 5193

How do I calculate the Pitch between two vectors?

Okay, so I have 2 vectors, the enemy position vector, and the player position vector. I need to calculate the Pitch (The degree I would have to rotate something along the X axis from the enemy position to point to the player's Y position.)

How do I get the pitch between enemy position, and player position?

This was my last attempt and it seems to be stuck around 0 degrees.

player_pos.normalise();
enemy_pos.normalise();

float dot = Vector3f.dot(player_pos, enemy_pos);


Vector3f rotationVector = new Vector3f(0,0,0);
rotationVector. x = (float) Math.toDegrees(Math.acos(dot));

Upvotes: 0

Views: 1462

Answers (3)

dash-tom-bang
dash-tom-bang

Reputation: 17853

Something that you're missing in your initial stab is the current orientation of whatever it is that you're trying to point in a new direction. You'll need that if you want to figure out which way something needs to rotate.

You definitely do not want to normalize the positions. That operation isn't meaningful in this context. The dot product between two vectors gives you the angle between them but the dot product between (normalized) entity positions gives you an angle between those two items from the perspective of the origin, so this probably isn't what you want.

I'd suggest drawing on a piece of paper the information that you have and look at what information you want. Primarily, in this case, you want the angle between what and what? You want to change what in which entity?

You may find out that when you start drawing it all out that the data that you need to process and the operations that you need to use to do that processing become obvious.

Good luck!

Upvotes: 0

blinkymomo
blinkymomo

Reputation: 15

ok I am not quite sure what you mean, what you seem to have there is if the gun was at the origin pointing at the what is the angle between the enemy and the player.

I will assume you want to turn the enemies gun at the player.

first get the direction of the player relative to the enenmy //don't forget to check for zero

dir = player_pos -enemy_pos
dir.normalize()

angle = asin(dir.y) or angle = acos(dir.z)
angle = Math.toDegrees(angle);

you will have to adjust for quadrant (for asin)

if (z < 0) {
    if (y < 0) {
        angle = -180 - angle;
    }
    else {
        angle = 180 - angle;
    }
}

I don't do much game development so there might be a more efficient way, also I am not sure this is exactly what you want, it would be a lot easier if I could draw pictures

that was around the x axis, you may have meant around the z axis replace z with x

Upvotes: 0

ra4king
ra4king

Reputation: 594

Aren't you just looking for the difference angle?

double angle = Math.acos(Vector3f.dot(player_pos,enemy_pos) /
                         (player_pos.length() * enemy_pos.length()));

Upvotes: 1

Related Questions