Skip to content


 Zabbix
 Grafana
 Prometheus
 Threejs and TypeScript
 SocketIO and TypeScript
 Blender Topological Earth
 Sweet Home 3D
 Design Patterns Python
 Design Patterns TypeScript
   
 Course Coupon Codes
Three.js and TypeScript
Kindle Edition
$9.99 $14.99 Paperback 
$29.99 $34.99




Design Patterns in TypeScript
Kindle Edition
$9.99 $14.99 Paperback
$19.99 $24.99




Design Patterns in Python
Kindle Edition
$9.99 $14.99 Paperback
$19.99 $24.99




Collision Detection using OBB

Description

If you don't need physics simulation, but you still want some kind of collision detection, one option you have is to use an Oriented Bounding Box (OBB).

Lesson Script

./src/client/client.ts

 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import * as THREE from 'three'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'
import Stats from 'three/examples/jsm/libs/stats.module'
import { OBB } from 'three/examples/jsm/math/OBB'

const scene = new THREE.Scene()
scene.add(new THREE.AxesHelper(5))

const light = new THREE.AmbientLight()
scene.add(light)

const camera = new THREE.PerspectiveCamera(
    75,
    window.innerWidth / window.innerHeight,
    0.1,
    1000
)
camera.position.set(0.8, 1.4, 3.0)

const renderer = new THREE.WebGLRenderer()
renderer.setSize(window.innerWidth, window.innerHeight)
document.body.appendChild(renderer.domElement)

const controls = new OrbitControls(camera, renderer.domElement)
controls.enableDamping = true

const geometry = new THREE.BoxGeometry(1, 2, 3)
geometry.computeBoundingBox()
const material = new THREE.MeshPhongMaterial()
const mesh = new THREE.Mesh(geometry, material)
mesh.position.set(4, 1, 0)
mesh.geometry.userData.obb = new OBB().fromBox3(
    mesh.geometry.boundingBox as THREE.Box3
)
mesh.userData.obb = new OBB()
scene.add(mesh)

const mesh2 = new THREE.Mesh(
    geometry,
    new THREE.MeshBasicMaterial({ color: 0x00ff00, wireframe: true })
)
mesh2.position.set(-3, 1, 0)
mesh2.geometry.userData.obb = new OBB().fromBox3(
    mesh2.geometry.boundingBox as THREE.Box3
)
mesh2.userData.obb = new OBB()

scene.add(mesh2)

const floor = new THREE.Mesh(
    new THREE.PlaneBufferGeometry(20, 20, 10, 10),
    new THREE.MeshBasicMaterial({ color: 0xaec6cf, wireframe: true })
)
floor.rotateX(-Math.PI / 2)
scene.add(floor)

window.addEventListener('resize', onWindowResize, false)
function onWindowResize() {
    camera.aspect = window.innerWidth / window.innerHeight
    camera.updateProjectionMatrix()
    renderer.setSize(window.innerWidth, window.innerHeight)
    render()
}

const stats = Stats()
document.body.appendChild(stats.dom)

const clock = new THREE.Clock()

function animate() {
    requestAnimationFrame(animate)

    mesh.position.x = Math.sin(clock.getElapsedTime() * 0.5) * 4

    controls.update()

    mesh.userData.obb.copy(mesh.geometry.userData.obb)
    mesh2.userData.obb.copy(mesh2.geometry.userData.obb)
    mesh.userData.obb.applyMatrix4(mesh.matrixWorld)
    mesh2.userData.obb.applyMatrix4(mesh2.matrixWorld)
    if (mesh.userData.obb.intersectsOBB(mesh2.userData.obb)) {
        mesh.material.color.set(0xff0000)
    } else {
        mesh.material.color.set(0x00ff00)
    }

    mesh.rotateY(0.01)
    mesh2.rotateY(-0.005)

    render()

    stats.update()
}

function render() {
    renderer.render(scene, camera)
}

animate()

OBB (Official)