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
}