Jump to content
  • Advertisement
Sign in to follow this  
Loudboy

Wraparound

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!