Sign in to follow this  
kingpinzs

Help with collision testing

Recommended Posts

here is what I have while( bugy <= mapy ) { bugy += 4; bugx += 4; cout << bugy << "\n"; if (bugx <= mapx) { cout <<bugx<<"\n"; } } now when it does = mapy or mapx I want it to go the other direction tell it = 0 for x and 0 for y then change direction again. How would I accomplish that?

Share this post


Link to post
Share on other sites
Here's a simple solution

[source lang='cpp']

float speedX, speedY;
float x, y;
float mapX, mapY; // assume these two variables are right and bottom corners of the map

x = 100; // some value
y = 100;
speedX = 1;
speedY = 1;
mapX = 640; // size of map
mapY = 480;

while(1)
{
x += speedX;
y += speedY;

if(x < 0 || x > mapX) speedX *= -1;
if(y < 0 || y > mapY) speedY *= -1;
}


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this