I've searched for this and found a few topics , usually they used a function normalize and using simple vector subtracting which is ok , but how should I do it in sfml ? Instead of using:
Movement = p.position() - m.position();
p is the player and m is the monster
I used something like this to move on a straight line:
sf::Vector2f Tail(0,0);
if((mPlayer.getPosition().y - mMonster.GetInstance().getPosition().y) >= (mPlayer.getPosition().x - mMonster.GetInstance().getPosition().x)){
//sf::Vector2f Tail(0,0);
Tail.x = mPlayer.getPosition().x - mMonster.GetInstance().getPosition().x;
}
else if((mPlayer.getPosition().y - mMonster.GetInstance().getPosition().y) <= (mPlayer.getPosition().x - mMonster.GetInstance().getPosition().x)){
//sf::Vector2f Tail(0,0);
Tail.y = mPlayer.getPosition().y - mMonster.GetInstance().getPosition().y;
}
if(!MonsterCollosion())
mMonster.Move(Tail * (TimePerFrame.asSeconds() * 1/2 ) );
It works ok if the the height = the width for the game window, although I think it's not the best looking game when it comes to a moving monster, since it starts fast and then it gets slower
so what do you guys advise me to do ?
v = v/magnitude(v)
, andmagnitude(v) = sqrt(v.x*v.x + v.y*v.y)
. You can implement these yourself; they're not specific to any library. – Chaosed0 Aug 13 '14 at 15:37