Rotate upwards facing side of cube towards camera

Godot Version

v4.3.stable.arch_linux

Question

I have a cube that I apply a central and a torque impulse to, so it rolls like a dice.
The camera onto that dice is looking at a 30° angle.
To get a better look of the result, I want to tween the rotation of the upwards facing face towards the camera.

But I can not figure out how to do that.
I’m so bad at this kind of math right now that it hurts me.

What would be the best approach?
Manipulating the rotation/rotation_degrees, or use the quaternion property, or the transform.basis, I don’t know.

I tried multiplying and adding stuff around, but I have no idea what I’m doing lol.

I’ll take any form of solution/advice. Pointing me towards the math keywords I need to study up on, would be helpful as well.

Thanks!

Could you move the camera above the dice’s global position and point it straight down?

1 Like

Yes, I could absolutely do that!
Didn’t think of that, thanks!

I have images on each side of the cube and ideally I’d like all of the images to be oriented “correctly”. So the top side of the sprite should be facing the direction that is “up” now with this new camera position.

I think i still need to figure out which side is facing up to rotate the cubes along the correct axis. I think I could do that with a look-up-table (that was always an option I guess), but I’d rather try the more general math approach if anyone could point me to the relevant topics :slight_smile:


Top right cube should be rotated 180° and top right cube 90° around whatever axis I need to rotate around now

1 Like

Okay, no complex math needed. Seems like I was pretty hellbent on doing it the mathy way that I don’t understand quite yet.

I assigned a “up” metadata to every cube face since they are just Sprite3D’s. I rotated the cube in the editor and noted down the rotation angle of each side, the way I want the cube to look like when facing the camera.

Then I made a get_top function on the dice:

func get_top() -> Node3D:
	var top := -pow(2, 8)
	var face: Node3D
	for node: Node3D in $Faces.get_children():
		if top < node.global_transform.origin.y:
			top = node.global_transform.origin.y
			face = node
	
	return face

and just tried out how to rotate the cube towards the camera manually in the remote scene tree to figure out the axis and angles and came up with this:

var face: Node3D = dice.get_top()
var up_rotation: Vector3 = face.get_meta("up")
var transform_vector: Vector3
if up_rotation.y != 0 and up_rotation.z != 0:
	transform_vector = Vector3(0, 0, 60) * sign(up_rotation.y)
else:
	transform_vector = Vector3(60, 0, 0) * (-1 if up_rotation.y > 0 else 1)
tween.tween_property(dice, "rotation_degrees", face.get_meta("up") + transform_vector, 0.5)

Looks like it works that way :slight_smile:

1 Like