Public Group

# Wraparound

This topic is 5019 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Ok, bear with me while i try to explain this :) I have and object on the screen that you control with the keys and i am trying to make it so if you hit the edge of the screen horizontally(x), you will end up on the left side of the screen, and vice versa.
// Update the ship's position
_iShipX = min(500 - _pSaucer->GetWidth(), max(0, _iShipX + _iSpeedX));
_iShipY = min(330, max(0, _iShipY + _iSpeedY));


This is the code being run every time windows repaints the screen, which is set at 30 fps currently. The dimensions of the program are 500x400. Can any help me with this? Thanks, Loudboy/John

##### Share on other sites
Can you explain what you're doing with the code you posted a little better? Why all the calls to min and max?

If you have some ship with an X and Y coordinate, and some speed coordinates, and every iteration you want to add those speeds to the coordinates, you could just do some bounds checking:

shipX += speedX;shipY += speedY;if(shipX < 0) //out of bounds on the left{  shipX += 500;}else if(shipX >= 500) //out of bounds on the right{  shipX -= 500;}if(shipY < 0){  shipY += 400;}else if(shipY >= 400){  shipY -= 400;}

Of course if you want to make it easier to change the size of your screen later, you'll use a constant for your screen size:

const int SCREEN_WIDTH = 500;const int SCREEN_HEIGHT = 400;shipX += speedX;shipY += speedY;if(shipX < 0) //out of bounds on the left{  shipX += SCREEN_WIDTH;}else if(shipX >= SCREEN_WIDTH) //out of bounds on the right{  shipX -= SCREEN_WIDTH;}if(shipY < 0){  shipY += SCREEN_HEIGHT;}else if(shipY > SCREEN_HEIGHT){  shipY -= SCREEN_HEIGHT;}

Hope that helps.

##### Share on other sites
thank you :) That worked great :D

this is an example from the book and they overcomplicate stuff sometimes which is kind of bothersome in a beginning book

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 9
• 31
• 16
• 11
• 10
• ### Forum Statistics

• Total Topics
634121
• Total Posts
3015613
×