Collision detection between two points?

Started by auser, November 27, 2011, 09:14:48 PM

Previous topic - Next topic

auser

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. :(

EgonOlsen

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.

auser

Oh thanks! But sorry, where can i find that method hehe? :P

EgonOlsen


auser

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?  :-[

EgonOlsen

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?

auser

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?

auser

#7
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?

EgonOlsen

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.

auser

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?

auser

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;
}

EgonOlsen

#11
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.

auser