Rotating cube with camera

492 Views Asked by At

I have a cube positioned at the origin $(0,0,0)$, and a point in 3D space (it's considered a camera ) and I know its position and where it's looking.

Now as the camera moves left and right I want the box to rotate with it, i.e I want the front face to stay the same

I'm a little familiar with homogeneous transformation matrix and the stuff related to it but I'm not sure how to apply the transformation or whether this is the right approach. Thanks.

the first image is my initial state (the camera is looking at the box which is at the origin) initial_state the second image is after I dragged my mouse around a little (the scene is not moving of course ...just the camera)some_other_state

2

There are 2 best solutions below

0
On

Usually, when dealing with 3D rendering, one has

  • a model matrix (for positioning and orienting an object in the scene),
  • a view matrix (for positioning and orienting the camera),
  • a projection matrix (for modifying other camera stuff, e.g. "field of view" etc.).

As the cameras position should not influence the rendering of the cube, can't you just not apply the view matrix to the cubes vertices? Alternatively, if you cannot prevent this, apply the inverse view matrix to the cube's vertices. Then the view matrices cancel out and the cube is rendered independent of the cameras position.

If you can zoom into your scene and the cube should stay of the same size (w.r.t the scene), then you have to remove the scaling factor from the view matrix before applying its inverse, e.g. by scaling it to have determinant equal one.

0
On

I assume that the height of the camera is constant. If the height does change then you will also have a time-dependent radius $r$.

The situation is as described in the picture at the end of my answer (sorry for the bad drawing, but I do not have a clean paper at the moment :D).

If you know the $x(t)$ and $y(t)$ coordinates of your camera you can use polar coordinated to determine the angle $\varphi(t)$. It is given by

$$\varphi(t) = \arctan \left[\dfrac{y(t)}{x(t)} \right]$$

if your software has the atan2 function you can use this instead of the $\arctan$ function to prevent the problems with $x(t)=0$.

In order to check if your camera does not change the height, you could calculate $x(t)^2+y(t)^2$ if this is always equal to the squared constant radius $r^2$ then your camera does not change its height. Or you could simply check if the $z$-coordinate (height) stays constant.

Then you simply use $\varphi(t)$ as the time-dependent rotation angle of your cube (about the $z$-axis) and it will always rotate in such a way that its front face is looking in the direction of the camera.

If you want the normal of the front face to look at the camera you will have to calculate the additional angle $\theta(t)$ between the $xy$-plane (street) and the camera it is simply done by

$$\theta(t) = \arctan\left[\dfrac{z(t)}{x^2(t)+y^2(t)} \right]$$

the angle will be constant if the height $z(t)$ is constant. You will need to rotate the cube with this angle such that the normal vector of the front face will point towards the camera.

enter image description here