0

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 ?

  • The normalize function is essentially just v = v/magnitude(v), and magnitude(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
  • @Chaosed0, thanks for your help , it turns out it only moves the vector 1 pixel at a time , it's not really helpful as I thought it would be. – user3504658 Aug 13 '14 at 18:22
  • Normalization alone will get you a unit vector, it's true, but you can multiply the resulting vector by a scalar (number) to scale it. If you want your monster to move 50 units at a time, just multiply the normalized vector by 50. – Chaosed0 Aug 13 '14 at 19:21
  • @Chaosed0, the problem isn't here , as I said I can move my objects however I like , but my game depends up on moving on a straight line , for example if my player was in a position(100,100) and my monster was in position (300,300) when I use p - m it'll actually move the monster to the player but not in a straight line moving on x then y or y then x , so you could say that I want the monster to tail the player but not in a diagonal line , you remember pacman ? you remember the little monsters that moves on a straight line ? basically I want that. – user3504658 Aug 13 '14 at 19:39
  • Ah, okay, by "straight" you mean "horizontal and vertical only" (a diagonal line is still straight). – Chaosed0 Aug 14 '14 at 13:43
  • @Chaosed0 , Yes only horizontal and vertical. – user3504658 Aug 14 '14 at 19:45

2 Answers2

1

My English is poor, but here's some pseudocode:

// angle (in radians) between monster and player
float angle = atan2(player.y - monster.y, player.x - monster.x);
// monster.speed is the amount of pixels to move
// If this doesn't work, invert cos for x and sin for y
monster.x += sin(angle) * monster.speed;
monster.y += cos(angle) * monster.speed;
Anko
  • 13,393
  • 10
  • 54
  • 82
user50466
  • 11
  • 1
  • Thanks for your reply but it doesn't work unless I'm not following you correctly , first I find the angle and then use what you said at last the cos with the x and sin with y, it does follow the player but it's not on a straight line. – user3504658 Aug 13 '14 at 21:41
0

If I understand it correctly, then you want the monster to move only in horizontal or vertical direction. Which means that for every frame, you need to select the direction to move in.

One way to select could be based on which distance is more. Once you choose that, then you know the direction to move (horiz: (1,0) or vert:(0,1)), so just multiply that with the amount to move (which depends on the speed):

sf::Vector2f horizVec(1, 0);
sf::Vector2f vertVec(0, 1);

sf::Vector2f Tail(0,0);

float ydelta = mPlayer.getPosition().y - mMonster.GetInstance().getPosition().y;
float xdelta = mPlayer.getPosition().x - mMonster.GetInstance().getPosition().x;

if (xdelta > ydelta) // horiz distance is more        
   Tail = vertVec * monster.speedPerSec * TimePerFrame.asSeconds();
else
   Tail = horizVec * monster.speedPerSec * TimePerFrame.asSeconds();

You would probably want to handle the case where the Tail distance for the current frame over shoots the player, and clamp at that.

radical
  • 262
  • 3
  • 9