# Collisions two objects

Hello. I wrote a function that checks the collision of an object and an array. Also works and collision check of two objects, i can add pozition z but not need now. Its cool for me but maybe there is a ready function for this in three js?
And how check collision if i rotating any object?

``````function intersectObjMas(obj, mas) {
for (var i = 0; i < mas.length; i++) {
if (obj.position.x+obj.geometry.parameters.width/2 > mas[i].position.x-mas[i].geometry.parameters.width/2
&& obj.position.x-obj.geometry.parameters.width/2 < mas[i].position.x+mas[i].geometry.parameters.width/2
&& obj.position.y+obj.geometry.parameters.height/2 > mas[i].position.y-
mas[i].geometry.parameters.height/2 && obj.position.y-obj.geometry.parameters.height/2 <
mas[i].position.y+mas[i].geometry.parameters.height/2) {
isIntersect = 1;
}

}
if (isIntersect) refresh();

}``````

Look there @looeee collision-detection

Did you see that?

https://stemkoski.github.io/Three.js/Collision-Detection.html

This is my simple test example:
( It’s in German, but with https://www.deepl.com/translator easy to translate. This is how I do it . )
http://threejs.hofk.de/kollision/02_kollision.html

3 Likes

Since you are using the `position` and the `geometry` of the object, you can use the built in ThreeJS function `intersectsBox()`. You can clone the original geometrys’ bounding boxes, apply their object’s transform matrix to them and check for intersections between them.

An example of this is discussed here:

With the relevant code below

``````function detectCollisionCubes(object1, object2){
object1.geometry.computeBoundingBox(); //not needed if its already calculated
object2.geometry.computeBoundingBox();
object1.updateMatrixWorld();
object2.updateMatrixWorld();

var box1 = object1.geometry.boundingBox.clone();
box1.applyMatrix4(object1.matrixWorld);

var box2 = object2.geometry.boundingBox.clone();
box2.applyMatrix4(object2.matrixWorld);

return box1.intersectsBox(box2);
}
``````
5 Likes

its amazing. Thanx!!!

Thank you very much. This code helped me