for some reason the portal edge body is not set in exactly the position of the circle :…
- User
for some reason the portal edge body is not set in exactly the position of the circle : shoot(event) { this.mouse.x = (event.clientX / window.innerWidth) * 2 - 1 this.mouse.y = -(event.clientY / window.innerHeight) * 2 + 1
this.raycaster.setFromCamera({ x: 0, y: 0 }, this.game.camera.instance) const intersects = this.raycaster.intersectObjects([ this.plane, this.roof, this.scene.getObjectByName('backWall'), this.scene.getObjectByName('frontWall'), this.scene.getObjectByName('leftWall'), this.scene.getObjectByName('rightWall') ]) if (intersects.length > 0) { const intersection = intersects[0] const object = intersection.object const geometry = new THREE.CircleGeometry(5, 32) const material = new THREE.MeshBasicMaterial({ color: 0x000000 }) const circle = new THREE.Mesh(geometry, material) const portalSize = new THREE.Vector2(10, 15) const halfPortalSize = portalSize.clone().multiplyScalar(0.5) const wallSize = new THREE.Vector2(object.geometry.parameters.width, object.geometry.parameters.height) const halfWallSize = wallSize.clone().multiplyScalar(0.5) let localPosition = this.worldToLocal(object, intersection.point) localPosition.x = THREE.MathUtils.clamp(localPosition.x, -halfWallSize.x + halfPortalSize.x, halfWallSize.x - halfPortalSize.x) localPosition.y = THREE.MathUtils.clamp(localPosition.y, -halfWallSize.y + halfPortalSize.y, halfWallSize.y - halfPortalSize.y) const validPoint = this.localToWorld(object, localPosition) circle.position.copy(validPoint) circle.rotation.copy(object.rotation) circle.scale.y = 1.5 circle.position.addScaledVector(circle.getWorldDirection(new THREE.Vector3()), 0.01) this.boxBody = new CANNON.Body({ collisionFilterGroup: GROUP_PORTAL, collisionFilterMask: GROUP_WALL | GROUP_DEFAULT | GROUP_CUBE | GROUP_PLAYER, }) this.boxBody.mass = 0 this.boxBody.material = this.defaultMaterial this.boxBody.collisionResponse = false this.boxBody.addShape(new CANNON.Box(new CANNON.Vec3(3.34, 5, 1.5))) this.boxBody.quaternion.copy(circle.quaternion) this.boxBody.position.copy(circle.position) this.boxBody.class = 'portal' this.boxBody.object = intersects[0].object this.portalEdgeBody = new CANNON.Body({ collisionFilterGroup: GROUP_DEFAULT, collisionFilterMask: GROUP_PLAYER, mass: 0, material: this.defaultMaterial, }); // Position and orient the portal edge body this.portalEdgeBody.position.copy(circle.position); this.portalEdgeBody.quaternion.copy(circle.quaternion); this.portalEdgeBody.addShape(new CANNON.Box(new CANNON.Vec3(3.34, 0.1, 5)), new CANNON.Vec3(0, -5, 0)); this.physics.addBody(this.portalEdgeBody) // Keep a reference to the portal edge body circle.portalEdgeBody = this.portalEdgeBody; let intersectionDetected = false if (event.button === 0) { this.rightPortal.forEach((rightPortal) => { if (this.boxBody.object === rightPortal.physicObject.object && this.detectIntersection(this.boxBody, rightPortal.physicObject)) { intersectionDetected = true } }) } else if (event.button === 2) { this.leftPortal.forEach((leftPortal) => { if (this.boxBody.object === leftPortal.physicObject.object && this.detectIntersection(this.boxBody, leftPortal.physicObject)) { intersectionDetected = true } }) } if (intersectionDetected) { return } this.physics.addBody(this.boxBody) circle.physicObject = this.boxBody const geometry1 = new THREE.PlaneGeometry(19, 19) let materialPortal if (event.button === 0) { this.materialBlue = new THREE.ShaderMaterial({ uniforms: { iResolution: { value: new THREE.Vector2(window.innerWidth, window.innerHeight) }, iTime: { value: 0 } }, vertexShader: document.getElementById('vertexShader').textContent, fragmentShader: document.getElementById('fragmentShaderOrange').textContent }) materialPortal = this.materialBlue this.boxBody.ref = 'left' this.leftPortal.push(circle) } else if (event.button === 2) { this.materialOrange = new THREE.ShaderMaterial({ uniforms: { iResolution: { value: new THREE.Vector2(window.innerWidth, window.innerHeight) }, iTime: { value: 0 } }, vertexShader: document.getElementById('vertexShader').textContent, fragmentShader: document.getElementById('fragmentShaderBlue').textContent }) materialPortal = this.materialOrange this.boxBody.ref = 'right' this.rightPortal.push(circle) } const planePortal = new THREE.Mesh(geometry1, materialPortal) planePortal.position.copy(circle.position) planePortal.rotation.copy(circle.rotation) planePortal.position.addScaledVector(circle.getWorldDirection(new THREE.Vector3()), 0.01) this.scene.add(planePortal) circle.torus = planePortal } } - Assistant
It looks like the issue arises from the way you're adding the shape to
portalEdgeBodywith an offset, which causes it to be misaligned with the circle in your scene. Specifically, when you add the shape to theportalEdgeBody, you're using an offset of(0, -5, 0):this.portalEdgeBody.addShape(new CANNON.Box(new CANNON.Vec3(3.34, 0.1, 5)), new CANNON.Vec3(0, -5, 0));This offset moves the shape 5 units down along the y-axis relative to the body's position. As a result, the physics body doesn't align with the visual representation (
circle) in your scene.To fix this issue, you should remove the offset or adjust it appropriately so that the shape aligns correctly with the circle. Here's how you can modify your code:
Option 1: Remove the Offset
If there's no specific reason to have the offset, you can remove it entirely:
this.portalEdgeBody.addShape(new CANNON.Box(new CANNON.Vec3(3.34, 0.1, 5)));By not specifying an offset, the shape will be centered on the body's position, which you've already set to match the circle's position.
Option 2: Adjust the Offset
If the offset is necessary for your use case (for example, if you intentionally want the shape to be offset from the body's position), you need to adjust the body's position to account for the offset so that the shape aligns with the circle. Here's how you can adjust the body's position:
// Adjust the body's position to account for the offset this.portalEdgeBody.position.copy(circle.position).sub(new CANNON.Vec3(0, -5, 0)); // Add the shape with the offset this.portalEdgeBody.addShape(new CANNON.Box(new CANNON.Vec3(3.34, 0.1, 5)), new CANNON.Vec3(0, -5, 0));However, in most cases, it's simpler and more maintainable to keep the shape centered on the body and avoid unnecessary offsets unless they serve a specific purpose.
Final Adjusted Code:
Here's the adjusted snippet for adding the shape without the offset:
// Position and orient the portal edge body this.portalEdgeBody.position.copy(circle.position); this.portalEdgeBody.quaternion.copy(circle.quaternion); // Add the shape without an offset this.portalEdgeBody.addShape(new CANNON.Box(new CANNON.Vec3(3.34, 0.1, 5))); // Add the body to the physics world this.physics.addBody(this.portalEdgeBody);By aligning the shape directly with the body's position and orientation, the
portalEdgeBodyshould now be set exactly at the position of the circle, resolving the misalignment issue.Additional Tips:
- Ensure that any scaling applied to the
circle(e.g.,circle.scale.y = 1.5) is also considered if it affects the visual size relative to the physics body. If necessary, adjust the dimensions of the physics shape to match the visual representation. - Always double-check coordinate spaces (local vs. global) when positioning objects and physics bodies to ensure they align correctly.
- Use visual debugging tools or add simple visual helpers (like wireframe boxes) to verify the alignment between visual meshes and physics bodies.
By making this adjustment, your
portalEdgeBodyshould now correctly align with your circle, ensuring consistent interactions between the visual elements and the physics simulation in your Three.js and Cannon.js application. - Ensure that any scaling applied to the