I am using a scene scanned from an iPhone. In a scene scanned by iPhone, it looks like I can’t treat objects in the scene as 3D objects and the position of that object as a reference for another thing like creating the physics body of cannon-es.js
I am following this as a resource:
https://sbcode.net/threejs/physics-cannonDebugrenderer/
So I thought that I can add a physics body in the position where raycast intersect the 3d object in the scene and put 3d physics body in that place.
When it is placed in the scene, I want to be able to move it, scale it and select it so that I can manually make it’s size because it is not possible to find bounding geometry size in scanned model’s child objects.
I double clicked on chairs and i am able to place sphere in that position
but now i want to select them.
These physics body are visible by the
CannonDebugRenderer maintained and shared by @seanwasere .
This is my code to create a spahere:
function createPhysicsBody(e: MouseEvent) {
event?.preventDefault()
let mouse_position = new THREE.Vector2()
mouse_position.x = (e.clientX / renderer.domElement.clientWidth) * 2 - 1
mouse_position.y = -(e.clientY / renderer.domElement.clientHeight) * 2 + 1
raycaster.setFromCamera(pointer, camera)
const intersections = raycaster.intersectObjects(sceneObjects, true)
let intersection = intersections.length > 0 ? intersections[0] : null
if (intersection !== null) {
let point = intersection.point
let itemsBody = new CANNON.Body({
mass: 0,
shape: new CANNON.Sphere(5),
})
let itemsBodycopy = Object.assign(itemsBody, { name: 'chair' })
console.log(itemsBodycopy)
itemsBodycopy.position.copy(new CANNON.Vec3(point.x, point.y, point.z))
world.addBody(itemsBodycopy)
}
}
which is triggered by this:
renderer.domElement.addEventListener('dblclick', createPhysicsBody)
i added name element in the body with a hope that i will get this name in the CannonDebugRenderer
const cannonDebugRenderer = new CannonDebugRenderer(scene, world)
which i call
cannonDebugRenderer.update()
in animate function
i consoled in canonDebugRender, i found a mesh but could not find that name or anything !!
Is there anyway i can select this physics body so that i can do some work on the selected physics body.
Thank you