Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip ??? #66

Draft
wants to merge 1 commit into
base: feat-re-collisions
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 13 additions & 58 deletions app/design/ui-3d/fresh/gestures/move.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,9 @@ import { ThreeEvent } from "@react-three/fiber"
import { Handler } from "@use-gesture/react"
import { pipe } from "fp-ts/lib/function"
import { useRef } from "react"
import { Matrix4, Vector3 } from "three"
import { Vector3 } from "three"
import { ref } from "valtio"
import {
A,
O,
pipeLog,
pipeLogWith,
someOrError,
} from "../../../../utils/functions"
import { A, O, someOrError } from "../../../../utils/functions"
import pointer from "../../../state/pointer"
import scope from "../../../state/scope"
import { dispatchOutline } from "../events/outlines"
Expand All @@ -34,7 +28,7 @@ const useOnDragMove = () => {
point0: Vector3
houseTransformsGroup: HouseTransformsGroup
layoutGroup: HouseLayoutGroup
nearHouseTransformGroups: HouseTransformsGroup[]
nearNeighbours: HouseTransformsGroup[]
thresholdFactor: number
} | null>(null)

Expand All @@ -45,34 +39,6 @@ const useOnDragMove = () => {
event: { intersections },
} = state

const computeNearHouseTransformsGroups = (
houseTransformsGroup: HouseTransformsGroup
): HouseTransformsGroup[] =>
pipe(
houseTransformsGroup.parent,
O.fromNullable,
O.map((scene) =>
pipe(
scene.children,
A.filterMap((htg) => {
if (
!isHouseTransformsGroup(htg) ||
htg.uuid === houseTransformsGroup.uuid
) {
return O.none
}

const { aabb } = getActiveHouseUserData(houseTransformsGroup)

return getActiveHouseUserData(htg).aabb.intersectsBox(aabb)
? O.some(htg)
: O.none
})
)
),
O.getOrElse((): HouseTransformsGroup[] => [])
)

switch (true) {
case first: {
pipe(
Expand All @@ -86,9 +52,6 @@ const useOnDragMove = () => {
object,
findFirstGuardUp(isHouseTransformsGroup),
O.map((houseTransformsGroup) => {
const nearHouseTransformGroups =
computeNearHouseTransformsGroups(houseTransformsGroup)

const layoutGroup = pipe(
houseTransformsGroup.userData.getActiveLayoutGroup(),
someOrError(`no active layout group in move`)
Expand All @@ -100,7 +63,8 @@ const useOnDragMove = () => {
houseTransformsGroup,
lastPoint: point,
point0: point,
nearHouseTransformGroups,
nearNeighbours:
houseTransformsGroup.userData.computeNearNeighbours(),
thresholdFactor: 1,
layoutGroup,
}
Expand Down Expand Up @@ -134,34 +98,25 @@ const useOnDragMove = () => {
const {
lastPoint,
houseTransformsGroup,
nearHouseTransformGroups,
nearNeighbours,
point0,
thresholdFactor,
layoutGroup,
} = moveData.current

let collision = false

const [px, pz] = pointer.xz
const thisPoint = new Vector3(px, 0, pz)
const delta: Vector3 = thisPoint.clone().sub(lastPoint)

const { obb: thisOBB } = getActiveHouseUserData(houseTransformsGroup)
const { obb } = layoutGroup.userData

thisOBB.center.add(delta)
obb.center.add(delta)

for (const nearHouse of nearHouseTransformGroups) {
const { obb: nearOBB } = getActiveHouseUserData(nearHouse)

console.log(`obb check`)

if (thisOBB.intersectsOBB(nearOBB)) {
collision = true
}
}
const collision =
houseTransformsGroup.userData.checkCollisions(nearNeighbours)

if (collision) {
thisOBB.center.sub(delta)
obb.center.sub(delta)
return
}

Expand All @@ -175,8 +130,8 @@ const useOnDragMove = () => {
const threshold = (AABB_OFFSET - 1) ** 2

if (dts >= threshold * thresholdFactor) {
moveData.current.nearHouseTransformGroups =
computeNearHouseTransformsGroups(houseTransformsGroup)
moveData.current.nearNeighbours =
houseTransformsGroup.userData.computeNearNeighbours()

moveData.current.thresholdFactor++
}
Expand Down
27 changes: 26 additions & 1 deletion app/design/ui-3d/fresh/gestures/rotate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
getActiveHouseUserData,
} from "../helpers/sceneQueries"
import {
HouseLayoutGroup,
HouseTransformsGroup,
isHouseTransformsGroup,
UserDataTypeEnum,
Expand All @@ -19,6 +20,8 @@ import {
const useOnDragRotate = () => {
const rotateData = useRef<{
houseTransformsGroup: HouseTransformsGroup
nearNeighbours: HouseTransformsGroup[]
layoutGroup: HouseLayoutGroup
center: Vector3
rotation0: number
angle0: number
Expand Down Expand Up @@ -47,19 +50,25 @@ const useOnDragRotate = () => {
object,
findFirstGuardUp(isHouseTransformsGroup),
O.map((houseTransformsGroup) => {
const layoutGroup =
houseTransformsGroup.userData.unsafeGetActiveLayoutGroup()

const {
obb: {
center,
center: { x: cx, z: cz },
},
} = getActiveHouseUserData(houseTransformsGroup)
} = layoutGroup.userData

const { x: x0, z: z0 } = point

const angle0 = atan2(cz - z0, cx - x0)

rotateData.current = {
houseTransformsGroup,
nearNeighbours:
houseTransformsGroup.userData.computeNearNeighbours(),
layoutGroup,
center,
rotation0: houseTransformsGroup.rotation.y,
angle0,
Expand Down Expand Up @@ -87,16 +96,32 @@ const useOnDragRotate = () => {
const {
center: { x: cx, z: cz },
houseTransformsGroup,
layoutGroup,
nearNeighbours,
} = rotateData.current

rotateData.current.angle = atan2(cz - pz, cx - px)

const angleDifference =
rotateData.current.angle - rotateData.current.angle0

const { obb } = layoutGroup.userData

// obb.??? how do I rotate the OBB by (-angleDifference)?

houseTransformsGroup.rotation.y =
rotateData.current.rotation0 - angleDifference

obb.rotation.setFromMatrix4(houseTransformsGroup.matrix)

if (houseTransformsGroup.userData.checkCollisions(nearNeighbours)) {
houseTransformsGroup.rotation.y =
rotateData.current.rotation0 + angleDifference
obb.rotation.setFromMatrix4(houseTransformsGroup.matrix)
return
}

layoutGroup.userData.updateBBs()
break
}
}
Expand Down
48 changes: 48 additions & 0 deletions app/design/ui-3d/fresh/scene/houseTransformsGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
isActiveLayoutGroup,
isElementMesh,
isHouseLayoutGroup,
isHouseTransformsGroup,
isHouseTransformsHandlesGroup,
isRotateHandlesGroup,
isXStretchHandleGroup,
Expand Down Expand Up @@ -187,6 +188,9 @@ export const createHouseTransformsGroup = ({
)
)

const unsafeGetActiveLayoutGroup = (): HouseLayoutGroup =>
pipe(getActiveLayoutGroup(), someOrError(`no active layout group`))

const setActiveLayoutGroup = (nextLayoutGroup: HouseLayoutGroup) => {
pipe(
houseTransformsGroup.userData.getActiveLayoutGroup(),
Expand Down Expand Up @@ -384,6 +388,47 @@ export const createHouseTransformsGroup = ({
activeElementMaterials[ifcTag] = specification
}

const computeNearNeighbours = (): HouseTransformsGroup[] =>
pipe(
houseTransformsGroup.parent,
O.fromNullable,
O.map((scene) =>
pipe(
scene.children,
A.filterMap((htg) => {
if (
!isHouseTransformsGroup(htg) ||
htg.uuid === houseTransformsGroup.uuid
) {
return O.none
}

const { aabb } = getActiveHouseUserData(houseTransformsGroup)

return getActiveHouseUserData(htg).aabb.intersectsBox(aabb)
? O.some(htg)
: O.none
})
)
),
O.getOrElse((): HouseTransformsGroup[] => [])
)

const checkCollisions = (neighbours: HouseTransformsGroup[]) => {
let collision = false

for (const neighbour of neighbours) {
const { obb: nearOBB } = getActiveHouseUserData(neighbour)

if (houseTransformsGroup.userData.obb.intersectsOBB(nearOBB)) {
collision = true
break
}
}

return collision
}

const houseTransformsGroupUserData: Omit<
HouseTransformsGroupUserData,
"activeLayoutGroupUuid" | "activeLayoutDnas"
Expand All @@ -404,6 +449,7 @@ export const createHouseTransformsGroup = ({
initRotateAndStretchXHandles,
updateXStretchHandleLengths,
getActiveLayoutGroup,
unsafeGetActiveLayoutGroup,
setActiveLayoutGroup,
setXStretchHandlesVisible,
setZStretchHandlesVisible,
Expand All @@ -412,6 +458,8 @@ export const createHouseTransformsGroup = ({
refreshAltSectionTypeLayouts,
resetMaterials,
changeMaterial,
computeNearNeighbours,
checkCollisions,
}

houseTransformsGroup.userData =
Expand Down
3 changes: 3 additions & 0 deletions app/design/ui-3d/fresh/scene/userData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export type HouseTransformsGroupUserData = {
updateActiveLayoutDnas: (x: string[]) => Promise<void>
updateXStretchHandleLengths: () => void
getActiveLayoutGroup: () => O.Option<HouseLayoutGroup>
unsafeGetActiveLayoutGroup: () => HouseLayoutGroup
setActiveLayoutGroup: (layoutGroup: HouseLayoutGroup) => void
setXStretchHandlesVisible: (bool?: boolean) => void
setZStretchHandlesVisible: (bool?: boolean) => void
Expand All @@ -77,6 +78,8 @@ export type HouseTransformsGroupUserData = {
dbSync: () => Promise<void>
resetMaterials: () => void
updateHandlesGroupZ: () => void
computeNearNeighbours: () => HouseTransformsGroup[]
checkCollisions: (nearNeighbours: HouseTransformsGroup[]) => boolean
}

export type HouseTransformsHandlesGroupUserData = {
Expand Down