'Move an object with a given speed
Here's an example of an if-statement where depending on what keystroke is pressed it will move to a certain position. I have also googled the "move" command in SFML and it takes two arguments.
Move the object by a given offset.
This function adds to the current position of the object, unlike setPosition which overwrites it. Thus, it is equivalent to the following code:
object.setPosition(object.getPosition() + offset);
Here is the code:
if ( key.code == Keyboard::Key::W )
{
sprite.move (0, -speed);
}
else if ( key.code == Keyboard::Key::S )
{
sprite.move (0, speed);
}
else if ( key.code == Keyboard::Key::A )
{
sprite.move (-speed, 0);
}
else if ( key.code == Keyboard::Key::D )
{
sprite.move (speed, 0);
}
However I dont understand the arguments. speed is defined as a float with the value 4.0
.What does sprite.move(0,-speed) mean. I understand we start at 0 but how do we move up if speed is negative? Shoudn't be move down if its negative and up if its positive? Same goes for A and D. I cant draw a picture in my brain where these arguments make sense. Can someone please througly explain?
Solution 1:[1]
In SFML point (0,0) is in the left up corner, and by moving to the right you are increasing x-axis value, and by moving down you are increasing y-asix value. If there is window with 600 width and 400 heigth, then left up corner will be (0,0), right up corner (600,0), left down corner (0, 400), and right down corner (600,400). You can see that left down and right down corner has positive y value, so when you are moving by negative value, you are moving up. You have sprite named "sprite" in the position (300,350) and by calling sprite.move(100,-100) you are "adding" values from move function to your curren position, so you are moving from (300,350) to ( 300 + 100 , 350 + (-100) ), so sprite will change position to (400,250) and 250 i closer to the point(0,0) in y-axis which is on the left up corner so you are moving up, the same logic applies to moving by positive value in y-axis. You call sprite.move(0,100) and sprite is on point (200,200), so sprite will move to point (200 + 0 , 200 + 100) so it will be point (200,300), and (200,300) is further from point (0,0) than (200,200) so it will move down. The same situation is with moving left and right. sprite is on position (200,200), you call sprite.move(100,0), so sprite will move to point (200 + 100, 200 + 0) = (300,200), and point (300,200) is further from point (0,0) so it will move to the right. And moving to the left, sprite is on position (200,200) and you call sprite.move(-100,0), sprite will move to the point (200 + (-100) , 200 + 0) = (100,200). Point (100,200) is closer to the point (0,0) than point (200,200) so it will move to the left. As you can see sprite.move(100,100) adds to the sprite position 100 to the x-axis, and 100 to the y-axis and change to the new position.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | ClockZzz |