Inside3D!
     

Distance between 2 entities?

 
Post new topic   Reply to topic    Inside3d Forums Forum Index -> QuakeC Programming
View previous topic :: View next topic  
Author Message
Ghost_Fang



Joined: 12 Nov 2009
Posts: 162

PostPosted: Mon Jun 28, 2010 3:19 am    Post subject: Distance between 2 entities? Reply with quote

I think this might be a dumb question. I think I found it by looking in the ogre code but im not sure.
I wanna add code to a bot that will add melee.
something like this: (not the real code)

Code:
if (the distance between self and target < 64)
     {
      melee();
     }
    else
    {
    bot_shoot();
    }


I don't know how to detect distance. I tried something and all they did was try to run up to you just to melee. I want them to shoot at you and if you happen to be close enough, they might punch you.
Back to top
View user's profile Send private message
Wazat



Joined: 15 Oct 2004
Posts: 732
Location: Middle 'o the desert, USA

PostPosted: Mon Jun 28, 2010 3:28 am    Post subject: Reply with quote

No problem.

Code:

dist = vlen(enemy.origin - self.origin);


vlen (vector length) will give you the length of any vector. Directional vectors (used for aiming) have a length of 1 by definition. Vector length can also be used to get the speed when used on a velocity.

Subtracting your bot's position from his enemy's position creates a weighted vector whose length is the distance between them. That gives us something to use vlen on.
_________________
When my computer inevitably explodes and kills me, my cat inherits everything I own. He may be the only one capable of continuing my work.
Back to top
View user's profile Send private message MSN Messenger
Ghost_Fang



Joined: 12 Nov 2009
Posts: 162

PostPosted: Mon Jun 28, 2010 3:54 am    Post subject: Reply with quote

so it should look like this?


Code:
local vector dist;

dist = vlen(enemy.origin - self.origin);

if (vlen < 64)
   {
   melee();
   }
   else
   {
   bot_shoot();
   }
Back to top
View user's profile Send private message
Wazat



Joined: 15 Oct 2004
Posts: 732
Location: Middle 'o the desert, USA

PostPosted: Mon Jun 28, 2010 4:23 am    Post subject: Reply with quote

Looks about right.
_________________
When my computer inevitably explodes and kills me, my cat inherits everything I own. He may be the only one capable of continuing my work.
Back to top
View user's profile Send private message MSN Messenger
Ghost_Fang



Joined: 12 Nov 2009
Posts: 162

PostPosted: Mon Jun 28, 2010 4:37 am    Post subject: Reply with quote

Ill try it out and see what i get.
Back to top
View user's profile Send private message
Sajt



Joined: 16 Oct 2004
Posts: 1026

PostPosted: Mon Jun 28, 2010 7:30 am    Post subject: Reply with quote

Ghost_Fang wrote:
so it should look like this?


Code:
local vector dist;

dist = vlen(enemy.origin - self.origin);

if (vlen < 64)
   {
   melee();
   }
   else
   {
   bot_shoot();
   }


if (vlen < 64)

should be

if (dist < 64)
_________________
F. A. Špork, an enlightened nobleman and a great patron of art, had a stately Baroque spa complex built on the banks of the River Labe.
Back to top
View user's profile Send private message
frag.machine



Joined: 25 Nov 2006
Posts: 728

PostPosted: Mon Jun 28, 2010 11:21 am    Post subject: Reply with quote

You could also use the provided range() function, that basically wraps the vlen code provided by Wazat and returns some constants:

Code:

local float dist;

dist = range(self.enemy);

if (range == RANGE_MELEE) // constants in defs.qc
{
  bot_melee ();
}
else
{
  bot_shoot ();
}

_________________
frag.machine - Q2K4 Project
http://fragmachine.quakedev.com/
Back to top
View user's profile Send private message Visit poster's website
Ghost_Fang



Joined: 12 Nov 2009
Posts: 162

PostPosted: Mon Jun 28, 2010 6:27 pm    Post subject: Reply with quote

Ohh. Well i got it working anyhow. I had to switch a couple things. This is what it ends up looking like (its a frikbot addon).

Code:
if ( (random () < 0.002) )
         {
         bot_frag1();
         }         
         else if (dist < 80)
         {
            if ( (random () < 0.05) )
            {
            bot_melee1();
            }
         }
         else
         {
         bot_shoot();
         }
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Inside3d Forums Forum Index -> QuakeC Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2004 phpBB Group