-1

This may be an uneducated question - or an odd one of the kind.

My question is why this code doesn't work:

            if (up == true)
        {
            SDL_Delay(pause_t);
            player.setY(player.velocity - player.getY());
        }
        if (left == true)
        {
            SDL_Delay(pause_t);
            player.setX(player.velocity - player.getX());
        }

However, this code does work:

            if (up == true)
        {
            SDL_Delay(pause_t);
            player.setY(player.getY() - player.velocity);
        }
        if (left == true)
        {
            SDL_Delay(pause_t);
            player.setX(player.getX() - player.velocity);
        }

The difference between the two codes is that in the first example, I am subtracting a part firstly (the velocity of the object) before calling the position function. What happens when I run this code is that it doesn't move 1 on the axis, but rather 10. The size of the Y axis is above 10, so I am not actually subtracting its own coordinates.

However, in the second piece of code, I am subtracting the velocity in the end, after having called my position function. What happens with the second piece of code is that the object moves one pixel (velocity's integer value).

To those who should wonder what I am trying to do, these are controls for my openGL game. It's for moving an object.

And for the record, this is not an important question. My code does work. I am simply curious why it works the way it does.

Tim
  • 35,413
  • 11
  • 95
  • 121
  • 1
    I'm not sure I understand. Are you asking why `a - b` gives a different result than `b - a`? – Tim Oct 01 '12 at 19:40
  • Yes, that would be it. I realize that in normal arithmetic, this would be obvious why. However, the processor calls the function BEFORE it attempts to do the arithmetic, correct? This would be why I asked my question. – user1707244 Oct 01 '12 at 19:51

1 Answers1

1

in the first example, I am subtracting a part firstly (the velocity of the object) before calling the position function.

No, in the first version, you're subtracting the old location, not subtracting the velocity.

The code for what you describe is:

player.setY((-player.velocity) + player.getY());

and that will work.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • Isn't subtraction done -> that way, as an oppose to the opposite way which you seem to suggest? You always subtract the first number from the second number. – user1707244 Oct 01 '12 at 19:44
  • @user1707244 you know 4 - 2 is 2, right? Not -2? – harold Oct 01 '12 at 19:46
  • I may have gone to the wrong school - or this may be how we do it in Europa.. Or maybe I am completely off. When I were in elementary school, we were taught that 4 - 2 is -2... As explained in the comment above, it goes this way ->. As in, 4 subtracted from 2 - not vice versa. – user1707244 Oct 01 '12 at 19:49
  • 1
    @user1707244: Even if it were done that way, which it isn't in C or C++, ever, your first and second code snippets would still be opposite of each other. Also I know enough people from Europe to know that it's not a continent-wide notation, rather you are wrong. – Ben Voigt Oct 01 '12 at 19:52
  • In almost all programming languages it is left to right, therefore 2, not -2 is the result of (4 - 2) – Stephan van den Heuvel Oct 01 '12 at 19:52
  • Please take notice of the word "may" used in conjunction with my sentence. I did not state that it is how we do it here. However, coming to think of it. It IS how we do it in my country. 4 - 2 being equivalent to 2 is completely absurd! I did not realize this, Stephan. Thank you for informing me. – user1707244 Oct 01 '12 at 19:54
  • 1
    @user1707244 what country is that, if I may ask? In the Netherlands at least, 4 - 2 is positive 2 – harold Oct 01 '12 at 19:55
  • @user1707244: You are perhaps reading `4 - 2` incorrectly. It is not *4 subtracted from 2* but *Starting with 4 take away 2*. – Ben Voigt Oct 01 '12 at 19:56
  • That would be in Scandinavia. Norway, Denmark, Finland, Sweden. It's all the same. – user1707244 Oct 01 '12 at 19:56
  • @user1707244 so is this wrong? http://da.wikipedia.org/wiki/Subtraktion – harold Oct 01 '12 at 19:58
  • Harold. I have not once said that it is wrong. I said it would be absurd - relative to where I have been brought up. If it is on Wikipedia, and if so many seem to disagree with me, then it means I am wrong. You can not escape this irrefutable use of reasoning. – user1707244 Oct 01 '12 at 20:00