# Getting x rotation with atan2

Hi! I trying to get dif between rotations in euler angles.

I use formula from [SOLVED] Getting Y rotation as 0 - 360 degrees (or -180 to 180)

with atan2, but when i trying to use it with x axis, its doing something wrong…sometimes its going to other side

``````update()
{
this.camera.getLocalRotation().transformVector(pc.Vec3.FORWARD, transformedForward);

var x = Math.atan2(-transformedForward.y,-transformedForward.z) * pc.math.RAD_TO_DEG - this.prevPosX;

}
``````

The math looks right. Can you share your project to look at please?

Sorry, it’s a commercial project and I cant show it.
I think problem is that I use it for 2 axis.

``````MoveGameFrame.prototype.update = function(dt){
var transformedForward = new pc.Vec3();
this.camera.getLocalRotation().transformVector(pc.Vec3.FORWARD, transformedForward);

var x = (Math.atan2(-transformedForward.y,-transformedForward.z) * pc.math.RAD_TO_DEG) - (this.prevPosX);
var y = (Math.atan2(-transformedForward.x, -transformedForward.z) * pc.math.RAD_TO_DEG + 180 ) - (this.prevPosY+180);
if(Math.abs(x)>180)
{
x = 0;
}
if(Math.abs(y)>180)
{
y = 0;
}
var z = 0;
this.text.element.text = Math.floor(x);
this.app.fire('devMot',new pc.Vec3(x,y,z),dt);
this.prevPos = new pc.Vec3(this.camera.getLocalEulerAngles().x,this.camera.getLocalEulerAngles().y,this.camera.getLocalEulerAngles().z);
transformedForward = new pc.Vec3();
this.camera.getLocalRotation().transformVector(pc.Vec3.FORWARD, transformedForward);

this.prevPosY = Math.atan2(-transformedForward.x, -transformedForward.z) * pc.math.RAD_TO_DEG;
};
MoveGameFrame.prototype.deviceMotion = function(e,dt)
{

// e.acceleration
// e.accelerationIncludingGravity
// e.rotationRate (Returns the rate at which the device is rotating around each of its axes in degrees per second)
// e.interval(Returns the interval, in milliseconds, at which data is obtained from the underlaying hardware)
this.rotX = Math.sign(e.y) *Math.max(Math.abs(e.y - 0.15),0);
this.rotY = Math.sign(e.x) *Math.max(Math.abs(e.x - 0.1),0);
window.context.entity.setLocalPosition(window.context.clamp(window.context.entity.getLocalPosition().x + this.rotX/30,-2.2,2.7),
window.context.clamp(window.context.entity.getLocalPosition().y -  this.rotY/50,-0.5,0.5),
-1);
var screenCenterX = (window.context.app.graphicsDevice.width / 2)/window.devicePixelRatio;
var screenCenterY = (window.context.app.graphicsDevice.height / 2)/window.devicePixelRatio;
var from = window.context.camera.camera.screenToWorld(screenCenterX, screenCenterY, window.context.camera.camera.nearClip);
var to = window.context.camera.camera.screenToWorld(screenCenterX, screenCenterY, window.context.camera.camera.farClip);
this.result = window.context.app.systems.rigidbody.raycastFirst(from, to);
//window.context.text.element.text = this.result;
if(this.result!==null)
{
window.context.app.fire('target:found',this.result.entity);
}
};
``````

I use it instead of device motion, because it’s a new bug on ios 13.4.1, that always returns false after devicemotion.requestpermission, but 8thwall give me position and rotation of camera.

Maybe somehow I can use quaternions? I am not really good in math…

When y rotation at ~180 degrees, x axis rotation from 180 to 0, when y rotation at ~0 degrees, x axis rotate from 0 to 180 =[

Can you demo the problem in a new project?

What are you trying to achieve with this? Is the camera attached to the ‘phone’ and therefore trying to get the angle that camera is facing?

I was wrong, the math isn’t quite right. That will work for the the Yaw rotation until the player looks straight up (which is easy enough to avoid) but I forgot that getting the Pitch is affected by the rotation of the Yaw.

The best way I can come up with to work out the Pitch is to calculate the angle between the forward vector and the XZ plane.

1 Like

Sorry for late answer, I use full rotation of camera right now and all is ok, we changed conception of game) Thanks for reply