Rotate Vector around (0,0,0) so the another vector looks it's rotated in specific way

ggenije

New member
Joined
Mar 31, 2020
Messages
2
Hello!
I am creating custom 3D engine from scratch and I am having trouble with rotating.
To draw particular 3D model you need to store every vertex data in some kind of list,
then because computation rotation of every vertex individually is ineffective I can simply rotate camera in position so that model looks it's rotated then I just have to save calculated screen positions , and repeat that action for every object in scene.
Every model which is imported is located in origin(0,0,0)
and the information I have is cameraX , cameraY, cameraZ, cameraRotationX(angle), cameraRotationY,cameraRotationZ
then objectX , objectY, objectZ, objectRotationX, objectRotationY and objectRotationZ (when model it's imported we consider it's pointing at +Z axis)
And this is how I am calculating screen position of some vertex accordion to camera position and rotation:
rotate.png
Set Ver1(a)(b)(c) is same as ~GetX1=a; ~GetY1=b; ~GetZ1 = c;
Then to convert it to screen I use screenX=~GetX1/~GetZ1*FieldOfView and screenY=~GetY1/~GetY1*FieldOfView (FieldOfView is custom number and it's constant)

So to rotate object I can't simply rotate every of it's numerous vertices but I need to rotate and move camera in that way so the object looks rotated.
For real world example imagine a car which is pointing at you and you are watching at that car and you want to see side of that car, logically you won't rotate whole car it will be physically demanding , but you will change your position to be at the left of the car and rotate you by 90 degrees (at y axis).
Now in this case everything is same expect you can fly and you want to see it from below, from side ,from top ,half bottom -half side etc.
(from angle of (objectRotX,objectRotY,objectRotZ))

I tried to use same Vertex-> Screen funtion(as upper function) but to rotate camera around (0,0,0) and this is not working because that rotation is not rotated(and I don't know what I am doing basically :confused: )
for example if you want to watch the top of object and you are currently watching side you will rotate wrong way.

This image may help you to help me
(xs,ys,zs) is camera position and it's pointing to somewhere(cameraRotX, cameraRotY,cameraRotZ ) and Vt is always pointing at same (0,0,1)
So I need to calculate (x', y' z') and (cameraRotX' , cameraRotY', cameraRotZ') so the Vector Vt looks it's rotated from view camera for(objectRotX,objectRotY,objectRotZ)
(and (x' y' z') have the same distance from origin as (x,y,z)
rotate3.png
 
Welcome to Free Math Help!

The simple case of rotating the position of the camera around an XZ plane is a two-dimensional rotation, which can be achieved with the following formula for some angle [MATH]\theta[/MATH]:

[MATH]x' = x * cos(\theta) - z * sin(\theta)[/MATH][MATH]z' = x * sin(\theta) + z * cos(\theta)[/MATH]​

This also works with XY and YZ pairs. This will move a point from its current position about the origin by a given angle. For your camera, you'll then want to calculate the angle from the resulting position to the origin in order to determine the camera's facing direction.

So to rotate object I can't simply rotate every of it's numerous vertices but I need to rotate and move camera in that way so the object looks rotated.
Ultimately, it is in fact the vertices that move. The concept of "camera" is a bit of a misconception: the viewing position is always fixed at the origin, and the scene is transformed around it. In conventional 3D computing, vertices are typically expressed in homogeneous coordinates, which are then manipulated through transformation matrices and ultimately a projection matrix. The target is a coordinate space that is a cube 2 units to an edge centered on the origin, which is used to determine the output screen coordinates and compare depth between fragments.

I recommend you research this way of doing 3D graphics, as it is the method used by most (all?) modern graphics processors and the knowledge is useful across a large number of applications. By abandoning the concept of a camera, you will also be better equipped to visualize how objects in the scene eventually need to be transformed.
 
So naive solution will be rotate object with it's rotation then rotate object around camera rotation.
But this way I need to calculate position of one vertex by rotating it , then I need to rotate it again by camera rotation.
So I need to apply rotations per vertex twice , first for object and then for camera.

In other way(by moving and rotating camera in correct way) I need to vertex rotation only once per vertex(for camera), as camera position and rotation is calculated I don't need to apply any transformations but to rotate object around camera.

The first way says rotate object around self and then rotate it around camera.
And the second way says rotate camera so object looks it's rotated in it's rotation, then rotate every vertex around camera.

So I managed to rotate scene about camera position and camera rotation with simple formulas as listed before (XZ rotation XY rotation and YZ rotation)
But how to apply that when both camera and object have rotations.
I must calculate position and rotation of camera so the target object looks it's rotated.
That is my problem.

In next image:
rotate4.png

You can see example with only XZ rotation(only pitch);
camera is pointing at object(in this example) to I need to calculate camera that way the object looks rotated(only in pitch)
and camera stays pointing at the object.
If camera rotation was not pointing at object for example pointing so the object is in corner of screen, when rotation is applied object will look rotated and in same place.
 
Top