Sign in to follow this  

Enemy seek vector issue (opposite way)

This topic is 2548 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

Ed

Share this post


Link to post
Share on other sites

This topic is 2548 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this