Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C# Port Error: Quaternion is not normalized #11

Open
N1ghtTheF0x opened this issue Jun 24, 2023 · 3 comments
Open

C# Port Error: Quaternion is not normalized #11

N1ghtTheF0x opened this issue Jun 24, 2023 · 3 comments

Comments

@N1ghtTheF0x
Copy link

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

@fire
Copy link

fire commented Jun 24, 2023

Basis and Quaternions need special treatment and I don't know your types.

@N1ghtTheF0x
Copy link
Author

Basis and Quaternions need special treatment and I don't know your types.

There are no types defined by me, everything is from Godot and what do you mean with "special treatment"? Why does C# need it, but GDScript not?

@fire
Copy link

fire commented Jun 24, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants