Jump to content
  • Advertisement
Sign in to follow this  

Enemy seek vector issue (opposite way)

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I am working on a game and I have the enemies seek the player with a very simple method of turning their direction with respect to the vector from the enemy to the player. It works quite well and I am happy with the movement of the enemies, except for one small problem, when they are walking directly away from me, the vector I use to change their heading direction doesn't turn them. I would like to stay clear of using matrices because there is tons and tons of objects and collisions at the same time and performance is of high value. My current 'seek' code is showing below:

toTarget = (Game1.player[seekTarget].position - Game1.mapOffset) - (position - Game1.mapOffset);

//normalize the vector (inline) and avoid dividing by zero
length = (float)Math.Sqrt((toTarget.X * toTarget.X + toTarget.Y * toTarget.Y));
if (length <= 0.001)
length = 0.00001f;
toTarget.X = toTarget.X / length;
toTarget.Y = toTarget.Y / length;

//turn the direction of its velocity
m_vTurn = toTarget - m_vHeading;

//normalize the vector(same as before)
length = (float)Math.Sqrt((m_vTurn.X * m_vTurn.X + m_vTurn.Y * m_vTurn.Y));
if (length <= 0.1)
length = 0.00001f;
m_vTurn.X = m_vTurn.X / length;
m_vTurn.Y = m_vTurn.Y / length;

//make sure it's not turning faster than it's allowed
m_vTurn = Vector2.Clamp(m_vTurn, new Vector2(-(float)m_dMaxTurnRate), new Vector2((float)m_dMaxTurnRate));

//implement acceleration in it's movement
Vector2 acceleration = m_vTurn / new Vector2((float)m_dMass, (float)m_dMass);

//change its heading from acceleration
m_vHeading += acceleration * timeLapse / 16;

//normalize the vector(again!)
length = (float)Math.Sqrt((m_vHeading.X * m_vHeading.X + m_vHeading.Y * m_vHeading.Y));
if (length <= 0.1)
length = 0.00001f;
m_vHeading.X = m_vHeading.X / length;
m_vHeading.Y = m_vHeading.Y / length;

//store the rotation so the enemy is drawn facing the correct way
rotation = (float)Math.Atan2(m_vHeading.Y, m_vHeading.X) + (float)Math.PI / 2;

//finally make the changes to their position, since heading is
//normalized we can multiply by its max speed
position.X += speed.X * m_vHeading.X * timeLapse / 16;
position.Y += speed.Y * m_vHeading.Y * timeLapse / 16;

so you can see its pretty simple, but I am trying to implement a simple cost effective way to determine that it is walking away and turn it accordingly.. any ideas?? thanks a lot


Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!