Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect collision between two objects in JavaScript with Three.js?

There a lot of nice things for collision detection like threex.colliders or code snippets here on questions, but acutally the most stuff is old (some functions like multiplyVector3 are changed and other are removed. I have a Object3D (Character Model) and a World (3D Models: cars, trees, buildings etc.). I can move the Character with the arrow keys (moving it via translateX/Y in a render loop.

What I want is a collision detection between the character model and everything else (except ground and some else). So I need a collision detection between Object3D (Character) and WorldObjects[] (all Objects).

So, now there are a few ways to get the wanted result maybe, which is the best (fast & readable) way to perform this? And now the problem (maybe) if it works: When the Character collides with anything else, how can I stop that he can move in this direction but he can go back, right or left?

like image 272
Display Name Avatar asked Feb 11 '15 12:02

Display Name


People also ask

Which method is used to collision detection between two rectangles?

Axis-Aligned Bounding Box One of the simpler forms of collision detection is between two rectangles that are axis aligned — meaning no rotation. The algorithm works by ensuring there is no gap between any of the 4 sides of the rectangles. Any gap means a collision does not exist.

What is collision in Javascript?

The overlap of elements is what can be referred to as collision detection. This is a standard part of most games, when moving page elements to be able to detect the position and calculate if they are overlapping.

What is a collision detection in programming?

Collision detection concerns the detection of collisions between objects in the virtual environment. Primarily employed to stop objects moving through each other and the environment. Collision Detection is everywhere in computer games: between characters and characters, between characters and terrain, etc.

What is collision detection in Java?

Simply said, we need to detect when two objects collide on screen. In the next code example, we will expand the previous example. We add a new Alien sprite. We will detect two types of collisions: when the missile hits an alien ship and when our spacecraft collides with an alien.


1 Answers

You can detect collisions by using bounding boxes of objects. Bounding boxes are of type THREE.Box3 and have useful methods as isIntersectionBox, containsBox or containsPoint which will suit all your needs when you want to detect collisions.

Using them is as simple as this:

var firstObject = ...your first object...

var secondObject = ...your second object...

firstBB = new THREE.Box3().setFromObject(firstObject);

secondBB = new THREE.Box3().setFromObject(secondObject);

var collision = firstBB.isIntersectionBox(secondBB);

collision will be true if the boxes are colliding/hitting each-other. This gives you an impression on how you can use bounding boxes.

You can also check this example out. I think it will be very useful for you.

EDIT:

You should maybe also checkout the Physijs library which is a plugin to three.js.

There is someone with a similar question on Stackoverflow that you might want to keep track of.

like image 125
Wilt Avatar answered Oct 15 '22 20:10

Wilt