Robert M said:
Its not clear to me why you are concerned that -128 -> $80 transforms into 128 -> $80. The fact is that 128 and -128 are representes by the same combination of bits. The meaning of those bits 128 or -128 is completely up to you as the programmer. Since you want all negative values to be made positive, isn't it correct then for -128 to become 128?
Regards,
Well, it is because I am using signed integers to represent velocity, so if I just left it as +128 to do my calculations, at some point I would have to transform that to +127 before I use it as velocity, or the velocity would be in the wrong direction.
Here's what I am doing:
Two sprites bump into each other, each with an X and Y velocity (signed integers): call them VX1, VY1, VX2, VY2
I want to take half the magnitude of the X velocities: VXsum = abs(VX1) / 2 + abs(VX2) / 2
and then, for the sprite who is leftmost, give him negative VXsum and give the other sprite positive VXsum.
Then I do something similar for the Y velocities.
So...if VX1 = -128 and VX2 = -128 (both sprites going full speed to the left, and suffering a glancing collision), then what I want VXsum to be is 127 - the largest positive signed integer. If my abs() function returns abs(-128)=127, then I'm fine. If it returns +128, then I get: $40 + $40 = $80 = -128
So if I then applied that velocity willy-nilly, then my two sprites would end up with velocity in the wrong direction! So I would need to check when I sum the velocities to see if the addition overflowed and, if so, reset to the maximum positive velocity. Which I could do, but it just seemed cleaner to turn -128 into 127 at the beginning which eliminates that problem completely!
But if there is a better way, I'm willing to be persuaded :)