NPC: determine the smallest angle of turn

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply

usefull?

yes
5
83%
no
1
17%
 
Total votes: 6

Katsankat
Posts: 178
Joined: Sun Mar 12, 2006 4:15 am
Contact:

NPC: determine the smallest angle of turn

Post by Katsankat »

Hey chaps lately I've been programming a NPC class to manipulate an animated model, this
class holds only very basic actions like "turn left", "turn right", "go forward", "stop", "shoot", "prone" etc ...

Most of the times, the NPC has to run somewhere.

When given a target, it is question for the NPC to know if he has to turn left
or right to be facing the objective describing the smallest angle of turn.

Best and fatest is to get the compass angle of the target and give it to the NPC
so that it can determine the fatest way to go to obj just by comparing it to
the angle it is facing at the moment get it scotty?

So when an angle is given to the NPC, it has to turn towards the target as fast
as possible using the smallest angle of turn. For example if you are facing 0° on
compass (NORTH) and the target is at 270° (WEST) on compass, you have to turn
left to describe the smallest angle, right?

This function determines if the NPC has to turn left or right, given its angle
and the target angle.

What I said did not say much about the code itself.

The code is here :

Code: Select all

#include <stdio.h>
#include <stdlib.h>

// for C code, otherwise include irrlicht.h
typedef unsigned char u8;
typedef float f32;

/*==============================================================================
  To make NPC movement more realistic, determine if, to be facing a certain
  angle,  it has to turn left or right.

  f32 pameters use degrees angles, from 0 to 359.99

  Returns 2 indicates to turn left, 1 to turn right, 0 go go straight ahead
==============================================================================*/
u8 whereToTurn(f32 myangle, f32 newrot)
{
  if (newrot == myangle) return 0;

  if (newrot > myangle)
    return ((newrot - myangle) > 180) ? 2 : 1;

  return ((myangle - newrot) > 180) ? 1 : 2;
}


void test_func()
{
 // i am looking towards 0°, target is at 270°, i got to turn left
 if (turn(0.f, 270.f) != 2) printf("error 0\n");

 // i am looking towards 0°, target is at 0°, i got to turn right
 if (turn(270.f, 0.f) != 1) printf("error 1\n");
 
 if (turn(20.f, 10.f) != 2) printf("error 2\n");
 if (turn(10.f, 20.f) != 1) printf("error 3\n");
 
 if (turn(0.f, 350.f) != 2) printf("error 4\n");
 if (turn(350.f, 0.f) != 1) printf("error 5\n");
}

int main(int argc, char *argv[])
{
  test_func();

  system("PAUSE");	
  return 0;
}
Not posted yet on the forums. If it is too much abstact just ask too mich blabla is done.

In an irrlicht app it would be used like this:

Code: Select all

vector3df mypos = camera->getPosition();
vector3df tgt = enemyNode->getPosition();

    vector3df forwardvector = tgt - mypos;
    forwardvector.Y = 0.0f;
    forwardvector.normalize();

    vector3df newrot = forwardvector.getHorizontalAngle();
    vector3df myrot = myNode->getRotation();

u8 a = whereToTurn(myrot.Y, newrot.Y);

if (a == 2)
  printf("NPC has to turn left OR \"there is a bandit on your left\"\n");
else if (a == 1)
  printf("NPC has to turn right\n");
else
  printf("NPC has to go straight forward\n");
and it makes damn good realistic movements.
Last edited by Katsankat on Tue Jun 30, 2009 3:59 pm, edited 2 times in total.
Virion
Competition winner
Posts: 2149
Joined: Mon Dec 18, 2006 5:04 am

Post by Virion »

this is useful. thanks. :D
My company: https://kloena.com
My profile: https://zhieng.com
My co-working space: https://deskspace.info
My game engine: https://kemena3d.com
Post Reply