Skip to content

Fix joint getReactionForce() before the first step - #324

Open
bardrop wants to merge 1 commit into
piqnt:masterfrom
bardrop-oss:fix-joint-reaction-before-step
Open

bardrop wants to merge 1 commit into
piqnt:masterfrom
bardrop-oss:fix-joint-reaction-before-step

Conversation

@bardrop

@bardrop bardrop commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Fixes #275

What was wrong

DistanceJoint, RopeJoint, PulleyJoint and GearJoint set their axis vectors (m_u, m_uA/m_uB, m_JvAC/m_JvBD) only in initVelocityConstraints(). So calling getReactionForce() on a new joint, before the first world.step(), threw:

const joint = world.createJoint(new DistanceJoint({}, bodyA, bodyB, anchorA, anchorB));
joint.getReactionForce(60);
// TypeError: Cannot read properties of undefined (reading 'x')

I checked all 11 joint types this way. These 4 threw; the other 7 already returned (0, 0) and 0.

Fix

Set these fields to zero in the constructor, as suggested in the issue comments. PrismaticJoint already does the same for m_axis / m_perp. For GearJoint I also set m_JwA..m_JwD to 0, because getReactionTorque() reads m_JwA (it would be NaN otherwise). initVelocityConstraints() still overwrites all of them, so stepping does not change.

Test

New src/__test__/Joint.test.ts: makes each of the 4 joints and checks that getReactionForce(60) is (0, 0) and getReactionTorque(60) is 0 before any step.

  • Before the fix: fails with TypeError: Cannot read properties of undefined (reading 'x').
  • After the fix: passes. Full suite: 21/21 tests pass (11 files). ESLint and Prettier are clean on the changed files.

Added a patch changeset.

DistanceJoint, RopeJoint, PulleyJoint and GearJoint set their axis
vectors only in initVelocityConstraints(), so calling getReactionForce()
before the first world step threw a TypeError. Initialize them to zero
in the constructor, like PrismaticJoint already does.

Fixes piqnt#275
@vercel

vercel Bot commented Nov 26, 2026

Copy link
Copy Markdown

@bardrop is attempting to deploy a commit to the Piqnt Team on Vercel.

A member of the Team first needs to authorize it.

This branch has not been deployed

No deployments
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

Successfully merging this pull request may close these issues.

DistanceJoint getReactionForce function not working

1 participant