Rotate 3D object to face target

Hi, I had a 2D sketch where an object went around the screen trying to find a target. It always rotated to face its target with the following code:

  float dX = location.x - target.x;
  float dY = location.y - target.y;

  float angle = radians((atan2(dY, dX) * 180 / PI));

However I am now working in 3D and atan2 doesn't allow me to add an additional value for location.z - target.z. I'm a bit stuck on trying to get this way of rotating the object to face its target to work in 3D. I've tried a few things but haven't had any luck yet so if anyone had any suggestions or could tell me where to look that would be much appreciated. Thanks.

Answers

  • atan() (arc tangent) takes only two arguments for a reason - how would you go about defining an angle of a vector in 3D? In 2D it is the angle between +ve X axis and the vector defined by the x and y values. Try thinking about it.

    So, you want to calculate how to point towards some object in 3D? Then you firstly require 2 angles of rotation for 3D. Then you decide what those angles are. Finally, try implementing it.

  • this is a 2D example that works only with vectors

    Since those can be 2D and 3D, it might be a cool starting point for you

    https://forum.processing.org/two/discussion/comment/81133/#Comment_81133

  • So, you want to calculate how to point towards some object in 3D? Then you firstly require 2 angles of rotation for 3D. Then you decide what those angles are. Finally, try implementing it.

    Hey, do you know how to actually do this? I've been searching like crazy for an actual implementation of this specific problem, but can't find any starting point. In Processing each rotation you do affects the following rotations, which makes it pretty tricky!

  • Yep, thanks, that's my thread haha! Did find a solution there though so people of the future looking for a solution, recommend clicking that link ^^^

  • @JLJac: Yeah, thanks a lot for sharing!

Sign In or Register to comment.