Skip to content

Vector2 normalize (division with zero) #44

Open
@bQvle

Description

@bQvle
public static void Normalize(ref Vector2 value, out Vector2 result)
        {
            float factor;
            DistanceSquared(ref value, ref zeroVector, out factor);
            factor = 1f / (float)Math.Sqrt(factor);
            result.X = value.X * factor;
            result.Y = value.Y * factor;
        }

I'm currently using Farseer and had some issues with contact solving. I figured the issue might still be with Velcro.

Normalize return a Vector2(X=NaN, Y=NaN) if you try to normalize a zero vector.
because factor will be zero so 1f / (float)Math.Sqrt(factor) will result in Infinity

the solver does something like Normalize(Position1 - Position2) so if the two positions is identical we get the NaN vector which crashes the physics.

Hopes this helps :)

I solved it this way

public static void Normalize(ref Vector2 value, out Vector2 result) {
            if (value == zeroVector)
                result = zeroVector;
            else {
                float factor;
                DistanceSquared(ref value, ref zeroVector, out factor);
                factor = 1f / (float)Math.Sqrt(factor);
                result.x = value.x * factor;
                result.y = value.y * factor;
            }
        }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions