Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Moving View  (Read 2080 times)

0 Members and 2 Guests are viewing this topic.

catalinnic

  • Newbie
  • *
  • Posts: 19
    • View Profile
Moving View
« on: July 24, 2019, 10:42:19 am »
Hi I try to move view that changes his position to be indentical (in some instances) with position of the player but in some key specific cases to lock the movement for some directons
void Ai::Move_view()
{
    if(Player1.Rect1.getPosition().y > Map_view.getCenter().y && Map_view.getCenter().y + 360 < Map_margin_y)
        Map_view.move(0, Player1.Movement_speed);
    else if(Player1.Rect1.getPosition().y < Map_view.getCenter().y && Map_view.getCenter().y - 360 > 0)
        Map_view.move(0, -Player1.Movement_speed);
    else if(Player1.Rect1.getPosition().x > Map_view.getCenter().x && Map_view.getCenter().x + 640 < Map_margin_x)
        Map_view.move(Player1.Movement_speed, 0);
    else if(Player1.Rect1.getPosition().x < Map_view.getCenter().x && Map_view.getCenter().x - 640 > 0)
        Map_view.move(-Player1.Movement_speed, 0);
}
 

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Moving View
« Reply #1 on: July 24, 2019, 11:35:50 am »
Hi

Your post doesn't describe any particular problem. Please give more details about what's wrong, what you expect, what you already tried, etc.
Laurent Gomila - SFML developer

catalinnic

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Moving View
« Reply #2 on: July 24, 2019, 12:55:45 pm »
The problem:
When I change the "Movement_speed" value to some float like 4.75 the function bounces the view up and down or left to right.
« Last Edit: July 24, 2019, 03:19:05 pm by catalinnic »

txes

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Moving View
« Reply #3 on: July 24, 2019, 09:35:06 pm »
It happens because you are moving the view further than the current distance to the player. You should check if the actual distance is smaller than the views movement speed.

catalinnic

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Moving View
« Reply #4 on: July 24, 2019, 09:52:18 pm »
But I don't know why it acts normally when the value is 4, 5, 6, etc.

txes

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Moving View
« Reply #5 on: July 24, 2019, 10:14:52 pm »
Imagine the distance between the player and the view is 3 and the view movement speed is 4. In the first call, the code will subtract 4 to the view position so its distance to the player will be 3 - 4 = -1. Next call, the code will add 4 to the view position so its distance to the player will be -1 + 4 = 3, back to its original position in an endless loop. So, in reference to the code you posted, it always happens. It may look it does not, but it does. 
 

 

anything