Applying force when click

what i’m trying to do is when i click an object is it apply force to it. this code don’t work, what am i missing here?

document.addEventListener('click', () => { this.onClick()})
window.addEventListener("mousemove", ()=> {this.onMouseMove(event)})

onMouseMove(event) {

    this.mouse.x = (event.clientX / window.innerWidth) * 2 - 1
    this.mouse.y = -(event.clientY / window.innerHeight) * 2 + 1

}

onClick() {

    this.raycaster.setFromCamera(this.mouse, this.camera)
    const intersects = this.raycaster.intersectObjects(this.scene.children, true)

    if (intersects.length > 0 ) {
        const obj = intersects[0]
        const {object, face} = obj

        if (!object.isMesh) return

        this.bodies.forEach ( (body, i) => {
            body.applyLocalImpulse(impulse, body.position)
        })
    }
}

setWorld() {

    this.world = new CANNON.World()
    this.world.gravity.set(0, 0, -3.5)
    this.world.allowSleep = true
    this.world.broadphase = new CANNON.NaiveBroadphase();
    this.world.defaultContactMaterial.friction = 0
    this.world.defaultContactMaterial.restitution = 0.2

}

Hey, I’ll suggest you 5 things:

  1. Put a console.log or debugger or something right under if (intersects.length > 0) in order to verify your intersection detection logic works.
  2. Put another console.log after if (!object.isMesh) return to make sure you’re not having an early return for given object.
  3. Put another console.log within your forEach loop to make sure your meshes are assigned a physics body.
  4. If step 1,2,3 succeeds, give impulse vector a huge value. It’s possible that you are actually applying the impulse but the body is not moving, this happened to me a lot of times : )
  5. Try to use applyImpulse API rather than applyLocalImpulse API.