Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="container"></div>
<div id="info">
  <a href="https://threejs.org" target="_blank" rel="noopener noreferrer">three.js</a> - OBB (Oriented Bounding Box)
</div>
              
            
!

CSS

              
                
              
            
!

JS

              
                import * as THREE from "https://threejs.org/build/three.module.js";

import { OBB } from "https://threejs.org/examples/jsm/math/OBB.js";
import { OrbitControls } from "https://threejs.org/examples/jsm/controls/OrbitControls.js";

import Stats from "https://threejs.org/examples/jsm/libs/stats.module.js";

var camera, scene, renderer, clock, controls, stats, raycaster, plane, hand;

var isColliding = false;
var clampPoint = new THREE.Vector3();
var goBack = new THREE.Vector3();
var goBackLength = 0;

var objects = [],
  mouse = new THREE.Vector2();

init();
animate();

function init() {
  camera = new THREE.PerspectiveCamera(
    70,
    window.innerWidth / window.innerHeight,
    1,
    1000
  );
  camera.position.set(0, 0, 75);

  scene = new THREE.Scene();
  scene.background = new THREE.Color(0xffffff);

  clock = new THREE.Clock();

  raycaster = new THREE.Raycaster();

  var hemiLight = new THREE.HemisphereLight(0xffffff, 0x222222, 1.5);
  hemiLight.position.set(1, 1, 1);
  scene.add(hemiLight);

  var size = new THREE.Vector3(10, 1, 6);
  var geometry = new THREE.BoxBufferGeometry(size.x, size.y, size.z);

  // setup OBB on geometry level (doing this manually for now)

  geometry.userData.obb = new OBB();
  geometry.userData.obb.halfSize.copy(size).multiplyScalar(0.5);

  hand = new THREE.Mesh(
    geometry,

    new THREE.MeshLambertMaterial({ color: 0x0000ff })
  );
  hand.userData.obb = new OBB();
  scene.add(hand);

  plane = new THREE.Mesh(
    new THREE.PlaneBufferGeometry(1000, 1000, 32),
    new THREE.MeshBasicMaterial({
      color: 0xffff00,
      side: THREE.DoubleSide,
      visible: false
    })
  );
  plane.position.set(0, 0, 0);
  scene.add(plane);

  for (var i = 0; i < 40; i++) {
    var object = new THREE.Mesh(
      geometry,
      new THREE.MeshLambertMaterial({ color: 0x00ff00 })
    );
    object.matrixAutoUpdate = false;

    object.position.x = Math.random() *  80 - 40;
    object.position.y = Math.random() *  80 - 40;
    object.position.z = Math.random() * 1;
    
    object.rotation.x = Math.random() * 2 * Math.PI;
    object.rotation.y = Math.random() * 2 * Math.PI;
    object.rotation.z = Math.random() * 2 * Math.PI;

    object.scale.x = Math.random() * 2  + 0.5;
    object.scale.y = Math.random() + 0.5;
    object.scale.z = Math.random() + 0.5;
    
    scene.add(object);

    // bounding volume on object level (this will reflect the current world transform)

    object.userData.obb = new OBB();

    objects.push(object);
  }

  //

  renderer = new THREE.WebGLRenderer({ antialias: true });
  renderer.setPixelRatio(window.devicePixelRatio);
  renderer.setSize(window.innerWidth, window.innerHeight);
  document.body.appendChild(renderer.domElement);

  window.addEventListener("resize", onWindowResize, false);

  document.addEventListener("mousemove", updateHandPosition, false);
}


function sortIntersections(a, b) {
  return a.distance - b.distance;
}

function onWindowResize() {
  camera.aspect = window.innerWidth / window.innerHeight;
  camera.updateProjectionMatrix();

  renderer.setSize(window.innerWidth, window.innerHeight);
}

//
var anim;
function animate() {
  //anim = requestAnimationFrame(animate);

  // transform cubes

  var delta = clock.getDelta();

  for (var i = 0, il = objects.length; i < il; i++) {
    var object = objects[i];

    //object.rotation.x += delta * Math.PI * i * 0.01;
    //object.rotation.y += delta * Math.PI * 0.1;

    object.updateMatrix();
    object.updateMatrixWorld();

    // update OBB

    object.userData.obb.copy(object.geometry.userData.obb);
    object.userData.obb.applyMatrix4(object.matrixWorld);

    // reset
    isColliding = false;
    object.material.color.setHex(0x00ff00);
    hand.material.color.setHex(0x0000ff);
    goBack.set(0,0,0);
  }

  // collision detection

  var object = hand;
  var obb = object.userData.obb;

  for (var j = 0, jl = objects.length; j < jl; j++) {
    var objectToTest = objects[j];
    var obbToTest = objectToTest.userData.obb;

    // now perform intersection test

    if (obb.intersectsOBB(obbToTest) === true) {
      object.material.color.setHex(0xff0000);
      objectToTest.material.color.setHex(0xff0000);

      //do reaction
      clampPoint = obbToTest.clampPoint(hand.position, clampPoint);
      goBack.subVectors(hand.position, clampPoint);
      goBackLength=goBack.length()
      isColliding = true;

      //console.log(goBack);
    }
  }

  renderer.render(scene, camera);
}
function updateHandPosition() {
  //event.preventDefault();
  animate();

  mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
  mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;

  raycaster.setFromCamera(mouse, camera);
  var intersects = raycaster.intersectObject(plane);

  
  if (intersects.length > 0) {
    
    var dist = intersects[0].point.distanceTo( clampPoint );
    if(dist > goBackLength){
      hand.position.copy(intersects[0].point);  
    }
    
  }

  hand.updateMatrix();
  hand.updateMatrixWorld();

  // update OBB

  hand.userData.obb.copy(hand.geometry.userData.obb);
  hand.userData.obb.applyMatrix4(hand.matrixWorld);

  renderer.render(scene, camera);
}

              
            
!
999px

Console