collisionTest

by jason870509

HTML

<div id="normal">normalVector
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/84/three.min.js"></script>
<script src="https://threejs.org/examples/js/controls/OrbitControls.js"></script>
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>

CSS

#normal {
  position: absolute;
  top: 2%;
  width: 100%;
  padding: 10px;
  color: #000000;
}

JavaScript

var renderer, scene, camera;
var controls;
var circle, circle2
var mouse = new THREE.Vector2();
var pickables = [];
var rec, mat1, mat2, raycaster, R
class Ball {
  constructor(mesh, rad = 2) {

    this.pos = new THREE.Vector2()
    this.vel = new THREE.Vector2()
    this.force = new THREE.Vector2()
    this.mesh = mesh;
    this.radius = rad;
    scene.add(this.mesh)
  }
  update(dt, i) {
    this.vel.add(this.force.clone().multiplyScalar(dt))
    this.pos.add(this.vel.clone().multiplyScalar(dt))
    this.i = i
    this.collidingPlanes(planes)
    this.collidingBalls();
    this.mesh.position.copy(this.pos)
    this.light.position.copy(this.pos)
    this.light.position.y += 20
  }
  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 + this.radius && point.projectOnPlane (plane.normal).length() < plane.length/2) {
        // position correction
        this.pos.copy(plane.ptOnPl.clone().add(point.projectOnPlane(plane.normal)))
        this.pos.add(plane.normal.clone().multiplyScalar(this.radius))
        // 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 Box2{
	constructor(){
  	this.max = new THREE.Vector3()
    this.min = new THREE.Vector3()
  }
}

init();
animate();

//r = box2 c = vector3
function check_Intersection(r, c, rad){
	
	var rad2 = rad * rad
  r.max.x -= c.x
  r.max.y -= c.y
  r.min.x -= c.x
  r.min.y -= c.y
  //console.log(r)
  if(r.max.x < 0){
    if(r.max.y < 0){ //left down  1
    	if(((r.max.x * r.max.x + r.max.y * r.max.y) < rad2))
      	return 1
      return 0
      //return ((r.max.x * r.max.x + r.max.y * r.max.y) < rad2)
    }
    else if(r.min.y > 0){ //left up  2
    	if(((r.max.x * r.max.x + r.min.y *...