Lunakepio / Mario-Kart-3.js

https://lunakepio.github.io/Mario-Kart-3.js/
MIT License
4.31k stars 952 forks source link

Collision detection #5

Closed zisra closed 7 months ago

zisra commented 7 months ago

Keep yourself on the road!

matsebas commented 7 months ago

Since it doesn't let me make a pull request, I'm copying the code to allow collision detection with the world. It's not the most efficient thing in the world, but at least it provides a better experience than now.

Change RigidBody wrapper in Tour_paris_promenade.jsx to wrap group mesh

export function Paris(props) {
  const { nodes, materials } = useGLTF('./models/tracks/tour_paris_promenade-transformed.glb')
  materials.M_Cmn_ShadowCollision.opacity = 0
  materials.M_Cmn_ShadowCollision.transparent = true
  return (
    <RigidBody type="fixed" colliders="trimesh" name="terrain">
      <group {...props} scale={50} position={[0, -3.6, 0]} dispose={null}>
        <mesh castShadow receiveShadow geometry={nodes.N_Road_Ground_M_Cmn_MainColor_Detail_0.geometry} material={materials.M_Cmn_MainColor_Detail} scale={0.01} />
        <mesh castShadow receiveShadow geometry={nodes.N_Road_Ground_M_Mobp1_Road_A_0.geometry} material={materials.M_Mobp1_Road_A} scale={0.01} />
        <mesh castShadow receiveShadow geometry={nodes.N_Road_Ground_M_Mobp1_Road_B_0.geometry} material={materials.M_Mobp1_Road_B} scale={0.01} />
        <mesh castShadow receiveShadow geometry={nodes.N_Road_Ground_M_Mobp1_Water_0.geometry} material={materials.M_Mobp1_Water} scale={0.01} />
        <mesh castShadow receiveShadow geometry={nodes.ShadowCollision_M_Cmn_ShadowCollision_0.geometry} material={materials.M_Cmn_ShadowCollision} scale={0.01} />
        <mesh castShadow receiveShadow geometry={nodes.F_BlindBuilding_M_Mobp1_Textures01_0.geometry} material={materials.M_Mobp1_Textures01} scale={0.01} />
        <mesh castShadow receiveShadow geometry={nodes.Gmob_Paris_VR_M_Mobp1_VR_0.geometry} material={materials.M_Mobp1_VR} scale={0.01} />
        <mesh castShadow receiveShadow geometry={nodes.N_Building_002_M_Mobp1_kanban_0.geometry} material={materials.M_Mobp1_kanban} scale={0.01} />
        <mesh castShadow receiveShadow geometry={nodes.N_Building_002_M_Mobp1_gaisenmon_tex_0.geometry} material={materials.M_Mobp1_gaisenmon_tex} scale={0.01} />
        <mesh castShadow receiveShadow geometry={nodes.Rainbow_model_M_Mobp1_Rainbow_0.geometry} material={materials.M_Mobp1_Rainbow} position={[-0.16, 0.001, 0]} rotation={[0, -Math.PI / 2, 0]} scale={0.01} />
        <mesh castShadow receiveShadow geometry={nodes.N_Audience_NoMove_01_M_Cmn_AudienceModel_0.geometry} material={materials.M_Cmn_AudienceModel} scale={0.01} />
      </group>
    </RigidBody>
  )
}
Lunakepio commented 7 months ago

Would you mind explain to me why you can't contribute ?

While this looks like a good idea at first, it actually isn't, the performance cost of having to check collision with the ENTIRE terrain is way to huge, which is why there's a mesh named ShadowCollision, I will add real and proper offtrack check, but it can't be that way

Lunakepio commented 7 months ago

Just fixed it by editing the 3D mesh directly in blender, thank you !