Characters walking from one of the screen to the other dilemma ( Modular arithmetic )

Started by
4 comments, last by bluemace 22 years, 3 months ago
Here is a piece of code that I don''t quite understand. Can someone explain how this works? I see the character walking from one end of the screen and pops up on the other end, yet when I look at the code I just get a bit confused. I''m confused because the initial value for "dwCaveManPosition" is 400. I follow the code when you make the character walk left, but I don''t understand what is going on when the character walks to the right due to the value being used! The code is on the CD of Ernest Pazera''s "Isometric Game Programming with DirectX 7.0". Also, what is the segment of update animation frame really doing? The math just doesn''t click with me. if(MoveLeft) { //moving left dwCaveManFace=1; //update position dwCaveManPosition+=796; dwCaveManPosition%=800; //update animation frame dwCaveManFrame+=1; dwCaveManFrame%=7; } else { //moving right dwCaveManFace=0; //update position dwCaveManPosition+=4; dwCaveManPosition%=800; //update animation frame dwCaveManFrame+=1; dwCaveManFrame%=7; } ~Robert
Advertisement
Doh! I forgot. The dimensions of the canvas is 800 x 600. I think the character starts at 400,300. The character only moves left and right.

~Robert
The reason why the caveman appears on the other side of the screen is the modulus operator (dwCaveManPosition%=800 that happens after the addition (dwCaveManPosition+=796.

The ''%= '' operator is taking the remainder of dwCaveManPosition divided by 800 and storing it in dwCaveManPosition (there may be a more technically correct explaination, but that is the effect of the line).

So, putting the two lines together, you have

dwCaveManPosition = 400
dwCaveManPosition += 796 (-> result is 1196)
dwCaveManPosition %= 800 (-> result is 396)

Repeating the action will eventually make dwCaveManPosition 0, which will result in the following:

dwCaveManPosition += 796 (-> result is 796)
dwCaveManPosition %= 800 (-> result is 796)

and the cave man pops up at position 796 on the other end of the screen.

Adding 4 will wrap him in the opposite direction, because any value greater than 799 will be shifted to the left of the screen.

Using the mod operator avoids having to use a conditional statements like "if(dwCaveManPosition > 800) dwCaveManPosition -= 800;" and "if(dwCaveManPosition < 0) dwCaveManPosition += 800;"

To be honest, I''m not sure which way is more efficient, but the way it''s written avoids a jump statement in your compiled code; I think that might lead to better prediction by modern processors (just my guess why it was done that way--the cost either way is probably trivial for an example problem).

XP-Cagey
Oops, in answering your first question, I didn''t see the second--as you have probably guessed from my first response above, the animation math sets up a loop that counts from 0 to 7 over and over again:

0 + 1 = 1
1 % 8 = 1

1 + 1 = 2
2 % 8 = 2

...

6 + 1 = 7
7 % 8 = 7

7 + 1 = 8
8 % 8 = 0

(repeats)

XP-Cagey
Thanks XP-Cagey. I feel a bit dumb, but this did not seem straightforward to me when I looked at it. I have a good question. Are there any math books that just concentrates on game programming? I have a funny feeling I''m gonna be running into all sorts of odd mathematics the more I continue to study game programming...

~Robert
If you want a good look at the sorts of odd math you''ll be getting into take a peek over here

This topic is closed to new replies.

Advertisement