Hello,
I’m trying to make my code more optimal by not having THREE.Vector3() creation inside useFrame().
The problem is that when I take out the declaration of m1,m2 from the const, it doesn’t work anymore. it only returns identical Box3 corresponding to the last formed Box.
I tried many different methods that all gave me the same result, like doing a function with a for loop and with .push method.
I am not sure why the function has such a behavior.
export const Player = ({ ...props }) => {
// Some code
useFrame((state, delta) => {
// Some code
boxes = myList.map((v) => AsAABB(v))
// Some code
})
}
const AsAABB = (v) => {
const m1 = new THREE.Vector3()
const m2 = new THREE.Vector3()
boxPositions.set(v[0], v[1], v[2])
m1.copy(boxPositions)
m2.copy(boxPositions)
return new THREE.Box3(m1, m2.add(BOXHITBOX))
}