How to make 3D Character move to a specific point

This topic is 1263 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi Everyone,

I'm having a hard time making my NPC travel to a specific created destination.

My NPCs move fine around the world witht their AI that randomizes the m_Roty angle of their direction and if they are moving or not. That works fine.

It's when i'm trying to say : "Go at that specific Location in the world" That I am having problems.

To move, my NPC use the following vector definition.

if(m_Move)
{

AnimateArms = true;
m_x1 += (float)sin(m_Roty*piover180) * MoveFactor;
m_z1 += (float)cos(m_Roty*piover180) * MoveFactor;

}

What i;m trying to figure out is how to determine the m_Roty angle at which they will travel to reach the created destination point.

Up to now i have done the following but it's not working properly...

void CVillager::CreateDestinationPoint(GLfloat a_x1, GLfloat a_z1)
{

//CREATE DESTINATION POINT
m_DestPoint = new CDestinationPoint(a_x1, a_z1, TextureLoader->DestPoint[0]);

//SET GOING HOME TO TRUE
m_GoingHome = true;

//DETERMINE ANGLE OF DIRECTION
if(m_DestPoint->m_x1 - m_x1 <= 0.0f && m_DestPoint->m_z1 - m_z1 >= 0.0f)
{
double angle = ( -( (double)m_DestPoint->m_x1 - (double)m_x1 )/( (double)m_DestPoint->m_z1 - (double)m_z1));
m_Roty = - (GLfloat)atan(angle);
}

if(m_DestPoint->m_x1 - m_x1 >= 0.0f && m_DestPoint->m_z1 - m_z1 >= 0.0f)
{
double angle = ( ( (double)m_DestPoint->m_x1 - (double)m_x1 )/( (double)m_DestPoint->m_z1 - (double)m_z1));
m_Roty = (GLfloat)atan(angle);
}

if(m_DestPoint->m_x1 - m_x1 >= 0.0f && m_DestPoint->m_z1 - m_z1 <= 0.0f)
{
double angle = -((double)m_DestPoint->m_z1 - (double)m_z1)/((double)m_DestPoint->m_x1 - (double)m_x1);
m_Roty = 90.0f + (GLfloat)atan(angle);
}

if(m_DestPoint->m_x1 - m_x1 <= 0.0f && m_DestPoint->m_z1 - m_z1 <= 0.0f)
{
double angle = ((double)m_DestPoint->m_z1 - (double)m_z1)/((double)m_DestPoint->m_x1 - (double)m_x1);
m_Roty = - 90.0f - (GLfloat)atan(angle);
}

}

Anybody has a lead for me.... The trigonometry seems right, i have sketched it out a lot and I don't see how else I could do it...

Thanks
Edited by Cydriic

Share on other sites

You should figure vectors; it's along the lines of:

difference = destination - position;
direction = normalize(difference);
movement = direction * move_speed;