Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit camera positional movements to area of a specific 3D object

I am trying to limit the camera position movements to specific areas defined by a 3D object/objects children. For example, if I had a walkway object on the ocean and I only wanted my user to be able to move the camera forward and backward on that walkway. Similar to a first person controller navigation mesh in Unity but without the AI aspects.

I would enjoy an AFrame based solution, but I do not mind writing a custom component if there is a THREE js solution.

Thank you!

like image 534
cnzac Avatar asked Feb 18 '17 21:02

cnzac


1 Answers

you can create a THREE.Box3 to get the boundaries of the 3D object/objects children by:

 var box = new THREE.Box3();
 box.setFromObject(yourObject);

Inside the first person controller you can check if the camera is out of bounds:

if(camera.position.x > box.max.x){
    camera.position.x = box.max.x;
}

if(camera.position.x < box.min.x){
    camera.position.x = box.max.x;
}

if(camera.position.z > box.max.z){
    camera.position.z = box.max.z;
}

if(camera.position.z < box.min.z){
    camera.position.z = box.max.z;
}

I hope it will be helpful

like image 63
Normal Avatar answered Nov 16 '22 10:11

Normal