0 votes

Hi,

I'm making a simple object viewer. When a user loads in a object I'd like the camera to move/resize itself so the object fills the view.

var body = load(user_model).instance()
add_child(body)

Is there a simple way or technique to place the camera?

Even something basic like moving the camera backwards until the object fits on the screen.

in Engine by (204 points)

1 Answer

0 votes

The following steps assume the loaded object is a visual instance.

Step 1: Retrieve the mesh's AABB. This will be needed later on.
Step 2: Put your current camera at the center of this AABB:

//bound_box is the retrieved AABB.
cam.translation = bound_box.position + bound_box.size/2.0

Step 3: Move the camera along its local z axis by the AABB's size/2.

cam.translation += cam.get_camera_transform().basis.z * bound_box.size.length()/2.0

If all goes well, you should see the entire mesh from any angle.

by (3,892 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.