Forum

Distance between 2 entities?

Discuss programming in the QuakeC language.

Moderator: InsideQC Admins

Distance between 2 entities?

Postby Ghost_Fang » Mon Jun 28, 2010 3:19 am

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: Select all
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.
Ghost_Fang
 
Posts: 336
Joined: Thu Nov 12, 2009 4:37 am

Postby Wazat » Mon Jun 28, 2010 3:28 am

No problem.

Code: Select all
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.
Wazat
 
Posts: 771
Joined: Fri Oct 15, 2004 9:50 pm
Location: Middle 'o the desert, USA

Postby Ghost_Fang » Mon Jun 28, 2010 3:54 am

so it should look like this?


Code: Select all
local vector dist;

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

if (vlen < 64)
   {
   melee();
   }
   else
   {
   bot_shoot();
   }
Ghost_Fang
 
Posts: 336
Joined: Thu Nov 12, 2009 4:37 am

Postby Wazat » Mon Jun 28, 2010 4:23 am

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.
Wazat
 
Posts: 771
Joined: Fri Oct 15, 2004 9:50 pm
Location: Middle 'o the desert, USA

Postby Ghost_Fang » Mon Jun 28, 2010 4:37 am

Ill try it out and see what i get.
Ghost_Fang
 
Posts: 336
Joined: Thu Nov 12, 2009 4:37 am

Postby Sajt » Mon Jun 28, 2010 7:30 am

Ghost_Fang wrote:so it should look like this?


Code: Select all
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.
Sajt
 
Posts: 1215
Joined: Sat Oct 16, 2004 3:39 am

Postby frag.machine » Mon Jun 28, 2010 11:21 am

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

Code: Select all
local float dist;

dist = range(self.enemy);

if (range == RANGE_MELEE) // constants in defs.qc
{
  bot_melee ();
}
else
{
  bot_shoot ();
}
I know FrikaC made a cgi-bin version of the quakec interpreter once and wrote part of his website in QuakeC :) (LordHavoc)
User avatar
frag.machine
 
Posts: 2090
Joined: Sat Nov 25, 2006 1:49 pm

Postby Ghost_Fang » Mon Jun 28, 2010 6:27 pm

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: Select all
if ( (random () < 0.002) )
         {
         bot_frag1();
         }         
         else if (dist < 80)
         {
            if ( (random () < 0.05) )
            {
            bot_melee1();
            }
         }
         else
         {
         bot_shoot();
         }
Ghost_Fang
 
Posts: 336
Joined: Thu Nov 12, 2009 4:37 am


Return to QuakeC Programming

Who is online

Users browsing this forum: No registered users and 1 guest