why in this code the rotation axis works diferent? like the axis rotate with the cube as well
extends Spatial

var target_position = Vector3()
var varAlph = 0.0

func _process(delta):
rot()
movement1(delta)

func rot():

$MeshInstance.rotation_degrees = lerp($MeshInstance.rotation_degrees, target_position, varAlph)

if varAlph <= 1:
	varAlph += .01

if Input.is_action_just_pressed("ui_left"): 
	target_position.z = $MeshInstance.rotation_degrees.z + 90
	varAlph = 0.0


if Input.is_action_just_pressed("ui_right"):
	target_position.z = $MeshInstance.rotation_degrees.z - 90
	varAlph = 0.0
	
	
if Input.is_action_just_pressed("ui_up"):
	target_position.x = $MeshInstance.rotation_degrees.x - 90
	varAlph = 0.0	
	
	
if Input.is_action_just_pressed("ui_down"):
	target_position.x = $MeshInstance.rotation_degrees.x + 90
	varAlph = 0.0

but in this one, the axis rotate in other way, the way that i want like the game bloxorz
extends RigidBody
var target_position = Vector3()
var varAlph = 0.0
var basis = Basis()

func _ready():
print($MeshInstance.rotation_degrees)

func _physics_process(delta):

$MeshInstance.rotation = lerp($MeshInstance.rotation, target_position, varAlph)

if varAlph <= 1:
	varAlph += .01

if Input.is_action_just_pressed("ui_left"): 
	$MeshInstance.rotate(-basis.z, deg2rad(90))
	print($MeshInstance.rotation_degrees)
	varAlph = 0.0
		
	
if Input.is_action_just_pressed("ui_right"):
	$MeshInstance.rotate(basis.z, deg2rad(90))
	print($MeshInstance.rotation_degrees)
	varAlph = 0.0

if Input.is_action_just_pressed("ui_up"):
	$MeshInstance.rotate(basis.x, deg2rad(90))
	print($MeshInstance.rotation_degrees)
	varAlph = 0.0
		
if Input.is_action_just_pressed("ui_down"):
	$MeshInstance.rotate(-basis.x, deg2rad(90))
	print($MeshInstance.rotation_degrees)
	varAlph = 0.0
  • xyz replied to this.

    elmrganzo You'd probably want to rotate around a global (or parent) axis transformed into local coordinate space.

    elmrganzo Here's the shortest example I could think of, assuming Godot 3.x.

    extends Spatial
    
    var basis_wanted: Basis
    
    func _process(delta):
    	transform.basis = transform.basis.slerp(basis_wanted, .2)
    
    func _input(event):
    	var input = Input.get_vector("ui_right", "ui_left", "ui_up", "ui_down")
    	var axis = Vector3(input.y, 0.0, 0.0) if input.y else Vector3(0.0, 0.0, input.x)
    	if axis:
    		transform.basis = basis_wanted
    		basis_wanted = transform.basis * Basis(transform.basis.inverse() * axis.normalized(), PI/2)

      xyz omg you are amazing, i been trying to do this for a week and you did it in a few minutes, where did you learn how to do it? can you give me an advice on how to improve in godot?

      • xyz replied to this.

        elmrganzo Thanks. If you're serious about game coding but don't have access to formal computer science education, I'd typically recommend two things. Surprisingly, none of them is directly Godot or even games related:

        • take an introductory course in linear algebra, a branch of math that deals with vectors and linear transformations
        • take a general programming course (in any language) that focuses on basic data structures (lists, stacks, hashes, etc...) and algorithms (searching, sorting, etc...)

        Knowledge from those areas will give you strong fundamentals for solving many problems typically encountered in game programming field. Investing time to thoroughly and systematically learn this stuff could pay great dividends long into your future.

          9 days later

          xyz do you recommend me begin with godot 3 or godot 4? i mean for the amount of information.

            elmrganzo IMHO unless you have a good reason (for example old tutorials that you cannot convert or unfixed bugs) you should always use the newest version.

            elmrganzo do you recommend me begin with godot 3 or godot 4? i mean for the amount of information.

            Go with 4. But if you have a tutorial for 3, switch to 3 for that tutorial to avoid confusion. Then go back to 4. There's really not that much difference between versions. The great majority of things you need to learn as a beginner is exactly the same in both versions, but some small changes in GDScript can cause you to get stuck if you follow a tutorial for 3 and try to shoehorn it into 4 yourself as you learn. It would be akin to "porting" a tutorial, which you cannot expect to be able to do if you already don't know everything the tutorial has to teach you, and some more.

            But again, better to invest time in some core math/programming skills than doing narrow-focused Godot specific tutorials. Although finishing a tutorial will give you some quick sense of accomplishment, you'll likely just learn a recipe without broader understanding of how to solve related problems.

            If you, for example, know linear algebra well, you won't need any object rotation tutorial ever, for any engine. You'd just glance at the transform class reference for a specific engine and implement whatever behavior you need.