The next time you play a 3D game you may look at it differently. Realizing that the world and all its objects spins around you every time you turn around, even though your head may tell you it's standing still. This should give better understanding for a good graphics card being important for gaming/3D work as well, since every time you move in a 3D world, it has to turn billions, maybe trillions of vectors to your wanted viewpoint, as well as showing e.g. particle explosions, lighting algorithms and more at the same time. GPUs are specialized for those kind of calculations, much more than CPUs.
Understanding the formula below and the example code (view source) should be able to inspire multiple creative uses as well as give a basic understanding of how 3D rotation and dynamic worlds works. Game development today largely happens in already-made 3D engines where all of this and SO much more is already covered, but it's still fun to learn and understand some of the lowest basics behind the curtains.
In the 2D script I used this formula to rotate around a center point:
This formula gives the coordinate/vector 'x' and/or 'y' new values based on finding the radian x and y ratio through cosine and sine multiplied by hypotenus to keep the proper center point (radius). This formula could be used for simulating 3D on a 2D plane. It could even be used for real 3D calculation, but you would need to find a way to make the rotation radius/hypotenus values work together when e.g crossing a z-axis with a y-axis rotation. When e.g. the z-rotation closes in on the y-axis, the radius of z-rotation (x) would have to be shortened accordingly, start a x-rotation as well and you may have a challenge.
Here is a formula that does the proper job of 3D calculation. It respects every axis radius/hypotenus in regards to each other.
By changing the RadianX, RadianY, RadianZ up to 2π(6.28)(360°) in this formula you control every axis rotation, and each rotation radius/hypotenus should work in harmony.
It will help to repeat the chapter in a math book on trigonometry (especially sine and cosine) to get a full grasp on the formula above. I had to myself, and will likely need to again when/if using this formula later.
Here I go deeper into trig and use dot product differences between my forward- and the enemy vector using normalizing and magnitudes.
Open in a separate tab or window for easier source view.