dimforge / rapier.js

Official JavaScript bindings for the Rapier physics engine.
https://rapier.rs
Apache License 2.0
397 stars 55 forks source link

`RigidBody.lockRotation` and `RigidBody.lockTranslation` do not unlock when sent `false` on its `locked` argument #282

Closed heysokam closed 1 month ago

heysokam commented 1 month ago

Found a bug where RigidBody.lockRotation and RigidBody.lockTranslation do not unlock when sent false on its locked argument.

If its not a bug, because the functions are not supposed to allow unlocking the value, then they should not have a locked argument.

RigidBody.setEnabledRotations does work as expected for both locking and unlocking.

This is the code where I was testing it, inside a Mocha UnitTest

import { RigidBodyDesc, Vector3, World, init } from '@dimforge/rapier3d-compat'

describe('TestWorld', () => {
  let physicsWorld: World | undefined = undefined

  beforeEach(async () => {
    await init()
    physicsWorld = new World({ x: 0, y: 0, z: 0 })!
  })

  afterEach(() => {
    physicsWorld = undefined
  })

  it('dummy', () => {
    // Create a dynamic rigid-body.
    const rigidBodyDesc = RigidBodyDesc.dynamic().setTranslation(0.0, 1.0, 0.0)
    const rigidBody = physicsWorld!.createRigidBody(rigidBodyDesc)
    // Ste the simulation forward.
    physicsWorld!.step()
    // Get and print the rigid-body's position.
    const position = rigidBody.translation()
    console.log('Rigid-body position: ', position.x, position.y, position.z)

    //____________________________________________________________
    // Locked Rotations
    rigidBody.lockRotations(true, false)
    rigidBody.applyTorqueImpulse(new Vector3(1, 2, 3), false)
    physicsWorld!.step()
    console.log(JSON.stringify(rigidBody.rotation()))

    // Locked Translations
    rigidBody.lockTranslations(true, false)
    rigidBody.applyImpulse(new Vector3(1, 2, 3), false)
    physicsWorld!.step()
    console.log(JSON.stringify(rigidBody.translation()))

    //____________________________________________________________
    console.log('------------------')
    //____________________________________________________________

    //____________________________________________________________
    // Unlock Rotations
    rigidBody.lockRotations(false, true)                     /// Does not work
    // rigidBody.setEnabledRotations(true,true,true, false)  /// Works
    rigidBody.applyTorqueImpulse(new Vector3(1, 2, 3), false)
    physicsWorld!.step()
    console.log(JSON.stringify(rigidBody.rotation()))

    // Unlock Translations
    rigidBody.lockTranslations(false, true)                     /// Does not work
    // rigidBody.setEnabledTranslations(true,true,true, false)  /// Works
    rigidBody.applyImpulse(new Vector3(1, 2, 3), false)
    physicsWorld!.step()
    console.log(JSON.stringify(rigidBody.translation()))
  })
})
nivekuil commented 1 month ago

relevant? https://github.com/dimforge/rapier.js/issues/245

heysokam commented 1 month ago

@nivekuil Oh, ty for that. I swear I searched for lockRotations but nothing showed up :shrug: Apparently I am on: "@dimforge/rapier3d-compat": "0.11.2",, so yeah that would explain it.