#Rotate camera around target

10 messages · Page 1 of 1 (latest)

waxen rapids
#

Hello,

I am quite new to 3D development, so I thought a good way to learn was to try and create some simplistic 3D editor integrated with Godot. My camera will point towards a focal point (similar to the regular 3D editor). I haven't integrated the handling of the mouse input yet, but I have added some simplistic code to move the camera each frame. It seems to work as it is, but I'm not sure it is the best approach. What I'm currently doing is:

  1. Rotate the camera along the Y axis of the target
  2. Compute the cross product between the new position of the camera and the target's Y axis
  3. Rotate the camera along the computed axis

I then point the camera towards the target and orthonormalize the transform, just in case. I've searched Google for better ways to do this and they all mention using a parent Spatial (or even two) and rotating that in order for Godot to automatically rotate the camera, but I failed to produce the same results with that method.

For reference the code I'm using so far in the _process function is:

transform.origin = transform.origin.rotated(_target.transform.basis.y, deg2rad(5))

var rot_y = sin(float(Time.get_ticks_msec()) / 1_000)
var axis = transform.origin.cross(_target.transform.basis.y).normalized()
transform.origin = transform.origin.rotated(axis, deg2rad(rot_y))

transform = transform.looking_at(_target.transform.origin, _target.transform.basis.y)
transform.orthonormalized()

Any help would be extremely appreciated!! Thanks 😄

odd steppe
#

That Spatial thing would be my first idea too. Youre in luck though as I need something like this for my own game. Do you want to rotate the cam or the object? I can build you a prototype and explain you whats happening but i want my object to rotate not the cqm

#

This is for rotating the camera if you need one for rotating the object let me know. If you have questions I will try to help you. You basically just rotate the cameras parent which has the same position as the object. The cam gets offset a bit so the whole object is in the view and with an animation you rotate it 360 degree (actually from 0 - 359 since 360 == 0 degree) every 5 seconds in this animation. Reducing the time makes the rotation faster.

#

This does the same but it doesnt rotate the camera but the object. Imo this is better since the background doesnt get rotatet but that is a matter of taste

#

Last thing: This is for Godot 3.5, if you need another version let me know

waxen rapids
#

Thanks @odd steppe! That was just what I needed to get it working 🙂

odd steppe
#

Glad to help

odd steppe
#
extends Node3D


@export_enum("Object", "Camera") var rotation_target: String = "Object"
@export var object: PackedScene

func _ready():
    %"AnimationPlayer".get_animation("Rotate360").track_set_path(
                0, str(rotation_target, "Parent:rotation_degrees"))
                
    for child in %"ObjectParent".get_children(): child.queue_free()
    if not object: return
    %"ObjectParent".add_child(object.instantiate())
    %"AnimationPlayer".play("Rotate360")
#

Heres a little extra, its for Godot 4 but maybe you can turn it into a Godot 3 Script 🙂