Making my world wrap
I''m writing a 2D side scroller, and its got stars and evil bugs flying around in the world. When the spaceship flies to an edge f the world, I want it to seamlessly wrap around.
I''ve looked at TotWGPG''s Outpost code, and I see how the stars work for wrap-around, but the stuff like asteroids and junk don''t wrap. It doesn''t look too seamless.
How do I do I make it wrap seamlessly?
Try using modulo in the point placing code...
For instance:
x = 640
y = 0
putPixel( x, y )
would place the pixel outside the screen @ 640x480 res...
but:
putPixel( x%XRES, y%YRES )
(NOTE: % is the modulo sign in c++)
would place the pixel @ (640%640 = 0, 0%480 = 0)...
but:
x = -1
y = 0
putPixel( x%XRES, y%YRES )
would place @ ( -1, 0 )
so:
putPixel( (x+XRES)%XRES, (y+YRES)%YRES )
would make:
( (-1 + 640)%640 = 639%640 = 639, 0 )
So:
putPixel( (x+XRES)%XRES, (y+YRES)%YRES )
For instance:
x = 640
y = 0
putPixel( x, y )
would place the pixel outside the screen @ 640x480 res...
but:
putPixel( x%XRES, y%YRES )
(NOTE: % is the modulo sign in c++)
would place the pixel @ (640%640 = 0, 0%480 = 0)...
but:
x = -1
y = 0
putPixel( x%XRES, y%YRES )
would place @ ( -1, 0 )
so:
putPixel( (x+XRES)%XRES, (y+YRES)%YRES )
would make:
( (-1 + 640)%640 = 639%640 = 639, 0 )
So:
putPixel( (x+XRES)%XRES, (y+YRES)%YRES )
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement