Author Topic: Understanding collisions in 3D  (Read 1194 times)

Offline haijin

  • byte
  • *
  • Posts: 37
    • View Profile
Understanding collisions in 3D
« on: May 01, 2011, 02:23:01 pm »
Ok, solved... the second (preferred) alternative works. It's just that I wasn't setting the collision mode to SELF (only OTHERS). Still not sure about the use of the axis vectors...
Hi, this is my second post, and it's about time to thank the developers on their effort to create an easy and comfortable 3D engine. After a project directly working with opengl (Trapped, for Android), this is a welcomed change.
That said, my 3d math knowledge is quite limited and conflicting with implementation of collisions. I am working on a top down view game with NPCs trying to catch the main character. Now I want to have the NPCs avoid each other (i.e. no overlapping) when stalking the player.
I first tried without checking the corresponding examples, sending to checkForCollisionSpherical() the transformation that I would send to translate() (thinking the collision would be based on the transformed position, but also trying to add the current transformed position). Seeing that wouldn't work I checked the wiki and I see the use of getX/Z/YAxis() which I don't quite understand, still tried to implement it, obviously with no success... here's the alternatives I have tried:
Code: [Select]
       if (false){
            SimpleVector center = getTransformedCenter();
            SimpleVector t = new SimpleVector(speedX + center.x, speedY + center.y, center.z);
            SimpleVector avoid = checkForCollisionSpherical(t, 5f);
            if (avoid.equals(t))
                translate(speedX, speedY, 0);
        }
        if (false){
            SimpleVector avoid = checkForCollisionSpherical(new SimpleVector(speedX, speedY, 0), 5f);
            translate(avoid);
        }
        if (true){
            //SimpleVector move = new SimpleVector();//getTransformedCenter();
            SimpleVector t = getXAxis();
            t.scalarMul(speedX);
            move.add(t);
            t = getYAxis();
            t.scalarMul(speedY);
            move.add(t);
            move.z = 0;
//System.out.println(t);

            move = checkForCollisionSpherical(move, 5f);
            translate(move);
        }
any hints/links/explanations on how this works would be appreciated...
« Last Edit: May 01, 2011, 06:17:25 pm by haijin »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11777
    • View Profile
    • http://www.jpct.net
Re: Understanding collisions in 3D
« Reply #1 on: May 01, 2011, 09:35:01 pm »
I'm not quite sure, where the problem in your understanding is here...all collision detection is done in world space. The position vector (if any) and the direction vectors are all in world space. How to get them highly depends on your application. getXAxis and getZAxis are used in the examples but that doesn't mean that this is needed nor useful in your case.

If you know how to translate your object without doing any collision detection, you already know which vector to use for translation in the collision detection. If it doesn't work, make sure that collision mode is properly set on all objects.

Offline haijin

  • byte
  • *
  • Posts: 37
    • View Profile
Re: Understanding collisions in 3D
« Reply #2 on: May 01, 2011, 09:48:07 pm »
Yes, I first assumed the collision was done on world coordinates but, when my implementation didn't work and after looking around the forums, I messed up and went off the right path :) the issue was not setting the collision mode right... I'll try to post some real problem next time :P
cheers!