Good Morning/Day/Evening fellow developers!
Now what I am trying to do, is to make an sort of Orbit camera, but instead of the Orbit camera used in the Tutorials and examples, I want to use my Phone, as the Orbit camera. I’ve already seen, that the Device Orientation can be read and that this information is sufficent to rotate the camera. But the movement is tricky… The movement is tracked by the accelerometer and therefore acceleration in m/s^2. Now I was wondering if anyone has done or heard of someone achieving this.
What I got so far was this:
window.addEventListener("deviceorientation", function(event) {
// for tracking orientation
txt_orientation.findByName("alpha").element.text = "alpha:" + event.alpha.toFixed(4);
txt_orientation.findByName("beta").element.text = "beta:" + event.beta.toFixed(4);
txt_orientation.findByName("gamma").element.text = "gamma:" + event.gamma.toFixed(4);
//Do some basic rotations
var zRot = Math.floor(event.alpha);
var xRot = Math.floor(event.beta) - 90;
var yRot = Math.floor(event.gamma);
//xRot = origCameraRot.x + xRot;
camera.setEulerAngles(xRot, yRot, origCameraRot.z);
}, true);
To read out the acceleration you can use:
window.addEventListener("devicemotion", function(event) {
// Process event.acceleration, event.accelerationIncludingGravity,
// event.rotationRate and event.interval
txt_Acceleration.findByName("x_Acc").element.text = "X:" + event.acceleration.x.toFixed(4);
txt_Acceleration.findByName("y_Acc").element.text = "Y:" + event.acceleration.y.toFixed(4);
txt_Acceleration.findByName("z_Acc").element.text = "Z:" + event.acceleration.z.toFixed(4);
txt_Acceleration.findByName("alpha").element.text = "alpha:" + event.rotationRate.alpha.toFixed(4);
txt_Acceleration.findByName("beta").element.text = "beta:" + event.rotationRate.beta.toFixed(4);
txt_Acceleration.findByName("gamma").element.text = "gamma" + event.rotationRate.gamma.toFixed(4);
}, true);
Does anyone have an idea how to move the camera according to the acceleration as to achieve a sort of orbit camera with phone movement ? (I am aware that the rotation routine is not good or even really bad)
Kind regards.