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.










share|cite|improve this question






















  • 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















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.










share|cite|improve this question






















  • 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













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.










share|cite|improve this question













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






share|cite|improve this question













share|cite|improve this question











share|cite|improve this question




share|cite|improve this question










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


















  • 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










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.






share|cite|improve this answer




























    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.






    share|cite|improve this answer





















      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',
      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
      });


      }
      });














      draft saved

      draft discarded


















      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








      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.






      share|cite|improve this answer

























        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.






        share|cite|improve this answer























          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.






          share|cite|improve this answer












          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.







          share|cite|improve this answer












          share|cite|improve this answer



          share|cite|improve this answer










          answered Dec 3 at 13:45









          kolenda

          101




          101






















              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.






              share|cite|improve this answer

























                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.






                share|cite|improve this answer























                  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.






                  share|cite|improve this answer












                  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.







                  share|cite|improve this answer












                  share|cite|improve this answer



                  share|cite|improve this answer










                  answered Dec 3 at 13:53









                  user126154

                  5,313716




                  5,313716






























                      draft saved

                      draft discarded




















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      Bressuire

                      Cabo Verde

                      Gyllenstierna