colliding general plane
by jmcjc5u
HTML
<div id="info">General Plane
</div>
CSS
#info {
position: absolute;
top: 0px;
width: 100%;
padding: 10px;
text-align: center;
color: #ffff00
}
body {
overflow: hidden;
}
JavaScript
import * as THREE from "https://threejs.org/build/three.module.js";
javascript:(function(){var script=document.createElement('script');script.onload=function(){var stats=new Stats();document.body.appendChild(stats.dom);requestAnimationFrame(function loop(){stats.update();requestAnimationFrame(loop)});};script.src='https://mrdoob.github.io/stats.js/build/stats.min.js';document.head.appendChild(script);})()
class Particle {
constructor (mesh) {
this.pos = new THREE.Vector3()
this.vel = new THREE.Vector3()
this.force = new THREE.Vector3(0,-10,0)
this.mesh = mesh;
scene.add (mesh)
}
update (dt) {
this.vel.add (this.force.clone().multiplyScalar (dt))
this.pos.add (this.vel.clone().multiplyScalar(dt))
// collision with planes
this.collidingPlanes (planes);
this.mesh.position.copy (this.pos)
}
collidingPlanes (planes) {
const EPS = 1e-3
const CR = 0.96
for (let i = 0; i < planes.length; i++) {
let plane = planes[i]
let point = this.pos.clone().sub (plane.ptOnPl)
if ( point.dot(plane.normal) < EPS ) {
// position correction
this.pos.copy (plane.ptOnPl.clone().add (point.projectOnPlane(plane.normal)) )
// velocity update
this.vel.sub (plane.normal.clone().multiplyScalar ((1+CR)*this.vel.dot(plane.normal)))
//return; // assume particle collides with AT MOST one plane
}
}
}
}
/*
class Particle {
constructor (mesh) {
this.pos = new THREE.Vector3()
this.vel = new THREE.Vector3()
this.force = new THREE.Vector3(0,-10,0)
this.mesh = mesh;
scene.add (mesh)
}
update (dt) {
this.vel.add (this.force.clone().multiplyScalar (dt))
this.pos.add (this.vel.clone().multiplyScalar(dt))
const epsilon = 0.01
let point;
point = this.pos.clone().sub (pointOnPlane)
if (point.dot (planeNormal) < epsilon + 2) {
// position correction
this.pos.copy (pointOnPlane.add (point.projectOnPlane(planeNormal)) )
//...