Author Topic: Collision detection between two points?  (Read 2717 times)

Offline auser

  • byte
  • *
  • Posts: 9
    • View Profile
Collision detection between two points?
« on: November 27, 2011, 09:14:48 pm »
Hi. Im trying to detect collision between two Points.
I have create a small AI turret how is looking for the player. But now the AI can see thought the walls.
So i need to check if there is walls between the AI and the player.

I was trying to use:
SimpleVector collideAtWall = AITurret.checkForCollisionSpherical(playerPosition, 10);

And creating a line between Turret and collideAtWall. But the line points at the player Always anyway. :(

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12002
    • View Profile
    • http://www.jpct.net
Re: Collision detection between two points?
« Reply #1 on: November 28, 2011, 08:10:54 pm »
For line intersections, it's better to use the ray-polygon detection that either calcMinDistance or checkCollision() offer. Make sure that the walls have the proper collision modes set and it should work fine.

Offline auser

  • byte
  • *
  • Posts: 9
    • View Profile
Re: Collision detection between two points?
« Reply #2 on: November 28, 2011, 09:13:46 pm »
Oh thanks! But sorry, where can i find that method hehe? :P

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12002
    • View Profile
    • http://www.jpct.net
Re: Collision detection between two points?
« Reply #3 on: November 28, 2011, 10:54:10 pm »
in World.

Offline auser

  • byte
  • *
  • Posts: 9
    • View Profile
Re: Collision detection between two points?
« Reply #4 on: November 28, 2011, 11:24:35 pm »
Hmm i really cant solve this. Right now it works when i use jBullet like this

ClosestRayResultCallback callback = new ClosestRayResultCallback(TurretPosition, PlayerPosition);
DynamicWorld.rayTest(TurretPosition, PlayerPosition, callback);
boolean hited = callback.hasHit();

Then i even can use  callback.hitPointWorld to get the position of the collision point.

But to use jBullet collision i must have both jBullet walls and jpct walls.
I havent much knowledge about collisions, so, there would be very nice if some one have an example code?  :-[

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12002
    • View Profile
    • http://www.jpct.net
Re: Collision detection between two points?
« Reply #5 on: November 29, 2011, 10:38:36 am »
I don't get the exact problem!? There's a brief overview of collision detection in the wiki: http://www.jpct.net/wiki/index.php/Collision_detection

All you need are a position and a direction vector and the collision mode (http://www.jpct.net/doc/com/threed/jpct/Object3D.html#setCollisionMode(int)) has to be set on the walls. You might have to adjust http://www.jpct.net/doc/com/threed/jpct/Config.html#collideOffset as well. What exactly do you want from the collision detection? Is it sufficient to know that there was one or do you need the object that caused it or do you need the point in world space where it happened?

Offline auser

  • byte
  • *
  • Posts: 9
    • View Profile
Re: Collision detection between two points?
« Reply #6 on: November 29, 2011, 07:49:54 pm »
Sorry for my very bad English and it is really hard for me to explain this very simple question.
I can change my question... I want to make a gun, and when i shoot at the wall, how can i get the actual hit position in the world?

I have the SimpleVector position at the gun, and i have a SimpleVector position at the end of the bullet position somewhere in the space. How can i check the SimpleVector position from the bullet hit?

Offline auser

  • byte
  • *
  • Posts: 9
    • View Profile
Re: Collision detection between two points?
« Reply #7 on: November 29, 2011, 09:15:10 pm »
Okej, here i am trying with this own method. I want to get the hit position between their two points:

SimleVector hited = GetRayHitPosition(turretPosition, camPosition);

public SimpleVector GetRayHitPosition(SimpleVector point1, SimpleVector point2)
{
   float viewDistance = Functions.computeAverageDistance(point1, point2);
   
   SimpleVector dir = new SimpleVector();
   dir.add(point2);
   dir.sub(point1);
   float aimToPlayer = RenderEngine.ThisWorld.calcMinDistance(point1, dir, viewDistance);
   
   SimpleVector result = new SimpleVector();
   result.add(point1);
   
   dir.x = dir.x / 1 * aimToPlayer;
   dir.y = dir.y / 1 * aimToPlayer;
   dir.z = dir.z / 1 * aimToPlayer;
   
   result.add(dir);
   
   return result;
}

But it dont work very well, it is hard to explain what the wrong is. Maybe there is somehere who can help me with the code, so i can get the hit position?
« Last Edit: November 29, 2011, 09:19:47 pm by auser »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12002
    • View Profile
    • http://www.jpct.net
Re: Collision detection between two points?
« Reply #8 on: November 29, 2011, 11:09:42 pm »
Looks fine too me at first glance except that i've no idea what computeAverageDistance() does. Have you tried to work with a multiple of that value. Maybe you are just too restrictive. Apart from that, "dont work very well" is too vague.
If done right, this method should be almost pixel perfect.

Offline auser

  • byte
  • *
  • Posts: 9
    • View Profile
Re: Collision detection between two points?
« Reply #9 on: November 30, 2011, 04:56:21 pm »
I changed to:

RenderEngine.ThisWorld.calcMinDistance(point1, dir, 100000);

It seems to work pretty well, But sometimes the return value is 1.45, should it not be from 0 to 1?
And sometime it is 1.0E12, is that when it doesent hit anything?

Offline auser

  • byte
  • *
  • Posts: 9
    • View Profile
Re: Collision detection between two points?
« Reply #10 on: November 30, 2011, 07:19:56 pm »
Soo, now i Think i made it. Thank you for your time!

public SimpleVector GetRayHitPosition(SimpleVector point1, SimpleVector point2)
{
   SimpleVector dir = new SimpleVector(
         point2.x - point1.x,
         point2.y - point1.y,
         point2.z - point1.z);
   
   float hitedRay = RenderEngine.ThisWorld.calcMinDistance(point1, dir, 1000000);
   if(hitedRay == Object3D.COLLISION_NONE) return null;
   
   dir.x = dir.x * hitedRay + point1.x;
   dir.y = dir.y * hitedRay + point1.y;
   dir.z = dir.z * hitedRay + point1.z;
   
   return dir;
}

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12002
    • View Profile
    • http://www.jpct.net
Re: Collision detection between two points?
« Reply #11 on: December 01, 2011, 06:57:16 am »
Albeit 1000000 might be a bit too large for your needs. Keep in mind that it reduces performance to check all polygons of a model against the ray. You should try to use a lower value if possible.
« Last Edit: December 01, 2011, 02:09:01 pm by EgonOlsen »

Offline auser

  • byte
  • *
  • Posts: 9
    • View Profile
Re: Collision detection between two points?
« Reply #12 on: December 01, 2011, 01:57:39 pm »
Aha ahokey then i know! i Fixed that. :)