Sign in to follow this  
Cappah

Obtaining angle of object in relation to camera direction

Recommended Posts

What I'm trying to achieve is get the angle that an object is in relation to the direction the camera is facing.

From the image I've provided:

Point A: Camera position.

Point B: Reference point which is always in front of the camera's view regardless of rotation.

Point C: Object in question.

 

Calculating the angle between point B and C is fairly easy, but the problem is that the yaw of my camera (in degrees) is according to world space and not in view(camera) space. This becomes problematic depending on which Cartesian Quadrant the camera position currently resides in.  

 

How do I define degrees based on the camera's direction independent of world space?

Share this post


Link to post
Share on other sites

Use the Law of Cosines:

 

b ? c = ||b|| ||c|| cos ?

 

If b and c are unit vectors this becomes:

 

b ? c = cos ?

 

Then to find the angle ?:

 

? = acos b ? c

 

Convert between radians and degrees as needed.

Sorry, but I'm not a mathematician, so I can't read math notation. Plus you didn't explain how this applies

Share this post


Link to post
Share on other sites

 

Use the Law of Cosines:

 

b ? c = ||b|| ||c|| cos ?

 

If b and c are unit vectors this becomes:

 

b ? c = cos ?

 

Then to find the angle ?:

 

? = acos b ? c

 

Convert between radians and degrees as needed.

Sorry, but I'm not a mathematician, so I can't read math notation. Plus you didn't explain how this applies

 

This is a trigonometric question. If you don't understand the answer someone gives you, maybe you should take a class before down voting someone.

 

Anyways, could you please clarify your question? You are asking how to get the angle, but later state you know how to get the angle. What do you mean by "degrees independent of world space"? You can treat your point B as if it is in standard position, then use your object C as the terminal branch. world view and camera view don't have anything to do with it. Please elaborate.

Edited by ExErvus

Share this post


Link to post
Share on other sites

Giving upvotes because the answer is correct.

 

The mathematics of 3D worlds is linear algebra.  You need to understand the fundamentals of linear algebra to program 3D software effectively.  Otherwise you will be entirely at the mercy of people online providing your basic formulas every time you need them.

 

The mathematics of 2D worlds is trigonometry and geometry. You need to understand the fundamentals to program 2D software effectively.

 

For how to apply the law of cosines, you can compute the angle between any two vectors using the function provided above. It applies just as well in 2D if you set the third component to zero; the two vectors are the legs of a triangle and the cosine (or inverse cosine) is used for calculating the angle between the legs of the triangle.

 

If you need the last line without 'math notation', the angle is equal to the inverse cosine of the dot product of two normalized vectors.  If you don't know what those words mean, work through this or similar.

Share this post


Link to post
Share on other sites

Giving upvotes because the answer is correct.

 

The mathematics of 3D worlds is linear algebra.  You need to understand the fundamentals of linear algebra to program 3D software effectively.  Otherwise you will be entirely at the mercy of people online providing your basic formulas every time you need them.

 

The mathematics of 2D worlds is trigonometry and geometry. You need to understand the fundamentals to program 2D software effectively.

 

For how to apply the law of cosines, you can compute the angle between any two vectors using the function provided above. It applies just as well in 2D if you set the third component to zero; the two vectors are the legs of a triangle and the cosine (or inverse cosine) is used for calculating the angle between the legs of the triangle.

 

If you need the last line without 'math notation', the angle is equal to the inverse cosine of the dot product of two normalized vectors.  If you don't know what those words mean, work through this or similar.

Ok, let me clarify, because it seems that the other answers are focused on the first half, but not the last half of what I said.

I have already calculated the angle between points B and C. That's not the problem whatsoever, so that part is already solved.

 

The problem (tested in my program) is that yaw of the camera is dependent upon world space with z = north (0), -z = south (180), x = east(90) and -x = west(270). 

Now I would have already been done just from the first half if the camera was statically placed at 0, y, 0 (since I'm only checking the horizontal plane), but this is not the case. The camera can be anywhere in the world (anywhere of the four Quadrants). This makes the yaw of the camera problematic as I will explain next.

 

Given the same image, if you move the diagram to the Quadrant I, II, III or IV, the world coordinates are valid in relation to the camera's orientation and the angle, plus the angle of point B and C matches that of the world.

 

But here is where everything fails:

Rotate the direction of the camera and point B for example 180 degrees, but keep point C in the same spot. Point C will still be 270 degrees in relation to the world, but in relation to the camera's direction (point B always = 0 degrees), point C = 90 degrees.

 

So the problem is not the angle between point B and C. The problem is defining degrees specific to the camera's direction it is facing, which will always be 0 degrees no matter which way it rotates.

If you dont understand that, the problem is no different than getting proper orientation. if you are facing the world's north and your dog is standing to your left, you understand that not only the dog is to the left of you, but the dog in reference to the world is also west of you.

But if you were to turn 90 degrees counter clockwise, the dog will still be west of you, but is the dog still to the left of you? NO. The dog is now in front of you. 

 

So roughly, as the world has its own Cartesian coordinate system, the camera needs an independent version based on the direction the camera is pointing.

 

P.S. Not all programmers read/write math notation and I don't appreciate the condescending response. Now if I give you sentence written in Farsi writing, should you know how to say it or should it written phonetically so you can say it and use it? Same goes with programming. A lot of people can't read mathematically notations, but they sure know how to program the same equations if it's written in a form they understand such as when you said "the angle is equal to the inverse cosine of the dot product of two normalized vectors." 

Edited by Cappah

Share this post


Link to post
Share on other sites

 

Giving upvotes because the answer is correct.

 

The mathematics of 3D worlds is linear algebra.  You need to understand the fundamentals of linear algebra to program 3D software effectively.  Otherwise you will be entirely at the mercy of people online providing your basic formulas every time you need them.

 

The mathematics of 2D worlds is trigonometry and geometry. You need to understand the fundamentals to program 2D software effectively.

 

For how to apply the law of cosines, you can compute the angle between any two vectors using the function provided above. It applies just as well in 2D if you set the third component to zero; the two vectors are the legs of a triangle and the cosine (or inverse cosine) is used for calculating the angle between the legs of the triangle.

 

If you need the last line without 'math notation', the angle is equal to the inverse cosine of the dot product of two normalized vectors.  If you don't know what those words mean, work through this or similar.

Ok, let me clarify, because it seems that the other answers are focused on the first half, but not the last half of what I said.

I have already calculated the angle between points B and C. That's not the problem whatsoever, so that part is already solved.

 

The problem (tested in my program) is that yaw of the camera is dependent upon world space with z = north (0), -z = south (180), x = east(90) and -x = west(270). 

Now I would have already been done just from the first half if the camera was statically placed at 0, y, 0 (since I'm only checking the horizontal plane), but this is not the case. The camera can be anywhere in the world (anywhere of the four Quadrants). This makes the yaw of the camera problematic as I will explain next.

 

Given the same image, if you move the diagram to the Quadrant I, II, III or IV, the world coordinates are valid in relation to the camera's orientation and the angle, plus the angle of point B and C matches that of the world.

 

But here is where everything fails:

Rotate the direction of the camera and point B for example 180 degrees, but keep point C in the same spot. Point C will still be 270 degrees in relation to the world, but in relation to the camera's direction (point B always = 0 degrees), point C = 90 degrees.

 

So the problem is not the angle between point B and C. The problem is defining degrees specific to the camera's direction it is facing, which will always be 0 degrees no matter which way it rotates.

If you dont understand that, the problem is no different than getting proper orientation. if you are facing the world's north and your dog is standing to your left, you understand that not only the dog is to the left of you, but the dog in reference to the world is also west of you.

But if you were to turn 90 degrees counter clockwise, the dog will still be west of you, but is the dog still to the left of you? NO. The dog is now in front of you. 

 

So roughly, as the world has its own Cartesian coordinate system, the camera needs an independent version based on the direction the camera is pointing.

 

P.S. Not all programmers read/write math notation and I don't appreciate the condescending response. Now if I give you sentence written in Farsi writing, should you know how to say it or should it written phonetically so you can say it and use it? Same goes with programming. A lot of people can't read mathematically notations, but they sure know how to program the same equations if it's written in a form they understand such as when you said "the angle is equal to the inverse cosine of the dot product of two normalized vectors." 

 

 

Measure the angle from the world to C. Then measure to B. Then subtract B from C. If C is 180 and B is 45, then the number of degrees between B and C is 135. Also you should be working with radians, not degrees. I suggest you study this https://en.wikipedia.org/wiki/Unit_circle.

 

If the answer is still off base, their exists some other issue. From your description it is difficult to tell what you want to solve.

 

"But here is where everything fails:

Rotate the direction of the camera and point B for example 180 degrees, but keep point C in the same spot. Point C will still be 270 degrees in relation to the world, but in relation to the camera's direction (point B always = 0 degrees), point C = 90 degrees"

 

So what do you want C degrees to be? Is 90 wrong for your use case?

 

I apologize, I did not mean to come off condescending. If you hadn't down voted I would not have sounded so harsh.

Share this post


Link to post
Share on other sites

 

 

Giving upvotes because the answer is correct.

 

The mathematics of 3D worlds is linear algebra.  You need to understand the fundamentals of linear algebra to program 3D software effectively.  Otherwise you will be entirely at the mercy of people online providing your basic formulas every time you need them.

 

The mathematics of 2D worlds is trigonometry and geometry. You need to understand the fundamentals to program 2D software effectively.

 

For how to apply the law of cosines, you can compute the angle between any two vectors using the function provided above. It applies just as well in 2D if you set the third component to zero; the two vectors are the legs of a triangle and the cosine (or inverse cosine) is used for calculating the angle between the legs of the triangle.

 

If you need the last line without 'math notation', the angle is equal to the inverse cosine of the dot product of two normalized vectors.  If you don't know what those words mean, work through this or similar.

Ok, let me clarify, because it seems that the other answers are focused on the first half, but not the last half of what I said.

I have already calculated the angle between points B and C. That's not the problem whatsoever, so that part is already solved.

 

The problem (tested in my program) is that yaw of the camera is dependent upon world space with z = north (0), -z = south (180), x = east(90) and -x = west(270). 

Now I would have already been done just from the first half if the camera was statically placed at 0, y, 0 (since I'm only checking the horizontal plane), but this is not the case. The camera can be anywhere in the world (anywhere of the four Quadrants). This makes the yaw of the camera problematic as I will explain next.

 

Given the same image, if you move the diagram to the Quadrant I, II, III or IV, the world coordinates are valid in relation to the camera's orientation and the angle, plus the angle of point B and C matches that of the world.

 

But here is where everything fails:

Rotate the direction of the camera and point B for example 180 degrees, but keep point C in the same spot. Point C will still be 270 degrees in relation to the world, but in relation to the camera's direction (point B always = 0 degrees), point C = 90 degrees.

 

So the problem is not the angle between point B and C. The problem is defining degrees specific to the camera's direction it is facing, which will always be 0 degrees no matter which way it rotates.

If you dont understand that, the problem is no different than getting proper orientation. if you are facing the world's north and your dog is standing to your left, you understand that not only the dog is to the left of you, but the dog in reference to the world is also west of you.

But if you were to turn 90 degrees counter clockwise, the dog will still be west of you, but is the dog still to the left of you? NO. The dog is now in front of you. 

 

So roughly, as the world has its own Cartesian coordinate system, the camera needs an independent version based on the direction the camera is pointing.

 

P.S. Not all programmers read/write math notation and I don't appreciate the condescending response. Now if I give you sentence written in Farsi writing, should you know how to say it or should it written phonetically so you can say it and use it? Same goes with programming. A lot of people can't read mathematically notations, but they sure know how to program the same equations if it's written in a form they understand such as when you said "the angle is equal to the inverse cosine of the dot product of two normalized vectors." 

 

 

Measure the angle from the world to C. Then measure to B. Then subtract B from C. If C is 180 and B is 45, then the number of degrees between B and C is 135. Also you should be working with radians, not degrees. I suggest you study this https://en.wikipedia.org/wiki/Unit_circle.

 

If the answer is still off base, their exists some other issue. From your description it is difficult to tell what you want to solve.

 

"But here is where everything fails:

Rotate the direction of the camera and point B for example 180 degrees, but keep point C in the same spot. Point C will still be 270 degrees in relation to the world, but in relation to the camera's direction (point B always = 0 degrees), point C = 90 degrees"

 

So what do you want C degrees to be? Is 90 wrong for your use case?

 

I apologize, I did not mean to come off condescending. If you hadn't down voted I would not have sounded so harsh.

 

If you add 180 degrees to the camera direction/Point B, it will be point 225 degrees in relation to the world. The angle between B and C will still be 90 degrees, but point C in relation to point B will be 90 degrees (to the right of the camera based on the direction it is pointing).

 

To make the explanation as simple is as possible, you have to relate the camera to yourself and world space to Earth. The cardinal directions never change, but you do depending on the direction you are facing. The direction you face is always 0 degrees. So no matter what cardinal direction you face whether N,NW,NE,S,SW,SE in the world, if an object is directly to your right, it is 90 degrees to your right. If an object is behind you, it is 180 degrees behind you regardless of the direction you face.

 

You've seen this enacted in games, movies and real life. It's applied in navigation points (arrows pointing offscreen to orientate you to your destination). Or most notably, 3D sound in a game. if an object blows up to the left of you, you get the angle of the position of the explosion in relation to the direction you are facing so that you can properly pan the sound in your speaker/headphones to the left, right or center.

 

So point C is the angle (clockwise) was from 0 (camera direction). This is why the camera's facing direction must always remain 0. If it went by world space, the camera direction would be arbitrary and will give false values based on where the camera and point C is positioned in the world. 

 

No apology needed. It's all gravy.

Share this post


Link to post
Share on other sites
For computing the angles the law of cosines still applies. If you need to rotate from one coordinate space to another do that by multiplying the vector and the transformation (or the transformation by the vector if that's your data layout), then you're in the different coordinate space. Once you're in the same space, law of cosine computes your angle.

Your updated description does not change that. The angles are still calculated by the same formula, no matter what points are involved or what transformations they go through.

Share this post


Link to post
Share on other sites

 

Sorry, but I'm not a mathematician, so I can't read math notation. Plus you didn't explain how this applies

 

 

I had to make some assumptions because your question wasn't very clear. One of which is your level of education. This is basic high school grade trigonometry, not higher mathematics. As for how this applies you're right, I guess I should have elaborated a bit more on that. I hope these fine people were able to clear that up for you.

Share this post


Link to post
Share on other sites

rel heading = atan(dx/dz)-yr, whats the big deal?  get your signs straight and trap out divide by zero, that's about it.  and that's all in world coordinates, BTW.

 

trig is the the math discipline behind that. if you want to program games, learn trig - then learn linear.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this