• 3D
  • How to roll a 3D cube?

I'm new to Godot, and I would like to know how to roll a cube. Example in the video below:

(For some reason this question was flagged, I’m not sure why. Regardless, it should be fixed now :) )

While not a perfect example, here is the code that will rotate the cube by 90 degrees when the keyboard arrows are pressed:

extends Spatial

const GRID_SIZE = 1;
const MOVE_SPEED = 1;

var tween_node;
var tween_moving = false;

func _ready():
	tween_node = get_node("Tween");
	tween_node.connect("tween_completed", self, "_on_tween_completed");
	tween_moving = false;

func _process(_delta):
	if (tween_moving == false):
		if (Input.is_action_pressed("ui_up")):
			tween_node.interpolate_property(self, "translation",
				translation, translation + (Vector3.FORWARD * GRID_SIZE),
				MOVE_SPEED, Tween.TRANS_LINEAR, Tween.EASE_IN_OUT);
			tween_node.interpolate_property(self, "rotation",
				rotation, rotation + Vector3(deg2rad(-90), 0, 0),
				MOVE_SPEED, Tween.TRANS_LINEAR, Tween.EASE_IN_OUT);
			tween_node.start();
			tween_moving = true;
			
		elif (Input.is_action_pressed("ui_down")):
			tween_node.interpolate_property(self, "translation",
				translation, translation + (Vector3.BACK * GRID_SIZE),
				MOVE_SPEED, Tween.TRANS_LINEAR, Tween.EASE_IN_OUT);
			tween_node.interpolate_property(self, "rotation",
				rotation, rotation + Vector3(deg2rad(90), 0, 0),
				MOVE_SPEED, Tween.TRANS_LINEAR, Tween.EASE_IN_OUT);
			tween_node.start();
			tween_moving = true;
			
		elif (Input.is_action_pressed("ui_right")):
			tween_node.interpolate_property(self, "translation",
				translation, translation + (Vector3.RIGHT * GRID_SIZE),
				MOVE_SPEED, Tween.TRANS_LINEAR, Tween.EASE_IN_OUT);
			tween_node.interpolate_property(self, "rotation",
				rotation, rotation + Vector3(0, 0, deg2rad(-90)),
				MOVE_SPEED, Tween.TRANS_LINEAR, Tween.EASE_IN_OUT);
			
		elif (Input.is_action_pressed("ui_left")):
			tween_node.interpolate_property(self, "translation",
				translation, translation + (Vector3.LEFT * GRID_SIZE),
				MOVE_SPEED, Tween.TRANS_LINEAR, Tween.EASE_IN_OUT);
			tween_node.interpolate_property(self, "rotation",
				rotation, rotation + Vector3(0, 0, deg2rad(90)),
				MOVE_SPEED, Tween.TRANS_LINEAR, Tween.EASE_IN_OUT);
			tween_node.start();
			tween_moving = true;


func _on_tween_completed(object, property_nodepath):
	if (property_nodepath == ":translation" and object == self):
		tween_moving = false;

The code above doesn't work properly when the rotation is non uniform. When the rotation is uniform, it works as expected though. Ideally you'd want to use interpolate_method instead of interpolate_property and interpolate the rotate_x/rotate_y/rotate_z functions instead of changing the rotation property. I wasn't able to get the interpolate_method function working with any of the rotate functions though, but I didn't send too much time playing with it.

Here is a link to the Godot project on my Google Drive. Hopefully this give you an idea of how to do it. :smile: (Side note: Welcome to the forums!)

17 days later

I sent you an answer here, but I dont know what happened to it. I send it again :) I am away from any projects right now (a lot of works to do in my job) but I will test it as soon as possible! Thank you so much!

It looks like your previous post got stuck in the moderation queue. I'm not sure why, especially since the second post didn't get stuck. Regardless, I hope the project is helpful :smile: