How to make an object face another object in 3D space?
$begingroup$
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
$endgroup$
add a comment |
$begingroup$
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
$endgroup$
$begingroup$
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.
$endgroup$
– David K
Dec 3 '18 at 13:06
$begingroup$
if my object can rotate 360 degrees I assume that it should be in the function somehow, no?
$endgroup$
– Brillll
Dec 3 '18 at 13:32
$begingroup$
$mathrm{atan2}$ does give you a full $360$ degrees of output, although it usually gives it in radians.
$endgroup$
– David K
Dec 3 '18 at 13:35
add a comment |
$begingroup$
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
$endgroup$
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 '18 at 12:56
BrillllBrillll
61
61
$begingroup$
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.
$endgroup$
– David K
Dec 3 '18 at 13:06
$begingroup$
if my object can rotate 360 degrees I assume that it should be in the function somehow, no?
$endgroup$
– Brillll
Dec 3 '18 at 13:32
$begingroup$
$mathrm{atan2}$ does give you a full $360$ degrees of output, although it usually gives it in radians.
$endgroup$
– David K
Dec 3 '18 at 13:35
add a comment |
$begingroup$
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.
$endgroup$
– David K
Dec 3 '18 at 13:06
$begingroup$
if my object can rotate 360 degrees I assume that it should be in the function somehow, no?
$endgroup$
– Brillll
Dec 3 '18 at 13:32
$begingroup$
$mathrm{atan2}$ does give you a full $360$ degrees of output, although it usually gives it in radians.
$endgroup$
– David K
Dec 3 '18 at 13:35
$begingroup$
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.
$endgroup$
– David K
Dec 3 '18 at 13:06
$begingroup$
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.
$endgroup$
– David K
Dec 3 '18 at 13:06
$begingroup$
if my object can rotate 360 degrees I assume that it should be in the function somehow, no?
$endgroup$
– Brillll
Dec 3 '18 at 13:32
$begingroup$
if my object can rotate 360 degrees I assume that it should be in the function somehow, no?
$endgroup$
– Brillll
Dec 3 '18 at 13:32
$begingroup$
$mathrm{atan2}$ does give you a full $360$ degrees of output, although it usually gives it in radians.
$endgroup$
– David K
Dec 3 '18 at 13:35
$begingroup$
$mathrm{atan2}$ does give you a full $360$ degrees of output, although it usually gives it in radians.
$endgroup$
– David K
Dec 3 '18 at 13:35
add a comment |
2 Answers
2
active
oldest
votes
$begingroup$
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.
$endgroup$
add a comment |
$begingroup$
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.
$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "69"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
noCode: true, onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
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.
$endgroup$
add a comment |
$begingroup$
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.
$endgroup$
add a comment |
$begingroup$
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.
$endgroup$
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 '18 at 13:45
kolendakolenda
101
101
add a comment |
add a comment |
$begingroup$
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.
$endgroup$
add a comment |
$begingroup$
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.
$endgroup$
add a comment |
$begingroup$
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.
$endgroup$
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 '18 at 13:53
user126154user126154
5,378716
5,378716
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.
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
$begingroup$
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.
$endgroup$
– David K
Dec 3 '18 at 13:06
$begingroup$
if my object can rotate 360 degrees I assume that it should be in the function somehow, no?
$endgroup$
– Brillll
Dec 3 '18 at 13:32
$begingroup$
$mathrm{atan2}$ does give you a full $360$ degrees of output, although it usually gives it in radians.
$endgroup$
– David K
Dec 3 '18 at 13:35