vertxxyz / Vertx.Debugging

Debugging utilities for Unity
MIT License
508 stars 33 forks source link

Quaternion To Matrix conversion failed #13

Closed Atomiz2002 closed 1 year ago

Atomiz2002 commented 1 year ago
Error 1 Quaternion To Matrix conversion failed because input Quaternion is invalid {-nan(ind), -nan(ind), -nan(ind), -nan(ind)} l=-nan(ind) UnityEngine.Matrix4x4:TRS (UnityEngine.Vector3,UnityEngine.Quaternion,UnityEngine.Vector3) Vertx.Debugging.Shape/Arc:.ctor (UnityEngine.Vector3,UnityEngine.Quaternion,single,Vertx.Debugging.Shape/Angle) (at Library/PackageCache/com.vertx.debugging@2.1.1/Runtime/Shapes/Shapes.cs:528) Vertx.Debugging.Shape/Arc:.ctor (UnityEngine.Vector2,single,single,Vertx.Debugging.Shape/Angle) (at Library/PackageCache/com.vertx.debugging@2.1.1/Runtime/Shapes/Shapes.cs:533) Vertx.Debugging.Shape/CapsuleCast2D:Draw (Vertx.Debugging.CommandBuilder,UnityEngine.Color,UnityEngine.Color,single) (at Library/PackageCache/com.vertx.debugging@2.1.1/Runtime/Shapes/Shapes.Casts.2D.cs:516) Vertx.Debugging.Shape/CapsuleCast2D:Draw (Vertx.Debugging.CommandBuilder,UnityEngine.Color,single) (at Library/PackageCache/com.vertx.debugging@2.1.1/Runtime/Shapes/Shapes.Casts.2D.cs:455) Vertx.Debugging.D:raw (Vertx.Debugging.Shape/CapsuleCast2D,single) (at Library/PackageCache/com.vertx.debugging@2.1.1/Runtime/Draw.cs:20) Vertx.Debugging.DrawPhysics2D:CapsuleCast (UnityEngine.Vector2,UnityEngine.Vector2,UnityEngine.CapsuleDirection2D,single,UnityEngine.Vector2,single,int) (at Library/PackageCache/com.vertx.debugging@2.1.1/Runtime/DrawPhysics2D.cs:613) Entities.Items.Projectiles.Base.Projectile:OnDrawGizmos () (at Assets/Scenes/Game/Scripts/Entities/Items/Projectiles/Base/Projectile.cs:51) UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)
Error 2 Quaternion To Matrix conversion failed because input Quaternion is invalid {nan, nan, nan, nan} l=nan UnityEngine.Matrix4x4:TRS (UnityEngine.Vector3,UnityEngine.Quaternion,UnityEngine.Vector3) Vertx.Debugging.Shape/Arc:.ctor (UnityEngine.Vector3,UnityEngine.Quaternion,single,Vertx.Debugging.Shape/Angle) (at Library/PackageCache/com.vertx.debugging@2.1.1/Runtime/Shapes/Shapes.cs:528) Vertx.Debugging.Shape/Arc:.ctor (UnityEngine.Vector2,single,single,Vertx.Debugging.Shape/Angle) (at Library/PackageCache/com.vertx.debugging@2.1.1/Runtime/Shapes/Shapes.cs:533) Vertx.Debugging.Shape/CapsuleCast2D:Draw (Vertx.Debugging.CommandBuilder,UnityEngine.Color,UnityEngine.Color,single) (at Library/PackageCache/com.vertx.debugging@2.1.1/Runtime/Shapes/Shapes.Casts.2D.cs:517) Vertx.Debugging.Shape/CapsuleCast2D:Draw (Vertx.Debugging.CommandBuilder,UnityEngine.Color,single) (at Library/PackageCache/com.vertx.debugging@2.1.1/Runtime/Shapes/Shapes.Casts.2D.cs:455) Vertx.Debugging.D:raw (Vertx.Debugging.Shape/CapsuleCast2D,single) (at Library/PackageCache/com.vertx.debugging@2.1.1/Runtime/Draw.cs:20) Vertx.Debugging.DrawPhysics2D:CapsuleCast (UnityEngine.Vector2,UnityEngine.Vector2,UnityEngine.CapsuleDirection2D,single,UnityEngine.Vector2,single,int) (at Library/PackageCache/com.vertx.debugging@2.1.1/Runtime/DrawPhysics2D.cs:613) Entities.Items.Projectiles.Base.Projectile:OnDrawGizmos () (at Assets/Scenes/Game/Scripts/Entities/Items/Projectiles/Base/Projectile.cs:51) UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)
^ Repeating each frame on Unity 2021.3.24f1. Code in FixedUpdate: ```cs DrawPhysics2D.CapsuleCast( origin: Rb2D.position, size: RaycastSize, // 1.74, 4.2 for information's sake capsuleDirection: CapsuleDirection2D.Vertical, angle: 0f, direction: Rb2D.velocity, distance: RaycastDistance, // Rb2D.velocity.magnitude * Time.fixedDeltaTime layerMask: Layers.Ground); // 1 << 3 ```
vertxxyz commented 1 year ago

Rb2D.velocity is Vector2.zero in some cases (ie. not a valid direction), and I don't handle this. I've just pushed an update that handles it, 2.1.2. It should arrive in OpenUPM soon.

Atomiz2002 commented 1 year ago

It seems it still appears... at the exact same place, and from my experiments it doesnt seem like its just because of the velocity being zero. image DrawColliderBug

vertxxyz commented 1 year ago

I can't seem to replicate this unless I pass NaN into direction or angle from outside. Is there any indication of where/how NaN is being introduced into this codepath for you? If you use the debugger in the CapsuleCast2D:Draw (Shapes.Casts2D.cs:460) method, can you see where NaN gets created when this occurs?

Otherwise I could write some code to protect against it, but it's hard to make it reasonable without knowing what's introducing NaN in the first place.

vertxxyz commented 1 year ago

I've pushed 2.1.3 which should at least silently handle this case.

Atomiz2002 commented 1 year ago
Vector2 velocity = Rb2D.velocity * Mathf.Max(0f, 1f - .5f * Time.fixedDeltaTime);

Origin           = Rb2D.position + velocity *
                   Time.fixedDeltaTime + velocity.normalized *
                   (RaycastLength / 2 - RaycastRadius / 2);
Size             = new(RaycastRadius, RaycastLength);
CapsuleDirection = CapsuleDirection2D.Vertical;
Angle            = Mathf.Atan2(velocity.y, velocity.x) * Mathf.Rad2Deg + 90;
Direction        = velocity;
Distance         = RaycastDistance;
LayerMask        = Layers.Ground;

Debug.Log($"{float.IsNaN(Angle)}");

image

The Debugger:

Shapes.Casts.2D.cs image

Shapes.cs image

To me it really seems the issue is that dotA sometimes results in a value < -1, which results in NaN (https://learn.microsoft.com/en-us/dotnet/api/system.mathf.acos?view=net-7.0#returns the unity docs dont really note that, but it might be applicable there too, not sure). I checked the value of dotA when the error was not occuring and it was always either -1 or -0.9948...smth. I have no clue why it doesn't seem to appear for dotB in Arc arcA = ... but it might be irrelevant.

Note: this is on v2.1.2

vertxxyz commented 1 year ago

This helps a lot, and makes total sense now thanks! Now I can fix it properly haha. The current release should hopefully be functional, but a future release will fix it properly.