Game Development Community

Cloest Object in Collision

by Nikos Beck · in Technical Issues · 11/29/2007 (12:24 pm) · 6 replies

Is there a way of identifying the closest object when there are two or three objects in a collision?

I'm using TGB btw.

The game is top-down and the player has a kind of radar. It's possible to detect one, two or more objects in the radar at a time. In the first few levels when the player sights an enemy in the radar, I'll launch a rocket to the nearest target (the rocket will follow a bezier spline to the target). In later levels, the player can launch multiple rockets, one or more against each target.

If I use collision, is there a way to guarantee that I'll receive a list in any particular order. I can't use the centre of the object as a reference, I need to know the nearest vertex of it's collision poly to sort them.

Is this something that I'd need to add to the engine? Build a collision list, sorted by the nearest vertex?

Any help would be greatly appreciated.

#1
11/29/2007 (1:34 pm)
You can always use the VectorDist function to find the distance between two points. It sounds to me as if you already have the objects and their positions, so all you need to do is loop through all of the objects, calculate the distance to each and then find the minimum. For example:

%objectCount = getWordCount(%objectList); // Assuming you have a nice list of all the objects
for (%i = 0; %i < %objectCount; %i++)
{
    %object = getWord(%objectList, %i);
    %distance = VectorDist(%object.getPosition(), %referencePosition);
    if (%minDistance $= "" || %distance < %minDistance)
        %minDistance = %distance;
}

If you have the list of objects in a SimSet, the above code could be easily modified. Hope I helped!
#2
12/04/2007 (1:28 pm)
That works but it uses the center of the object rather than each vertex of it's collision poly. I get the "person on a mountain" issue where the foot of the mountain is closer than a person on the mountain but if I use the object's center, the center of the mountain is much further away.
#3
12/05/2007 (7:23 pm)
Hi, you want %object.getCollisionPoly(), I think.

It give you a list of [x,y] pairs in object space. To get the position of each vertex in world space you need to scale the x's by the width and the y's by the height, then add it to the position of the object.

Torquescript is evil, is there a shortcut to processing this list of points besides the obvious for loop?

and by that I mean this
%poly = %this.getCollisionPoly();
%pos = %this.getPosition();
for (%i = 0; %i < getWordCount(%poly); %i += 2)
{
    %vertRelativeObjectCenter = getWord(%poly, %i) * %this.getWidth() SPC  getWord(%poly, %i + 1) * %this.getHeight();
    %vertRelativeToWorld = VectorAdd(%vertRelativeObjectCenter, %pos);
    echo(%i SPC %vertRelativeToWorld);
    // calculate distance between %vertRelativeToWorld and whatever
}
#4
12/06/2007 (6:57 am)
I didn't realize I could get access to the collision poly vertices. I'll have to try this out. Thanks.
#5
01/05/2008 (10:19 pm)
Hi, if I have a sword weapon mounted on a player, can I use the getCollisionPoly() to check whether it collide with the AI Player? Or is there other collision methods that can be used?
#6
01/06/2008 (2:43 pm)
If you turn on collisions then you should get a callback when the sword and AI collide. You need to set collision for the level and all of the objects involved.