Find nearest vehicle? [message #446647] |
Sat, 07 May 2011 04:51 |
robbyke
Messages: 348 Registered: September 2010 Location: Belgium
Karma: 0
|
Recruit |
|
|
is there a way to find the nearest vehicle?
in C++
Owner of kambot TT server
kambot.freeforums.org
|
|
|
Re: Find nearest vehicle? [message #446651 is a reply to message #446647] |
Sat, 07 May 2011 08:19 |
|
Omar007
Messages: 1711 Registered: December 2007 Location: Amsterdam
Karma: 0
|
General (1 Star) |
|
|
Something like this should do the trick.
GameObject * getClosestVehicle(GameObject *sourceObject)
{
GenericSLNode *x = SmartGameObjList->HeadNode;
float closestDist = FLT_MAX;
Vector3 sourceDist = Commands->Get_Position(sourceObject);
GameObject *closestVeh = NULL;
while (x)
{
GameObject *o = (GameObject *)x->NodeData;
if (o && o != sourceObject && Is_Vehicle(o))
{
Vector3 TargetPos = Commands->Get_Position(o);
float distance = Commands->Get_Distance(sourceDist, TargetPos);
if (distance < closestDist)
{
closestDist = distance;
closestVeh = o;
}
}
x = x->NodeNext;
}
return closestVeh;
}
NOTE: This code might not be 100% because I wrote it down quickly in Notepad.
[Updated on: Sun, 08 May 2011 13:00] Report message to a moderator
|
|
|
Re: Find nearest vehicle? [message #446653 is a reply to message #446647] |
Sat, 07 May 2011 09:42 |
robbyke
Messages: 348 Registered: September 2010 Location: Belgium
Karma: 0
|
Recruit |
|
|
WOW thnx i wondered if there was an existing code already you just made my work easy
only one small linking error beacause you initialise closestVeh but dont assign anythin to it
edit:
ok that didnt work out as i hoped to
Owner of kambot TT server
kambot.freeforums.org
[Updated on: Sat, 07 May 2011 10:23] Report message to a moderator
|
|
|
Re: Find nearest vehicle? [message #446665 is a reply to message #446647] |
Sat, 07 May 2011 15:07 |
|
Omar007
Messages: 1711 Registered: December 2007 Location: Amsterdam
Karma: 0
|
General (1 Star) |
|
|
You mean it does not work, or that it does not do what you want?
I'm fairly sure this gets the closest vehicle 0o
Also, if it does not work, please tell me what it is that does not work and don't just say that it doesn't work.
[Updated on: Sat, 07 May 2011 15:08] Report message to a moderator
|
|
|
Re: Find nearest vehicle? [message #446666 is a reply to message #446647] |
Sat, 07 May 2011 15:13 |
iRANian
Messages: 4308 Registered: April 2011
Karma: 0
|
General (4 Stars) |
|
|
initalizing but not using using a variable should link correctly, unless you have 'treat warnings as errors' turned on in your compiler (most compilers warn if variables aren't used).
EDIT: closestVeh is assigned to 'o' in the inner if-loop. So the compiler shouldn't be giving that error.
Long time and well respected Renegade community member, programmer, modder and tester.
Scripts 4.0 private beta tester since May 2011.
My Renegade server plugins releases
[Updated on: Sat, 07 May 2011 15:17] Report message to a moderator
|
|
|
|
|
Re: Find nearest vehicle? [message #446681 is a reply to message #446647] |
Sun, 08 May 2011 02:25 |
robbyke
Messages: 348 Registered: September 2010 Location: Belgium
Karma: 0
|
Recruit |
|
|
It didnt work because it does find the nearest vehicle wich is, if your in a vehicle, yourself
and i meant it didnt work as i hoped to, and i didnt really had found out why yet, but with some adjustements i got it to work
Owner of kambot TT server
kambot.freeforums.org
[Updated on: Sun, 08 May 2011 02:28] Report message to a moderator
|
|
|
|
|
|
|