Skip to content

C# Port Error: Quaternion is not normalized #11

@N1ghtTheF0x

Description

@N1ghtTheF0x

So I was trying to port this code to C#, this is how it looks (It's really 1:1 conversion)

using Godot;
using System;

public partial class SmoothCam : Camera3D
{
	[Export] public double translate_speed = 0.95;
	[Export] public double rotate_speed = 0.95;
	[Export] public double fov_speed = 0.95;
	[Export] public double near_far_speed = 0.95;
	[Export] public Node3D target;
	public override void _Ready()
	{

        }

    // Called every frame. 'delta' is the elapsed time since the previous frame.
    public override void _Process(double delta)
    {
        if(target == null) return;

		var translate_factor = 1 - Mathf.Pow(1 - translate_speed, delta * 3.45233);
		var rotate_factor = 1 - Mathf.Pow(1 - rotate_speed, delta * 3.45233);
		var target_xform = target.GlobalTransform;

		var local_transform_only_origin = new Transform3D(new(),GlobalTransform.Origin);
		var local_transfrom_only_basis = new Transform3D(GlobalTransform.Basis,new());
		local_transform_only_origin = local_transform_only_origin.InterpolateWith(target_xform, (float)translate_factor);
		local_transfrom_only_basis = local_transfrom_only_basis.InterpolateWith(target_xform, (float)rotate_factor);
		GlobalTransform = new(local_transfrom_only_basis.Basis,local_transform_only_origin.Origin);

        if(target is Camera3D camera)
        {
			if(camera.Projection == Projection)
			{
				var near_far_factor = 1 - Mathf.Pow(1 - near_far_speed, delta * 3.45233);
				var fov_factor = 1- Mathf.Pow(1 - fov_speed, delta * 3.45233);
				var new_near = (float)Mathf.Lerp(Near,camera.Near,near_far_factor);
				var new_far = (float)Mathf.Lerp(Far,camera.Far,near_far_factor);

				if(camera.Projection == ProjectionType.Orthogonal)
				{
					var new_size = (float)Mathf.Lerp(Size,camera.Size,fov_factor);
					SetOrthogonal(new_size,new_near,new_far);
				}
				else
				{
					var new_fov = (float)Mathf.Lerp(Fov,camera.Fov,fov_factor);
					SetPerspective(new_fov,new_near,new_far);
				}
			}
        }
    }
}

But I always get an exception at the InterpolateWith at local_transform_only_origin; Quaternion is not normalized.
This is really confusing because, as said, it's been ported 1:1 to C# so there should be any problems, right?
I'm using Godot 4.0.3.stable.mono

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions