Index | FAQ | Search | Memberlist | Usergroups | Profile | Log in to check your private messages | Log in | Register

Distance between 2 entities?

| Inside3d Forums Forum Index -> QuakeC Programming

View previous topic :: View next topic
Author | Message
--- | ---
Ghost_Fang Joined: 12 Nov 2009 Posts: 162 |

Posted: Mon Jun 28, 2010 3:19 am Post subject: Distance between 2 entities? |

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 |

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

Posted: Mon Jun 28, 2010 3:28 am Post subject: |

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 |

Ghost_Fang Joined: 12 Nov 2009 Posts: 162 |

Posted: Mon Jun 28, 2010 3:54 am Post subject: |

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 |

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

Posted: Mon Jun 28, 2010 4:23 am Post subject: |

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 |

Ghost_Fang Joined: 12 Nov 2009 Posts: 162 |

Posted: Mon Jun 28, 2010 4:37 am Post subject: |

Ill try it out and see what i get.

Back to top |

Sajt Joined: 16 Oct 2004 Posts: 1026 |

Posted: Mon Jun 28, 2010 7:30 am Post subject: |

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 |

frag.machine Joined: 25 Nov 2006 Posts: 728 |

Posted: Mon Jun 28, 2010 11:21 am Post subject: |

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 |

Ghost_Fang Joined: 12 Nov 2009 Posts: 162 |

Posted: Mon Jun 28, 2010 6:27 pm Post subject: |

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 |

Display posts from previous:

| 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