Renegade Public Forums
C&C: Renegade --> Dying since 2003™, resurrected in 2024!
Home » Renegade Discussions » Mod Forum » distance between two objects c++
distance between two objects c++ [message #432316] Thu, 08 July 2010 04:37 Go to next message
reborn is currently offline  reborn
Messages: 3231
Registered: September 2004
Location: uk - london
Karma: 0
General (3 Stars)
Someone asked me about setting a conditional that checked whether the distance between two objects was lower than 40 metres. They aaked me privately and for it in LUA, but I prefer this in the public domain, there could be others that have the same question or maybe someone else might have something useful to add...
Besides, they also said they would of posted the question in another forum that is public, so go figure? Fucking leachers!

I do not know how to write this in LUA, but here it is in C++:
//obj1 and obj2 are the Two GameObject's you're comparing.

float Dist;
Vector3 pos1, pos2;
pos1 = Commands->Get_Position(obj1);
pos2 = Commands->Get_Position(obj2);
Dist = Commands->Get_Distance(pos1, pos2);
if(Dist <= 40.0f){ // If the distance is less than 40 metres
	// do something
}
else{ // Distance is greater than 40 metres
	// do nothing?
}


or:

float Dist = Commands->Get_Distance(Commands->Get_Position(obj1),Commands->Get_Position(obj2));
if(Dist <= 40.0f){ // If the distance is less than 40 metres
	// do something
}
else{ // Distance is greater than 40 metres
	// do nothing?
}


or:

if((Commands->Get_Distance(Commands->Get_Position(obj1),Commands->Get_Position(obj2))) <= 40.0f){ // If the distance is less than 40 metres
	// do something
}


All of the above should work (not tested), but each show you basically the same thing using less lines.



[Updated on: Fri, 09 July 2010 13:09]

Report message to a moderator

Re: distance between two objects c++ [message #432329 is a reply to message #432316] Thu, 08 July 2010 07:41 Go to previous messageGo to next message
Ephphatha is currently offline  Ephphatha
Messages: 1
Registered: June 2010
Karma: 0
Recruit
For those interested in the math, the distance between two points is the magnitude of the vector from point A to point B.

Taking the first example, the distance between obj1 and obj2 would be calculated:
Vector3 pos1, pos2;
pos1 = Commands->Get_Position(obj1);
pos2 = Commands->Get_Position(obj2);

Vector3 gap = pos1 - pos2; //Doesn't matter which order the subtraction is in.
float dist = gap.length(); //Where length() returns the magnitude of the vector (is this in the sdk?)


And the magnitude is calculated by taking the square root of the sum of the square of each element.

class Vector3
{
public:
    float x, y, z;

    float length()
    {
        return sqrt(x*x + y*y + z*z);
    }
}


So if there is no length() or equivalent function, you can still get the distance if you have access to each element of the vector.
Re: distance between two objects c++ [message #432333 is a reply to message #432316] Thu, 08 July 2010 09:07 Go to previous messageGo to next message
T0tNl
Messages: 63
Registered: July 2007
Karma: 0
Recruit
Sounds good. In Love

[Updated on: Fri, 09 July 2010 13:13]

Report message to a moderator

Re: distance between two objects c++ [message #432334 is a reply to message #432333] Thu, 08 July 2010 09:10 Go to previous messageGo to next message
reborn is currently offline  reborn
Messages: 3231
Registered: September 2004
Location: uk - london
Karma: 0
General (3 Stars)
KaTaNa wrote on Thu, 08 July 2010 12:07

It's sort of hard to explain what I'm looking for, I'm wondering how can you set ob1 and ob2 to be the gameobj's you want. Eg:
How can I get obj1 to be the player that executed the command, and ob2 to be a defense or powerup or something that is bought from the command.

It's hard 2 explain this. Ob1 and Obj2 to me are undefined, so I don't know how it'll calculate the distance between a player and another object(in my case)


KaTaNa wrote on Thu, 08 July 2010 12:03

Thank you for without consent posting it, Now I know I shouldn't ask you for help.


So you want help or not?

Actually, fuck you. Thumbs Up



Re: distance between two objects c++ [message #432337 is a reply to message #432316] Thu, 08 July 2010 09:26 Go to previous messageGo to next message
renalpha is currently offline  renalpha
Messages: 1000
Registered: January 2007
Location: Holland - Zuid-Holland - ...
Karma: 0
General (1 Star)
How do you define obj1 and 2? on spawn or such?

Aircraftkiller wrote on Fri, 18 February 2011 23:50

I figured some people will still go LOLOLOL STARVING CATS LOOOOLZ UR A FAG or some dumb shit like that. Thanks for not disappointing! Smile

Re: distance between two objects c++ [message #432341 is a reply to message #432337] Thu, 08 July 2010 09:50 Go to previous messageGo to next message
T0tNl
Messages: 63
Registered: July 2007
Karma: 0
Recruit
[quote title=renalpha wrote on Fri, 09 July 2010 02:26]How do you define obj1 and 2?quote]

This. Rocked Over
Re: distance between two objects c++ [message #432342 is a reply to message #432316] Thu, 08 July 2010 09:53 Go to previous messageGo to next message
Sladewill is currently offline  Sladewill
Messages: 291
Registered: January 2009
Location: United Kingdom
Karma: 0
Recruit

Katana i suggest you say sorry to reborn, he is only trying to help you and everyone in this community. If you are looking for future assistance, he wont help.

FT-Owners - Sladewill,Snazy2007,Willdy
http://FT-Gaming.com for more info...
Re: distance between two objects c++ [message #432343 is a reply to message #432316] Thu, 08 July 2010 09:56 Go to previous messageGo to next message
T0tNl
Messages: 63
Registered: July 2007
Karma: 0
Recruit
Can anyone offer some real support and stay on topic?
Re: distance between two objects c++ [message #432346 is a reply to message #432343] Thu, 08 July 2010 10:00 Go to previous messageGo to next message
Tupolev TU-95 Bear is currently offline  Tupolev TU-95 Bear
Messages: 1176
Registered: April 2009
Location: Rìoghachd Aonaichte
Karma: 1
General (1 Star)
KaTaNa wrote on Thu, 08 July 2010 17:56

Can anyone offer some real support and stay on topic?

Actually, reborn has been nothing but a big help in the forums and this is his thanks?
The only support is reborn. Thumbs Up


Decent people


Quotes or w/e

Re: distance between two objects c++ [message #432348 is a reply to message #432346] Thu, 08 July 2010 10:05 Go to previous messageGo to next message
T0tNl
Messages: 63
Registered: July 2007
Karma: 0
Recruit
Phase-transport wrote on Thu, 08 July 2010 12:00

KaTaNa wrote on Thu, 08 July 2010 17:56

Can anyone offer some real support and stay on topic?

Actually, reborn has been nothing but a big help in the forums and this is his thanks?
The only support is reborn. Thumbs Up


I don't need to thank anyone here, it is out of his own free will at heart to help not on a condition of saying thanks or whatever. I kindly do ask someone please help with the issue so I can get it resolved and move on
Re: distance between two objects c++ [message #432362 is a reply to message #432348] Thu, 08 July 2010 11:38 Go to previous messageGo to next message
saberhawk
Messages: 1068
Registered: January 2006
Location: ::1
Karma: 0
General (1 Star)
KaTaNa wrote on Thu, 08 July 2010 13:05

Phase-transport wrote on Thu, 08 July 2010 12:00

KaTaNa wrote on Thu, 08 July 2010 17:56

Can anyone offer some real support and stay on topic?

Actually, reborn has been nothing but a big help in the forums and this is his thanks?
The only support is reborn. Thumbs Up


I don't need to thank anyone here, it is out of his own free will at heart to help not on a condition of saying thanks or whatever. I kindly do ask someone please help with the issue so I can get it resolved and move on


Sure, take a look at the source code available inside the scripts.dll package. I am positive that there are hundreds of examples of getting a GameObject* via various means...
Re: distance between two objects c++ [message #432375 is a reply to message #432316] Thu, 08 July 2010 17:44 Go to previous messageGo to next message
Hypnos is currently offline  Hypnos
Messages: 683
Registered: August 2009
Location: Scotland
Karma: 0
Colonel
Way to be a prick about it, he was only trying to help...

http://i33.tinypic.com/2ls7bzb.png

Caveman wrote on Fri, 21 January 2011 08:26

Well this topic is still going on. I have to say I haven't watched much Anime recently (maybe a year or so) the last thing I saw was GITS (for the third time)

Im not too sure whether I just dont enjoy Anime anymore or whether its just I dont have time really to shit and watch it.






Re: distance between two objects c++ [message #432394 is a reply to message #432316] Thu, 08 July 2010 22:26 Go to previous messageGo to next message
CarrierII is currently offline  CarrierII
Messages: 3804
Registered: February 2006
Location: England
Karma: 0
General (3 Stars)

Reborn! You asshole! How dare you help people by posting clear information in the public domain! We must conceal everything!

EVERYTHING!

For further interest still, the mathematical theorem used is Pythagoras' Theorem.


Renguard is a wonderful initiative
Toggle Spoiler
Re: distance between two objects c++ [message #432397 is a reply to message #432316] Fri, 09 July 2010 00:07 Go to previous messageGo to next message
cAmpa is currently offline  cAmpa
Messages: 597
Registered: March 2006
Karma: 0
Colonel
I say 30ban for reborn, for being helpful.

Bückstabü!
Re: distance between two objects c++ [message #432408 is a reply to message #432316] Fri, 09 July 2010 02:01 Go to previous messageGo to next message
saberhawk
Messages: 1068
Registered: January 2006
Location: ::1
Karma: 0
General (1 Star)
Also, a rather useful optimization when you don't need the exact length (like if you are comparing distances) is to use the squared length instead of the actual length. By doing so you can avoid the square root operation which is rather expensive.
Re: distance between two objects c++ [message #432409 is a reply to message #432316] Fri, 09 July 2010 02:16 Go to previous messageGo to next message
danpaul88 is currently offline  danpaul88
Messages: 5795
Registered: June 2004
Location: England
Karma: 0
General (5 Stars)
Here's a novel idea, why not explain the context of your request. Are you trying to get the distance in a script, in which case you can use the GameObject pointers passed to the script as parameters, or are you using some sort of hooks or what?


Also, if your getting the distance between two game objects quite a lot in your code it might be worth adding a function to do it to improve your code readability. Personally I never understood why something like this was NOT in scripts.dll....

float Get_Distance ( GameObject* obj1, GameObject* obj2 )
{
	// Gets distance between two objects
	return Commands->Get_Distance ( Commands->Get_Position ( obj1 ), Commands->Get_Position ( obj2 ) );
}





Might also be useful to have

float Get_Distance ( GameObject* obj, Vector3 pos )
{
	// Gets distance object and position
	return Commands->Get_Distance ( Commands->Get_Position ( obj ), pos );
}


To get the distance between a GameObject and an arbitrary position.


http://steamsignature.com/card/1/76561197975867233.png

[Updated on: Fri, 09 July 2010 02:19]

Report message to a moderator

Re: distance between two objects c++ [message #432437 is a reply to message #432316] Fri, 09 July 2010 12:29 Go to previous messageGo to next message
Goztow is currently offline  Goztow
Messages: 9738
Registered: March 2005
Location: Belgium
Karma: 13
General (5 Stars)
Goztoe
Reborn, may I suggest you take out the private stuff out of your posts? I Understood the person who sent them to you prefers them to stay private and they aren't really needed for the code?

You can find me in The KOSs2 (TK2) discord while I'm playing. Feel free to come and say hi! TK2 discord
Re: distance between two objects c++ [message #432446 is a reply to message #432316] Fri, 09 July 2010 14:07 Go to previous messageGo to next message
Sladewill is currently offline  Sladewill
Messages: 291
Registered: January 2009
Location: United Kingdom
Karma: 0
Recruit

but reborn posting this code enables it to help other members which may be asking the same question.

FT-Owners - Sladewill,Snazy2007,Willdy
http://FT-Gaming.com for more info...
Re: distance between two objects c++ [message #432465 is a reply to message #432316] Fri, 09 July 2010 15:50 Go to previous message
Omar007 is currently offline  Omar007
Messages: 1711
Registered: December 2007
Location: Amsterdam
Karma: 0
General (1 Star)
Goztow means he should've left the PM quote out Thumbs Up

IDK why KaTaNa wouldnt want to ask it in public though Dont Get It


http://tiberiumredux.omarpakker.nl/Old Unused Parts/Plaatjes/PromoteBanner_Hades_small.jpg
Previous Topic: C++ void On_Player_Death???
Next Topic: proximity speech
Goto Forum:
  


Current Time: Wed Dec 04 16:17:58 MST 2024

Total time taken to generate the page: 0.01132 seconds