How to make an object face another object in 3D space?
up vote
1
down vote
favorite
I have a question that maybe it is an easy calculation but I am a motion graphic designer not a Math guy so... here it goes.
I have a 3D scene with a perspective camera in it. I want to make a 2D object in the scene to rotate on Y axis to always look in the direction of the camera so from the Camera point of view it won't be visible as a 2D object.
So I can get the values of camera X and Z position and use them to calculate the Y rotation but I am not sure what is the math I should do.
currently I want to leave camera Y position (height) out of it.
I am attaching some photos to maybe make it clearer:
Pos1
Pos2
Pos3
Thanks in advance.
3d
add a comment |
up vote
1
down vote
favorite
I have a question that maybe it is an easy calculation but I am a motion graphic designer not a Math guy so... here it goes.
I have a 3D scene with a perspective camera in it. I want to make a 2D object in the scene to rotate on Y axis to always look in the direction of the camera so from the Camera point of view it won't be visible as a 2D object.
So I can get the values of camera X and Z position and use them to calculate the Y rotation but I am not sure what is the math I should do.
currently I want to leave camera Y position (height) out of it.
I am attaching some photos to maybe make it clearer:
Pos1
Pos2
Pos3
Thanks in advance.
3d
It sounds like you want to use the $mathrm{atan2}$ function somehow, perhaps $mathrm{atan2}(X,Z).$ You might have to experiment a bit to find out exactly how it fits in your environment.
– David K
Dec 3 at 13:06
if my object can rotate 360 degrees I assume that it should be in the function somehow, no?
– Brillll
Dec 3 at 13:32
$mathrm{atan2}$ does give you a full $360$ degrees of output, although it usually gives it in radians.
– David K
Dec 3 at 13:35
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have a question that maybe it is an easy calculation but I am a motion graphic designer not a Math guy so... here it goes.
I have a 3D scene with a perspective camera in it. I want to make a 2D object in the scene to rotate on Y axis to always look in the direction of the camera so from the Camera point of view it won't be visible as a 2D object.
So I can get the values of camera X and Z position and use them to calculate the Y rotation but I am not sure what is the math I should do.
currently I want to leave camera Y position (height) out of it.
I am attaching some photos to maybe make it clearer:
Pos1
Pos2
Pos3
Thanks in advance.
3d
I have a question that maybe it is an easy calculation but I am a motion graphic designer not a Math guy so... here it goes.
I have a 3D scene with a perspective camera in it. I want to make a 2D object in the scene to rotate on Y axis to always look in the direction of the camera so from the Camera point of view it won't be visible as a 2D object.
So I can get the values of camera X and Z position and use them to calculate the Y rotation but I am not sure what is the math I should do.
currently I want to leave camera Y position (height) out of it.
I am attaching some photos to maybe make it clearer:
Pos1
Pos2
Pos3
Thanks in advance.
3d
3d
asked Dec 3 at 12:56
Brillll
61
61
It sounds like you want to use the $mathrm{atan2}$ function somehow, perhaps $mathrm{atan2}(X,Z).$ You might have to experiment a bit to find out exactly how it fits in your environment.
– David K
Dec 3 at 13:06
if my object can rotate 360 degrees I assume that it should be in the function somehow, no?
– Brillll
Dec 3 at 13:32
$mathrm{atan2}$ does give you a full $360$ degrees of output, although it usually gives it in radians.
– David K
Dec 3 at 13:35
add a comment |
It sounds like you want to use the $mathrm{atan2}$ function somehow, perhaps $mathrm{atan2}(X,Z).$ You might have to experiment a bit to find out exactly how it fits in your environment.
– David K
Dec 3 at 13:06
if my object can rotate 360 degrees I assume that it should be in the function somehow, no?
– Brillll
Dec 3 at 13:32
$mathrm{atan2}$ does give you a full $360$ degrees of output, although it usually gives it in radians.
– David K
Dec 3 at 13:35
It sounds like you want to use the $mathrm{atan2}$ function somehow, perhaps $mathrm{atan2}(X,Z).$ You might have to experiment a bit to find out exactly how it fits in your environment.
– David K
Dec 3 at 13:06
It sounds like you want to use the $mathrm{atan2}$ function somehow, perhaps $mathrm{atan2}(X,Z).$ You might have to experiment a bit to find out exactly how it fits in your environment.
– David K
Dec 3 at 13:06
if my object can rotate 360 degrees I assume that it should be in the function somehow, no?
– Brillll
Dec 3 at 13:32
if my object can rotate 360 degrees I assume that it should be in the function somehow, no?
– Brillll
Dec 3 at 13:32
$mathrm{atan2}$ does give you a full $360$ degrees of output, although it usually gives it in radians.
– David K
Dec 3 at 13:35
$mathrm{atan2}$ does give you a full $360$ degrees of output, although it usually gives it in radians.
– David K
Dec 3 at 13:35
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
As David K already suggested you can use atan2
funtion.
It's a special version of arcus tangens
that takes into account all possible ranges and signs of input values and returns an angle.
Just compute a vector from your rotating object to target (Camera) and put its X and Y into an atan2
function. The result will be a rotation angle, in Radians.
add a comment |
up vote
0
down vote
That's basic linear algebra.
We use only X,Z cohordinates so we are working in the Euclidean plane $mathbb R^2$.
Put the origin in the point where the axis of your 2d object is placed. And works in such cohordinates. In practice if $(X_0,Z_0)$ are the cohordinates of the 2d object you use coordinates $(X-X_0,Z-Z_0)$ instead of $(X,Z)$.
Now some Thery:
In $mathbb R^2$ linear transformations that fixes the origin (and a rotation is one of them) are determined by $2times 2$ matrices.
Consider points $e_1=(1,0)$ and $e_2=(0,1)$. Then the matrix $begin{pmatrix} a & b\ c & dend{pmatrix}$ sends $e_1$ to point $(a,c)$ and $e_2$ to point $(b,d)$.
And in general $begin{pmatrix} a & b\ c & dend{pmatrix}begin{pmatrix} x \ y end{pmatrix}=begin{pmatrix} ax + by\ cx+ dyend{pmatrix}$, where $begin{pmatrix} x \ y end{pmatrix}$ is another way to say $(x,y)$.
(see here for more details on matrix multiplication)
The nice fact is that the images of $e_1$ and $e_2$ determines the matrix.
Now your case.
Start with your object facing the point $(1,0)$, so the camera is on a point $(X,0)$ with $X>0$. If the camera move to point $(X,Z)$ you want to rotate the object so taht it faces the camera, which is the same that facing the normalized pont $(frac{X}{sqrt{X^2+Z^2}}, frac{Z}{sqrt{X^2+Z^2}})$ that we denote by $(bar X,bar Z)$.
So you want a matrix $M$ so that $M e_1=(bar X,bar Z)$ and that maps $e_2$ to the normal to $(bar X,bar Z)=(-bar Z,bar X)$. This matrix is simply
$$M=begin{pmatrix}bar X & -bar Z\ bar Z&bar Xend{pmatrix}$$
The matrix $M$, which you see it depends on $X,Z$ will rotate your object as desired.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
As David K already suggested you can use atan2
funtion.
It's a special version of arcus tangens
that takes into account all possible ranges and signs of input values and returns an angle.
Just compute a vector from your rotating object to target (Camera) and put its X and Y into an atan2
function. The result will be a rotation angle, in Radians.
add a comment |
up vote
0
down vote
As David K already suggested you can use atan2
funtion.
It's a special version of arcus tangens
that takes into account all possible ranges and signs of input values and returns an angle.
Just compute a vector from your rotating object to target (Camera) and put its X and Y into an atan2
function. The result will be a rotation angle, in Radians.
add a comment |
up vote
0
down vote
up vote
0
down vote
As David K already suggested you can use atan2
funtion.
It's a special version of arcus tangens
that takes into account all possible ranges and signs of input values and returns an angle.
Just compute a vector from your rotating object to target (Camera) and put its X and Y into an atan2
function. The result will be a rotation angle, in Radians.
As David K already suggested you can use atan2
funtion.
It's a special version of arcus tangens
that takes into account all possible ranges and signs of input values and returns an angle.
Just compute a vector from your rotating object to target (Camera) and put its X and Y into an atan2
function. The result will be a rotation angle, in Radians.
answered Dec 3 at 13:45
kolenda
101
101
add a comment |
add a comment |
up vote
0
down vote
That's basic linear algebra.
We use only X,Z cohordinates so we are working in the Euclidean plane $mathbb R^2$.
Put the origin in the point where the axis of your 2d object is placed. And works in such cohordinates. In practice if $(X_0,Z_0)$ are the cohordinates of the 2d object you use coordinates $(X-X_0,Z-Z_0)$ instead of $(X,Z)$.
Now some Thery:
In $mathbb R^2$ linear transformations that fixes the origin (and a rotation is one of them) are determined by $2times 2$ matrices.
Consider points $e_1=(1,0)$ and $e_2=(0,1)$. Then the matrix $begin{pmatrix} a & b\ c & dend{pmatrix}$ sends $e_1$ to point $(a,c)$ and $e_2$ to point $(b,d)$.
And in general $begin{pmatrix} a & b\ c & dend{pmatrix}begin{pmatrix} x \ y end{pmatrix}=begin{pmatrix} ax + by\ cx+ dyend{pmatrix}$, where $begin{pmatrix} x \ y end{pmatrix}$ is another way to say $(x,y)$.
(see here for more details on matrix multiplication)
The nice fact is that the images of $e_1$ and $e_2$ determines the matrix.
Now your case.
Start with your object facing the point $(1,0)$, so the camera is on a point $(X,0)$ with $X>0$. If the camera move to point $(X,Z)$ you want to rotate the object so taht it faces the camera, which is the same that facing the normalized pont $(frac{X}{sqrt{X^2+Z^2}}, frac{Z}{sqrt{X^2+Z^2}})$ that we denote by $(bar X,bar Z)$.
So you want a matrix $M$ so that $M e_1=(bar X,bar Z)$ and that maps $e_2$ to the normal to $(bar X,bar Z)=(-bar Z,bar X)$. This matrix is simply
$$M=begin{pmatrix}bar X & -bar Z\ bar Z&bar Xend{pmatrix}$$
The matrix $M$, which you see it depends on $X,Z$ will rotate your object as desired.
add a comment |
up vote
0
down vote
That's basic linear algebra.
We use only X,Z cohordinates so we are working in the Euclidean plane $mathbb R^2$.
Put the origin in the point where the axis of your 2d object is placed. And works in such cohordinates. In practice if $(X_0,Z_0)$ are the cohordinates of the 2d object you use coordinates $(X-X_0,Z-Z_0)$ instead of $(X,Z)$.
Now some Thery:
In $mathbb R^2$ linear transformations that fixes the origin (and a rotation is one of them) are determined by $2times 2$ matrices.
Consider points $e_1=(1,0)$ and $e_2=(0,1)$. Then the matrix $begin{pmatrix} a & b\ c & dend{pmatrix}$ sends $e_1$ to point $(a,c)$ and $e_2$ to point $(b,d)$.
And in general $begin{pmatrix} a & b\ c & dend{pmatrix}begin{pmatrix} x \ y end{pmatrix}=begin{pmatrix} ax + by\ cx+ dyend{pmatrix}$, where $begin{pmatrix} x \ y end{pmatrix}$ is another way to say $(x,y)$.
(see here for more details on matrix multiplication)
The nice fact is that the images of $e_1$ and $e_2$ determines the matrix.
Now your case.
Start with your object facing the point $(1,0)$, so the camera is on a point $(X,0)$ with $X>0$. If the camera move to point $(X,Z)$ you want to rotate the object so taht it faces the camera, which is the same that facing the normalized pont $(frac{X}{sqrt{X^2+Z^2}}, frac{Z}{sqrt{X^2+Z^2}})$ that we denote by $(bar X,bar Z)$.
So you want a matrix $M$ so that $M e_1=(bar X,bar Z)$ and that maps $e_2$ to the normal to $(bar X,bar Z)=(-bar Z,bar X)$. This matrix is simply
$$M=begin{pmatrix}bar X & -bar Z\ bar Z&bar Xend{pmatrix}$$
The matrix $M$, which you see it depends on $X,Z$ will rotate your object as desired.
add a comment |
up vote
0
down vote
up vote
0
down vote
That's basic linear algebra.
We use only X,Z cohordinates so we are working in the Euclidean plane $mathbb R^2$.
Put the origin in the point where the axis of your 2d object is placed. And works in such cohordinates. In practice if $(X_0,Z_0)$ are the cohordinates of the 2d object you use coordinates $(X-X_0,Z-Z_0)$ instead of $(X,Z)$.
Now some Thery:
In $mathbb R^2$ linear transformations that fixes the origin (and a rotation is one of them) are determined by $2times 2$ matrices.
Consider points $e_1=(1,0)$ and $e_2=(0,1)$. Then the matrix $begin{pmatrix} a & b\ c & dend{pmatrix}$ sends $e_1$ to point $(a,c)$ and $e_2$ to point $(b,d)$.
And in general $begin{pmatrix} a & b\ c & dend{pmatrix}begin{pmatrix} x \ y end{pmatrix}=begin{pmatrix} ax + by\ cx+ dyend{pmatrix}$, where $begin{pmatrix} x \ y end{pmatrix}$ is another way to say $(x,y)$.
(see here for more details on matrix multiplication)
The nice fact is that the images of $e_1$ and $e_2$ determines the matrix.
Now your case.
Start with your object facing the point $(1,0)$, so the camera is on a point $(X,0)$ with $X>0$. If the camera move to point $(X,Z)$ you want to rotate the object so taht it faces the camera, which is the same that facing the normalized pont $(frac{X}{sqrt{X^2+Z^2}}, frac{Z}{sqrt{X^2+Z^2}})$ that we denote by $(bar X,bar Z)$.
So you want a matrix $M$ so that $M e_1=(bar X,bar Z)$ and that maps $e_2$ to the normal to $(bar X,bar Z)=(-bar Z,bar X)$. This matrix is simply
$$M=begin{pmatrix}bar X & -bar Z\ bar Z&bar Xend{pmatrix}$$
The matrix $M$, which you see it depends on $X,Z$ will rotate your object as desired.
That's basic linear algebra.
We use only X,Z cohordinates so we are working in the Euclidean plane $mathbb R^2$.
Put the origin in the point where the axis of your 2d object is placed. And works in such cohordinates. In practice if $(X_0,Z_0)$ are the cohordinates of the 2d object you use coordinates $(X-X_0,Z-Z_0)$ instead of $(X,Z)$.
Now some Thery:
In $mathbb R^2$ linear transformations that fixes the origin (and a rotation is one of them) are determined by $2times 2$ matrices.
Consider points $e_1=(1,0)$ and $e_2=(0,1)$. Then the matrix $begin{pmatrix} a & b\ c & dend{pmatrix}$ sends $e_1$ to point $(a,c)$ and $e_2$ to point $(b,d)$.
And in general $begin{pmatrix} a & b\ c & dend{pmatrix}begin{pmatrix} x \ y end{pmatrix}=begin{pmatrix} ax + by\ cx+ dyend{pmatrix}$, where $begin{pmatrix} x \ y end{pmatrix}$ is another way to say $(x,y)$.
(see here for more details on matrix multiplication)
The nice fact is that the images of $e_1$ and $e_2$ determines the matrix.
Now your case.
Start with your object facing the point $(1,0)$, so the camera is on a point $(X,0)$ with $X>0$. If the camera move to point $(X,Z)$ you want to rotate the object so taht it faces the camera, which is the same that facing the normalized pont $(frac{X}{sqrt{X^2+Z^2}}, frac{Z}{sqrt{X^2+Z^2}})$ that we denote by $(bar X,bar Z)$.
So you want a matrix $M$ so that $M e_1=(bar X,bar Z)$ and that maps $e_2$ to the normal to $(bar X,bar Z)=(-bar Z,bar X)$. This matrix is simply
$$M=begin{pmatrix}bar X & -bar Z\ bar Z&bar Xend{pmatrix}$$
The matrix $M$, which you see it depends on $X,Z$ will rotate your object as desired.
answered Dec 3 at 13:53
user126154
5,313716
5,313716
add a comment |
add a comment |
Thanks for contributing an answer to Mathematics Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3024027%2fhow-to-make-an-object-face-another-object-in-3d-space%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
It sounds like you want to use the $mathrm{atan2}$ function somehow, perhaps $mathrm{atan2}(X,Z).$ You might have to experiment a bit to find out exactly how it fits in your environment.
– David K
Dec 3 at 13:06
if my object can rotate 360 degrees I assume that it should be in the function somehow, no?
– Brillll
Dec 3 at 13:32
$mathrm{atan2}$ does give you a full $360$ degrees of output, although it usually gives it in radians.
– David K
Dec 3 at 13:35