From ac97a0192da696b7eda0ca60544788b3ec68c962 Mon Sep 17 00:00:00 2001 From: Jackie <1121@generative.xyz> Date: Thu, 1 Feb 2024 13:22:22 +0700 Subject: [PATCH 1/2] fix: place brick --- .../workshop/components/Brick/index.tsx | 24 +++++++++++-------- src/utils/helpers.ts | 22 +++++++++++++---- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/modules/workshop/components/Brick/index.tsx b/src/modules/workshop/components/Brick/index.tsx index e0043b8..9158610 100644 --- a/src/modules/workshop/components/Brick/index.tsx +++ b/src/modules/workshop/components/Brick/index.tsx @@ -141,9 +141,7 @@ export const Brick = ({ brickBoundingBox.max.x = roundToNearestMultiple(brickBoundingBox.max.x, base) brickBoundingBox.max.y = roundToNearestMultiple(brickBoundingBox.max.y, heightBase) brickBoundingBox.max.z = roundToNearestMultiple(brickBoundingBox.max.z, base) - - console.log('ADD : ', uID, ' :::: ', brickBoundingBox) - + console.log('add uid ', uID) bricksBoundBox.current[uID] = { uID, brickBoundingBox } return () => { @@ -238,13 +236,9 @@ export const Brick = ({ {!isNontTexture && ( {/**/} - + )} diff --git a/src/utils/helpers.ts b/src/utils/helpers.ts index 5aa8e7a..90c6077 100644 --- a/src/utils/helpers.ts +++ b/src/utils/helpers.ts @@ -1,6 +1,7 @@ import { mergeBufferGeometries } from 'three-stdlib' import { base, heightBase, knobSize } from '../constant/datablocks' import { BoxGeometry, CylinderGeometry } from 'three' +import { diffProps } from '@react-three/fiber/dist/declarations/src/core/utils' export function CSSToHex(cssColor) { return parseInt(`0x${cssColor.substring(1)}`, 16) @@ -66,6 +67,7 @@ export function radToDeg(angle) { export const checkCollision = (boundingBoxToCheck, otherBoundingBoxes) => { let isCollied = false let isSomethingBelow = false + let isSomethingTop = false let isFirstLayer = boundingBoxToCheck.brickBoundingBox.min.y === 0 if (otherBoundingBoxes.length < 1) return true @@ -78,6 +80,11 @@ export const checkCollision = (boundingBoxToCheck, otherBoundingBoxes) => { const diffX = boundingBoxToCheck.brickBoundingBox.min.x - brickBoundingBox.min.x const diffZ = boundingBoxToCheck.brickBoundingBox.min.z - brickBoundingBox.min.z const diffY = boundingBoxToCheck.brickBoundingBox.min.y - brickBoundingBox.min.y + console.log( + otherBoundingBoxes[index].uID, + ' ::: ', + boundingBoxToCheck.brickBoundingBox.min.y - brickBoundingBox.min.y, + ) const widthToCheck = Math.round( boundingBoxToCheck.brickBoundingBox.max.x - boundingBoxToCheck.brickBoundingBox.min.x, ) @@ -85,32 +92,37 @@ export const checkCollision = (boundingBoxToCheck, otherBoundingBoxes) => { boundingBoxToCheck.brickBoundingBox.max.z - boundingBoxToCheck.brickBoundingBox.min.z, ) - if (diffY <= 0) { + console.log(diffY) + + if (diffY === 0) { // TOP LEFT CORNER if (Math.abs(diffX) === base && Math.abs(diffZ) === base) { + console.log('collied case 1', diffX, diffZ, diffY, widthToCheck, depthToCheck) isCollied = true break } // BOTTOM LEFT CORNER if ((Math.abs(diffX) === base || diffX === 0) && diffZ >= 0 && diffZ <= base && widthToCheck !== base) { + console.log('collied case 2', diffX, diffZ, diffY, widthToCheck, depthToCheck) isCollied = true break } if ((Math.abs(diffZ) === base || diffZ === 0) && diffX >= 0 && diffX <= base && widthToCheck !== base) { + console.log('collied case 3', diffX, diffZ, diffY, widthToCheck, depthToCheck) isCollied = true break } } - // Filter out the top layer if (isFirstLayer || Math.abs(diffY) > heightBase) continue - if (diffY === heightBase && Math.abs(diffX) <= base && Math.abs(diffZ) <= base) isSomethingBelow = true - } + if (diffY > 0 && Math.abs(diffX) <= base && Math.abs(diffZ) <= base) isSomethingBelow = true - return !isCollied && ((isSomethingBelow && !isFirstLayer) || isFirstLayer) // true if it is not colliding + if (diffY < 0 && diffY === -heightBase) isSomethingTop = true + } + return !isCollied && (((isSomethingBelow || isSomethingTop) && !isFirstLayer) || isFirstLayer) // true if it is not colliding } export function uID(length = 8) { From c495f25f004239bebae1c2a60a0a66b2292d8ba0 Mon Sep 17 00:00:00 2001 From: Jackie <1121@generative.xyz> Date: Thu, 1 Feb 2024 13:51:24 +0700 Subject: [PATCH 2/2] fix: collision y axis --- .../workshop/components/Brick/index.tsx | 2 +- src/utils/helpers.ts | 20 +++---------------- 2 files changed, 4 insertions(+), 18 deletions(-) diff --git a/src/modules/workshop/components/Brick/index.tsx b/src/modules/workshop/components/Brick/index.tsx index 9158610..9b17f08 100644 --- a/src/modules/workshop/components/Brick/index.tsx +++ b/src/modules/workshop/components/Brick/index.tsx @@ -141,7 +141,7 @@ export const Brick = ({ brickBoundingBox.max.x = roundToNearestMultiple(brickBoundingBox.max.x, base) brickBoundingBox.max.y = roundToNearestMultiple(brickBoundingBox.max.y, heightBase) brickBoundingBox.max.z = roundToNearestMultiple(brickBoundingBox.max.z, base) - console.log('add uid ', uID) + bricksBoundBox.current[uID] = { uID, brickBoundingBox } return () => { diff --git a/src/utils/helpers.ts b/src/utils/helpers.ts index 90c6077..a9ef68a 100644 --- a/src/utils/helpers.ts +++ b/src/utils/helpers.ts @@ -80,37 +80,23 @@ export const checkCollision = (boundingBoxToCheck, otherBoundingBoxes) => { const diffX = boundingBoxToCheck.brickBoundingBox.min.x - brickBoundingBox.min.x const diffZ = boundingBoxToCheck.brickBoundingBox.min.z - brickBoundingBox.min.z const diffY = boundingBoxToCheck.brickBoundingBox.min.y - brickBoundingBox.min.y - console.log( - otherBoundingBoxes[index].uID, - ' ::: ', - boundingBoxToCheck.brickBoundingBox.min.y - brickBoundingBox.min.y, - ) - const widthToCheck = Math.round( - boundingBoxToCheck.brickBoundingBox.max.x - boundingBoxToCheck.brickBoundingBox.min.x, - ) - const depthToCheck = Math.round( - boundingBoxToCheck.brickBoundingBox.max.z - boundingBoxToCheck.brickBoundingBox.min.z, - ) - - console.log(diffY) + const widthToCheck = boundingBoxToCheck.brickBoundingBox.max.x - boundingBoxToCheck.brickBoundingBox.min.x + const depthToCheck = boundingBoxToCheck.brickBoundingBox.max.z - boundingBoxToCheck.brickBoundingBox.min.z if (diffY === 0) { // TOP LEFT CORNER if (Math.abs(diffX) === base && Math.abs(diffZ) === base) { - console.log('collied case 1', diffX, diffZ, diffY, widthToCheck, depthToCheck) isCollied = true break } // BOTTOM LEFT CORNER if ((Math.abs(diffX) === base || diffX === 0) && diffZ >= 0 && diffZ <= base && widthToCheck !== base) { - console.log('collied case 2', diffX, diffZ, diffY, widthToCheck, depthToCheck) isCollied = true break } if ((Math.abs(diffZ) === base || diffZ === 0) && diffX >= 0 && diffX <= base && widthToCheck !== base) { - console.log('collied case 3', diffX, diffZ, diffY, widthToCheck, depthToCheck) isCollied = true break } @@ -120,7 +106,7 @@ export const checkCollision = (boundingBoxToCheck, otherBoundingBoxes) => { if (diffY > 0 && Math.abs(diffX) <= base && Math.abs(diffZ) <= base) isSomethingBelow = true - if (diffY < 0 && diffY === -heightBase) isSomethingTop = true + if (diffY < 0 && diffY === -heightBase && Math.abs(diffX) <= base && Math.abs(diffZ) <= base) isSomethingTop = true } return !isCollied && (((isSomethingBelow || isSomethingTop) && !isFirstLayer) || isFirstLayer) // true if it is not colliding }