# Rotating object according to camera rotation

Discussion in 'AR/VR (XR) Discussion' started by nikhoney27, Jul 17, 2017.

1. ### nikhoney27

Joined:
May 31, 2017
Posts:
1
I am using rajawali to render a 360 image , where i am putting one object in front of camera which is 2d object that should always remain at the center of screen. Therefore the object needs to be rotated with respect to camera rotation.

It works fine when camera rotates around x axis and y axis but it does not work properly when rotated around z axis.

So any one change help me out how i can achieve this.

So currently i am using below code to put object in center and rotation of plane.

public void centerObject(Plane obj) {
Vector3 position;
float[] newPosition4 = new float[4];
position = obj.getPosition();
float[] posVec4 = {0, 0, -16, 1.0f};
float[] HeadViewMatrix_inv = new float[16];
Matrix4 HeadViewMatrix4 = new Matrix4();

//Copy matrix to HeadViewMatrix array,

//Translation of viewMatrix
Matrix.multiplyMV(newPosition4, 0, HeadViewMatrix_inv, 0, posVec4, 0);

obj.setPosition(newPosition4[0], newPosition4[1],
newPosition4[2]);

double localCameraRx = ((getCurrentCamera().getRotX() * 57.2958) + 180) / 57.2958;
double localCameraRy = ((getCurrentCamera().getRotY() * 57.2958) + 180) / 57.2958;
double localCameraRz = ((getCurrentCamera().getRotZ() * 57.2958) + 180) / 57.2958;

if (cameraRX != localCameraRx) {
obj.setRotation(Vector3.X, (cameraRX - localCameraRx));
cameraRX = localCameraRx;
}
if (cameraRY != localCameraRy) {
obj.setRotation(Vector3.Y, (cameraRY - localCameraRy));
cameraRY = localCameraRy;
}
if (cameraRZ != localCameraRz) {
obj.setRotation(Vector3.Z, (cameraRZ - localCameraRz));
cameraRZ = localCameraRz;
}

}

2. ### Selzier

Joined:
Sep 23, 2014
Posts:
567
Can't you just make the object a child of the camera?

SiliconDroid likes this.
3. ### SiliconDroid

Joined:
Feb 20, 2017
Posts:
264
Hi,

Simplest way is to just make the object a child of the camera and then add some localspace Z to set its depth:

Code (CSharp):
1.         ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2.         //    BIND OBJ TO CAM WITH Z PUSH
3.         bool BindObjToCam(GameObject oObj, float fPushZ)
4.         {
5.             //  PREPARE
6.             if(!oObj)
7.             {
8.                 return false;
9.             }
10.             Transform tObj = oObj.transform;
11.
12.             Camera oCam = Camera.main;
13.             if (!oCam)
14.             {
15.                 return false;
16.             }
17.
18.             Transform tCam = oCam.transform;
19.             if (!tCam)
20.             {
21.                 return false;
22.             }
23.
24.             //  BIND
25.             tObj.parent = tCam;
26.             tObj.SetPositionAndRotation(tCam.position, tCam.rotation);
27.             tObj.Translate(0, 0, fPushZ, Space.Self);
28.             return true;
29.         }
30.
EDIT: as @Selzier says!

Selzier likes this.