Hi,
I am having trouble reproducing an equation to have an object turn/face/turn toward and follow another object.
Can anyone please help me with this?
Hi,
I am having trouble reproducing an equation to have an object turn/face/turn toward and follow another object.
Can anyone please help me with this?
https://www.raywenderlich.com/2736-trigonometry-for-game-programming-part-1-2
@piercingarrow my video game is in java, could you give me an example in java?
Learn the math, then write it in any language you want.
Is this 2D (quite easy) or 3D (kind of tricky)?
I'll start with the 2D case, which is somewhat parallel to the 3D case and much easier to understand. The attitude of the object you are trying to turn can be represented by a unit-length complex number. If you are used to thinking in angles, it's the complex number z = cos(alpha) + i*sin(alpha). You are trying to make some part of the object point to a target location. If the part of the object is the complex number x in model space, once you apply the rotation to place it in the world, it'll be z*x. Now you want that to line up with your target t (also a complex number, and let's normalize it too). In order to get there, you need to multiply z by (t / (z*x)). You can do that in one bout, or you can look at the sign of the imaginary part to determine the direction in which you should rotate.
Now, for the 3D case, the attitude will be a unit-quaternion q, and the part of the object that was x in model space will be q*x*conj(q) after applying the rotation. You want that to line up with a target direction t. You can then compute the shortest-arc rotation between those two vectors (https://stackoverflow.com/questions/1171849/finding-quaternion-representing-the-rotation-from-one-vector-to-another). Then compose that rotation into q, or take the direction it points into (its logarithm) and apply an angular velocity to get to the target.
I realize this description is kind of terse. If you have a hard time with it, try to really understand it and ask further questions here. We can work with a concrete example if you want, for example.
@alvaro I think it's only a little bit more complicated in 3d. For a security camera, I would calculate a rotation about the camera's Up axis, then another about its Right axis.
@GManPro101
The JMonkey Engine is a 3D based game engine that supports shaders.