Reputation: 5045
I am able to get the current position the camera is in, i.e, its x,y,z co-ordinates in aframe.
In the below code, I am making my camera move forward.
function move_camera_forward(){
x=$("#cam").attr("position").x;
y=$("#cam").attr("position").y;
z=$("#cam").attr("position").z;
updated_pos=x+" "+y+" "+String(Number(z)-0.2);
$("#cam").attr("position",updated_pos);
}
But this moves the camera along z axis irrespective the direction the camera is facing. I want to move the camera based on the direction faced by the camera. If the camera is facing lets say 45 degrees, I want to update the three co-ordinates. For this I need to find out in which direction the camera is facing. How can I do this? Does it have something to do with fov?
Upvotes: 2
Views: 1511
Reputation: 5045
I finally figured out how to do this. camera has a rotation attribute which gives me the angle of rotation. With this data and a bit of trigonometry, we can find the updated position. The below code moves the camera in the direction in which the user sees.
new_x = 0;
new_z = 0;
function move_camera_forward() {
x = $("#cam").attr("position").x;
y = $("#cam").attr("position").y;
z = $("#cam").attr("position").z;
radian = -($("#cam").attr("rotation").y) * (Math.PI / 180);
new_z = (new_z + (0.1 * Math.cos(radian)));
new_x = new_x + (0.1 * Math.sin(radian));
new_pos = new_x + " " + y + " " + (-new_z);
console.log(new_pos)
$("#cam").attr("position", new_pos)
}
Upvotes: 3
Reputation: 667
You can dive into the Three.js API to get any additional info that Aframe doesn't necessarily bubble to the surface. So you can get the camera object using
var camera = document.querySelector('[camera]').object3D
and then you have access to all the Vector data for the camera. To get the direction the camera is facing you can use camera.getWorldDirection()
and that returns a Vector3 with X,Y and Z values.
Upvotes: 2