Hello. I have a problem, I am kinda get tired and it kinda late at night so don't get rough on me. Trust me I've researched on this and I am doing my best on studying vector math.
I am able to make a sprite to move towards a target say, a survivor which I control around the screen and the zombie follow me around. I have done it nice and easy. But I have a problem though, the movement seems to act like limit function, whereas the closer it gets, the slower it becomes. I do not know how to make the speed constant given a vector.
So here is my code I prefer this one over mine:
void Infested_One::update( float elapsed )
{
sf::Vector2f target_pos( target->getSpritePosition() ); // target position ( survivor)
sf::Vector2f target_dest( target_pos ); // position to go to target
sf::Vector2f start( getSprite().getPosition() ); // tells the position of the zomboid
sf::Vector2f delta = target_dest - start; // compute for the distance between the survivor and the zomboid
// normalize the vector
float dist = sqrt( ( target_dest.x - start.x ) * ( target_dest.x - start.x ) + ( target_dest.y * start.y ) * ( target_dest.y * start.y ) );
delta /= dist;
getSprite().setPosition( start + delta * distMoved);
}
I actually implement this code based on a site I just have found here on SFML,
http://en.sfml-dev.org/forums/index.php?topic=6654.0,
I have my original one which is quite short, and I implemented it just by getting the delta of the distance between 2 objects, then calculating
the displacement by delta * ( elapsedTime / velocity ). I know this is a stupid math formula, please pardon me that. both gave me same result. The zombie gets slower as it approaches its target.
but seems like I don't really know the concept on making the sprite move on a vector with a constant speed.
Thank you very much!
Note: I am using SFML 2.0 and I am implementing this code using the elapsedTime concept
so I used clock.restart().asSeconds() as elapsedTime.