0

I have a object that looks to another object. But when I do my collision detection. He doesn't sees the rotation wish I do with the LookAt. But he uses his default rotation. Why doesn't he uses the rotation wish I do with the LookAt?

This is the problem:

enter image description here

The blue one is the default. The green one shows the blue one with the LookAt.

Why does LookAt does not effect the collision detection?

My collision detection: (I make a RTS game that is why I only use X and Z)

function collisionXZ(o1, o2) {
    if (Math.abs(o1.position.x - o2.position.x) > (o1.geometry.parameters.width + o2.geometry.parameters.width) / 2)
        return false;
    if (Math.abs(o1.position.z - o2.position.z) > (o1.geometry.parameters.depth + o2.geometry.parameters.depth) / 2)
        return false;
    return true;
}
  • 1
    What do you mean by `LookAt`? You're rotating the object with the `THREE.Object3D.lookAt()` method, that's it? What do you mean by _collision detection_? Do your collision detection algorithm uses the `matrixWorld` for testing the objects? – neeh Feb 26 '17 at 14:54
  • @neeh Thanks for your answer. Yes I rotate a 3D object with `LookAt()`. No my collision detection doesn't use matrixWorld. Do I need that? I updated my post with my collision detection. Maybe you have some feedback..? many thanks –  Feb 26 '17 at 17:58

1 Answers1

0

THREE.Object3D.lookAt() changes the rotation of your object, not its position.

Thus, if you want to test whether those two rectangles collide, you have to take the rotation into account.

Your function looks like an AABB test, which is (in most cases) only used during the broad phase of a collision detection system. Basically, the purpose of this test is to detect which object might be colliding, to avoid unnecessary greedy collision tests but it's not a real collision test in itself.

I think you're better off using a library for collision detection that provides primitive tests (Rectangle over Rectangle, Rectangle over Circle, ...)

Community
  • 1
  • 1
neeh
  • 2,777
  • 3
  • 24
  • 32
  • Thanks for you great answer. Do you have any suggestions for a libary to do collision detention with Three JS? –  Feb 27 '17 at 20:15
  • 1
    Take a look at [this question](http://stackoverflow.com/questions/11473755/how-to-detect-collision-in-three-js) and maybe [physi.js](https://chandlerprall.github.io/Physijs/) – neeh Feb 28 '17 at 15:16