better collisions

This commit is contained in:
Adrien MALINGREY 2023-06-27 18:50:09 +02:00
parent f582e68d89
commit eb3ae6f00c
2 changed files with 12 additions and 13 deletions

View File

@ -1,10 +1,12 @@
import * as THREE from 'three'; import * as THREE from 'three';
const mazeGeometry = new THREE.BoxGeometry( 1, 1, 1 );
export default class MazeMesh extends THREE.InstancedMesh { export default class MazeMesh extends THREE.InstancedMesh {
constructor( width, length, material ) { constructor( width, length, height, material ) {
super( mazeGeometry, material, width*length - 2 ); super(
new THREE.BoxGeometry( 1, height, 1 ),
material,
width*length - 2
);
this.length = length this.length = length
this.width = width this.width = width
this.map = new Array(length).fill().map(() => new Array(width).fill(1)) this.map = new Array(length).fill().map(() => new Array(width).fill(1))

15
main.js
View File

@ -15,7 +15,7 @@ import MazeMesh from './MazeMesh.js';
const mazeWidth = 23 const mazeWidth = 23
const parameters = { const parameters = {
elevation: 90 * Math.random(), elevation: 1 + 89 * Math.random(),
azimuth: -160, azimuth: -160,
}; };
@ -87,7 +87,7 @@ const wallMaterial = new THREE.MeshStandardMaterial({
roughnessMap: loader.load('img/stonewall-roughness.png'), roughnessMap: loader.load('img/stonewall-roughness.png'),
}) })
const maze = new MazeMesh(mazeWidth, mazeWidth, wallMaterial); const maze = new MazeMesh(mazeWidth, mazeWidth, 1, wallMaterial);
maze.castShadow = true; maze.castShadow = true;
maze.receiveShadow = true; maze.receiveShadow = true;
maze.matrixAutoUpdate = false maze.matrixAutoUpdate = false
@ -95,12 +95,13 @@ scene.add(maze)
console.log(String(maze)) console.log(String(maze))
const wall = new THREE.Mesh(maze.geometry); const invisibleWall = new THREE.Mesh(new THREE.BoxGeometry( .9, 1.8, .9 ));
let matrix = new THREE.Matrix4() let matrix = new THREE.Matrix4()
for (let i = 0; i < maze.count; i++) { for (let i = 0; i < maze.count; i++) {
maze.getMatrixAt(i, matrix) maze.getMatrixAt(i, matrix)
const clone = wall.clone() const clone = invisibleWall.clone()
clone.position.setFromMatrixPosition(matrix) clone.position.setFromMatrixPosition(matrix)
clone.position.y += 0.5;
worldOctree.fromGraphNode(clone) worldOctree.fromGraphNode(clone)
} }
@ -179,11 +180,7 @@ ground.matrixAutoUpdate = false
ground.updateMatrix(); ground.updateMatrix();
scene.add(ground) scene.add(ground)
const groundCollisioner = new THREE.Mesh( worldOctree.fromGraphNode(ground)
new THREE.PlaneGeometry(mazeWidth, mazeWidth)
)
groundCollisioner.rotation.x = - Math.PI / 2;
worldOctree.fromGraphNode(groundCollisioner)
// Water // Water