-
Notifications
You must be signed in to change notification settings - Fork 0
/
getBodies.js
68 lines (61 loc) · 2.36 KB
/
getBodies.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import * as THREE from "three";
const sceneMiddle = new THREE.Vector3(0, 0, 0);
function getBody(RAPIER, world) {
const size = 0.1 + Math.random() * 0.25;
const range = 6;
const density = size * 1.0;
let x = Math.random() * range - range * 0.5;
let y = Math.random() * range - range * 0.5 + 3;
let z = Math.random() * range - range * 0.5;
// physics
let rigidBodyDesc = RAPIER.RigidBodyDesc.dynamic()
.setTranslation(x, y, z);
let rigid = world.createRigidBody(rigidBodyDesc);
let colliderDesc = RAPIER.ColliderDesc.ball(size).setDensity(density);
world.createCollider(colliderDesc, rigid);
const geometry = new THREE.IcosahedronGeometry(size, 1);
const material = new THREE.MeshStandardMaterial({
color: 0xffffff,
flatShading: true
});
const mesh = new THREE.Mesh(geometry, material);
const wireMat = new THREE.MeshBasicMaterial({
color: 0x990000,
wireframe: true
});
const wireMesh = new THREE.Mesh(geometry, wireMat);
wireMesh.scale.setScalar(1.001);
mesh.add(wireMesh);
function update () {
rigid.resetForces(true);
let { x, y, z } = rigid.translation();
let pos = new THREE.Vector3(x, y, z);
let dir = pos.clone().sub(sceneMiddle).normalize();
rigid.addForce(dir.multiplyScalar(-0.5), true);
mesh.position.set(x, y, z);
}
return { mesh, rigid, update };
}
function getMouseBall (RAPIER, world) {
const mouseSize = 0.25;
const geometry = new THREE.IcosahedronGeometry(mouseSize, 8);
const material = new THREE.MeshStandardMaterial({
color: 0xffffff,
emissive: 0xffffff,
});
const mouseLight = new THREE.PointLight(0xffffff, 1);
const mouseMesh = new THREE.Mesh(geometry, material);
mouseMesh.add(mouseLight);
// RIGID BODY
let bodyDesc = RAPIER.RigidBodyDesc.kinematicPositionBased().setTranslation(0, 0, 0)
let mouseRigid = world.createRigidBody(bodyDesc);
let dynamicCollider = RAPIER.ColliderDesc.ball(mouseSize * 3.0);
world.createCollider(dynamicCollider, mouseRigid);
function update (mousePos) {
mouseRigid.setTranslation({ x: mousePos.x * 5, y: mousePos.y * 5, z: 0.2 });
let { x, y, z } = mouseRigid.translation();
mouseMesh.position.set(x, y, z);
}
return { mesh: mouseMesh, update };
}
export { getBody, getMouseBall };