angleBetween() only returning smallest angle. Need negative aswell

From these lines of code I calculate the position of my camera (above and behind the player):
[java]
double dx = (8 * Math.cos(Vector3f.UNIT_X.negate().angleBetween(player.getViewDirection()))) + playerPosition.getX();
double dz = (8 * Math.sin(Vector3f.UNIT_X.negate().angleBetween(player.getViewDirection()))) + playerPosition.getZ();
[/java]

The problem I’m having is that angleBetween() only returns a value between 0 and Pi, when I need it to return between 0 and 2*Pi. It’s only giving me the smallest angle.

Is there a different function I should be using for this or is there something else I’m overlooking?

It may also be useful to know player.getViewDirection returns a normalised vector.

Ok so this works for me, putting it here in case anyone has a similar problem. I used the Math.atan2 function to get the angle:
[java]
Vector3f viewDir = player.getViewDirection();
angle = FastMath.atan2(viewDir.getZ(), viewDir.getX());
float dx = (-8 * FastMath.cos(angle)) + playerPosition.getX();
float dz = (-8 * FastMath.sin(angle)) + playerPosition.getZ();
float lx = (1 * FastMath.cos(angle)) + playerPosition.getX();
float lz = (1 * FastMath.sin(angle)) + playerPosition.getZ();
desiredLocation.setX(dx);
desiredLocation.setZ(dz);
desiredLookAt.setX(lx);
desiredLookAt.setZ(lz);
[/java]

2Pi is 360 degrees. So if you want your angle out of 2Pi, just use 2*Pi-AngleBetween().
There’s no negative angle since there is no real spatial reference to determine what way is negative or positive. It’s between just two arbitrary vectors.

99% of the time when you have to resort to angles for something like this then there is a better way to do it just with vectors and quaternions.

I’m not exactly sure what the resulting effect should be so I don’t want to lead you down wrong paths.

But for example, your first lines turn a vector into an angle and then right back into a vector again. You could have just zeroed out the y of the viewDir (after cloning) and renormalized.

[java]
Vector3f project = player.getViewDirection().clone();
project.y = 0;
project.normalizeLocal();
desiredLocation.set(playerPosition.add(viewDir.mult(-8)));
desiredLookAt.set(playerPosition.add(viewDir));
[/java]

1 Like

I’m creating an object to control the game camera, I just felt ChaseCam didn’t give me enough fine control. Normally the camera is above and about -30 in the z direction of the player looking at him (similar to the camera in MGS3 (not the prototype MGS4 cam that was included in subsistence and the HD collection)). When the right mouse button is held down however, it goes into “action cam” and the camera moves behind the player, and must always face the direction of the player and move on a fixed orbit around him.

Since yesterday it’s working a lot better, but it’s far from finished.

Thanks for the replies, when it works how it should, I’ll be optimising, and will take everything above into consideration :slight_smile:

What is mgs3?

@Empire Phoenix said: What is mgs3?

MGS4 was really excellent… and only had about 20 hours of cut scenes. :slight_smile: I’ve never played 3.