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

[Bug] Prismatic Joint affected by Vector2.Normalize #47

Open
BinarySpike opened this issue Oct 20, 2022 · 1 comment
Open

[Bug] Prismatic Joint affected by Vector2.Normalize #47

BinarySpike opened this issue Oct 20, 2022 · 1 comment
Labels
bug Something isn't working

Comments

@BinarySpike
Copy link

Description

When applying a Prismatic Joint, the localAxisA is normalized. This causes {NaN, NaN} when the joint is new Vector2(0,0)

See

m_localXAxisA = Vector2.Normalize(def.localAxisA);

How To Reproduce

Create a new prismatic joint where the localAxisA is new Vector2(0,0)

You can work around the issue by using Reflection to update the private values:

var jdef = new PrismaticJointDef();
jdef.bodyA = body1;
jdef.bodyB = body2;
jdef.collideConnected = false;
jdef.localAxisA = new Vector2(0, 0);

var pJoint = world.CreateJoint(jdef);

var propX = pJoint.GetType().GetField("m_localXAxisA", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
var propY = pJoint.GetType().GetField("m_localYAxisA", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
propX.SetValue(pJoint, new Vector2(0, 0));
propY.SetValue(pJoint, new Vector2(0, 0));

Expected Behavior

Something similar to:

https://github.com/erincatto/box2d/blob/c6cc3646d1701ab3c0750ef397d2d68fc6dbcff2/src/dynamics/b2_prismatic_joint.cpp#L88

@BinarySpike BinarySpike added the bug Something isn't working label Oct 20, 2022
@HughPH
Copy link
Collaborator

HughPH commented Nov 6, 2022

We have discussed Vector2.Normalize before:
#43
#33
It's worth noting that OpenTK's Vector2 will also produce NaN with a zero length:
https://github.com/opentk/opentk/blob/682ae61055aa7b606fe9a8510d6024b2220a22ac/src/OpenTK.Mathematics/Vector/Vector2.cs#L167

So if you can make a case for it being a common enough use case to create a prismatic joint with localAxisA of 0, I would suggest the fix should go into that contructor rather than into Vector2.Normalize

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants