Vector3 (0, 0, 0) must be normalized error keeps popping on my debugger

Godot Version

4.7 stable

Question

I’m working on a Portal clone and got the portals to stick on the wall correctly.

But everytime i run the game, i get an error that says: The axis Vector3 (0.0, 0.0, 0.0) must be normalized.

(var final_pos = transform.rotated(axis, alpha) is where the error occurs)

var original : Vector3 = global_transform.basis.y
var cosa := original.dot(new_normal)
var alpha := acos(cosa)
var axis : Vector3 = original.cross(new_normal).normalized()

var final_pos : Transform3D = global_transform.rotated(axis, alpha)

global_transform = final_pos
global_position = attach_point

var target_dir : Vector3 = attach_point - new_normal
if not global_position.is_equal_approx(target_dir):
	look_at(target_dir, -Vector3.UP)

if new_normal == Vector3(0, 1, 0): rotation_degrees.x = 90
elif new_normal == Vector3(0, -1, 0): rotation_degrees.x = -90

Despite of the error. The game runs and the portal alignment code works just fine.

I couldn’t find a proper solution online, so does anyone know how to fix this?

Because the engine keeps spamming this error on my debugger and it lags my game.

If new_normal and original are colinear, the cross product is undefined and the engine will complaint. Check for this and use a different vector in the cross product if those vectors coincide.

Good suggestion, but can you show me a code example?

Because I’m kinda dumb..

(Also if you have any suggestions for improving the alignment code, please tell me)

Assuming both vectors are normalized:

if new_normal.is_equal_approx(original)
	# pick a different axis for original

I tried your solution but it didn’t work…

Even though i normalized both vectors like what you suggested:

var original : Vector3 = global_transform.basis.y.normalized()
if original.is_equal_approx(new_normal): original = global_transform.basis.z.normalized()
var cosa := original.dot(new_normal)
var alpha := acos(cosa)
var axis : Vector3 = original.cross(new_normal.normalized())

var final_pos : Transform3D = global_transform.rotated(axis, alpha)

Also are there any suggestions for improving my code?

For an example: When i shoot on the floor or the roof, the portal doesn’t face the direction where i shot the portal from, like in Portal 1.

Here’s an example of what i mean:

Print the values to see that your vectors are not collinear and that cross product is valid.

Do you still get the error?

Everything is fine at first.

But after that, New normal and Axis are at zero.

Which causes the error.

Well switch the axis when the vectors are collinear and see how that shows in the printouts.

Nothing changed, it’s still the same.

var original : Vector3 = global_transform.basis.y
var cosa := original.dot(new_normal)
var alpha := acos(cosa)
var axis : Vector3 = original.cross(new_normal)
if new_normal.is_equal_approx(original):
	original = global_transform.basis.z
	axis = Vector3.UP

print("Original: " + str(original))
print("New normal: " + str(new_normal))
print("Axis: " + str(axis))

var final_pos : Transform3D = global_transform.rotated(axis, alpha)

print("Final Position: " + str(final_pos))

Also i forgot to tell you this, but this is how i use the function in _process

if back_ray.is_colliding():
	var pos : Vector3 = back_ray.get_collision_point()
	var normal : Vector3 = back_ray.get_collision_normal()
	align_to_new_normal(pos, normal)

if front_ray.is_colliding():
	var pos : Vector3 = front_ray.get_collision_point()
	var normal : Vector3 = front_ray.get_collision_normal()
	align_to_new_normal(pos, normal)