#Relative 3D rotation

1 messages · Page 1 of 1 (latest)

placid relic
#

Rotation is relative to the parent by default. You must use "global_rotation" to make it non-relative.

frank jasper
solemn gust
#

Do you mean you want to rotate the object as though its up and right axes perfectly point up and right on the screen?

solemn gust
#

IIRC there are ways to rotate a vector by passing in an arbitrary up and right vectors, so presumably passing in the camera's up and right vectors would achieve that. I think it involves quaternions... I'll see it I can quickly find it

solemn gust
#

I just woke up so I could well be wrong about this 😂 But I think if you use the vector representing the camera's rotation and the vector representing the object's rotation as as parameters for Quaternion(arc_from, arc_to), you could then multiply your camera-relative rotation by that resulting quaternion to get the equivalent rotation in object-space

#

Oh, or if that doesn't work, you could maybe figure out the axis of the rotation you want in camera-space, transform that into object-space using Quaternion(arc_from, arc_to), and then use Quaternion(axis, angle) to get the rotation

#

This is where I'd start tinkering if I were trying to do this myself anyway

frank jasper
frank jasper
solemn gust
#

You can multiply Vector3 by Quaternion. I think in Godot the vector needs to be on the right (according to the doc), so for example Rotation = Quaternion * Rotation should rotate the vector by the quaternion

#

I haven't worked with Quaternion in Godot though, only in other frameworks, so there may be quirks about implementation

frank jasper
#

👍

frank jasper
#

Or I'm just too dum to understand them

#

Basically like skewering the object

solemn gust
#

Darn; hopefully someone else has a better idea. Without digging into it myself I don't think I can offer further advice; this is the kind of thing I'd spend a while googling and testing out

frank jasper
#

UPDATE: I figured it out.

var prev_mouse_pos: Vector2
func _process(delta):
   var new_mouse_pos = get_viewport().get_mouse_position()
   var pos = new_mouse_pos - prev_mouse_pos
   var angle = pos * 0.01
   currentGrab.global_rotate(global_transform.basis.x,angle.y)
   currentGrab.global_rotate(global_transform.basis.y,angle.x)
   prev_mouse_pos = new_mouse_pos