pmndrs / use-cannon

👋💣 physics based hooks for @react-three/fiber
https://cannon.pmnd.rs
2.78k stars 156 forks source link

Can a complex geometry (from a gltf file) be used to detect collisions? #127

Closed ametis70 closed 3 years ago

ametis70 commented 4 years ago

I made a model that has a invisible geometry that should act like as a cage for the physics:

image

Is there a way to use the highlighted (orange) for this purpose? I'm trying to make something like a walking simulator, and the user shouldn't be able to cross that bound.

I was thinking of splitting it into smaller parts that are all convex, but that sounds pretty time consuming and I'm not even sure if it will work. Maybe I'm missing something and there is an easier way to achieve this?

indiebubbler commented 4 years ago

I think you need to useTrimesh, but be aware that it will detect collisions only against spheres.

export const TheModel = ({ ...props }) => {
    const { position, rotation } = props
    const { nodes } = useLoader(GLTFLoader, "model.glb");
    const vertices = geometry.attributes.position.array;
    const indices = geometry.index.array
    const [ref] = useTrimesh(() => ({
        rotation,
        position,
        mass: 0,
        args: [vertices, indices]
    }))
    return (
        <mesh ref={ref} geometry={geometry} >
            <meshStandardMaterial attach="material" color='#ff0000' />
        </mesh>
    )
}
ametis70 commented 3 years ago

Thanks! I ended up using trimesh in that way

Glavin001 commented 2 years ago

[trimesh] will detect collisions only against spheres.

FYI I'm working on a way easily convert complex geometries into decomposed convex shapes in browser/Node.js, such that they can collide with other shapes, not just spheres: https://twitter.com/GlavinW/status/1518397865592303618?s=20&t=db0kTGnYxorVMd-_93CqjQ Works well with Cannon in my testing.

sawsew467 commented 7 months ago

I think you need to useTrimesh, but be aware that it will detect collisions only against spheres.

export const TheModel = ({ ...props }) => {
    const { position, rotation } = props
    const { nodes } = useLoader(GLTFLoader, "model.glb");
    const vertices = geometry.attributes.position.array;
    const indices = geometry.index.array
    const [ref] = useTrimesh(() => ({
        rotation,
        position,
        mass: 0,
        args: [vertices, indices]
    }))
    return (
        <mesh ref={ref} geometry={geometry} >
            <meshStandardMaterial attach="material" color='#ff0000' />
        </mesh>
    )
}

I don't know what is 'geometry', where you import it to use?