I'm new to the site and decided to make an account because I'm having problems grasping the concept of collision in a 2D environment.
What I have is a set of road sprites from a top-down perspective, and I'm trying to set up rules for collision with them so that if the player goes off the road, he'll get a game over.
So far I have rules set in place for a straight road, which you can see here:
if ((*it)->getType() == ST_RoadStraight)
{
Road* road = (Road*)*it;
// Check to see if the road sprite is within 16 pixels of the bucket either way
int dx = road->getPosition().x - Pos.x;
if (dx < 0) dx = -dx;
if (dx > 200)
{
outOfBounds = true;
}
}
Basically, I say that if the player goes a certain distance either way on the x axis that it'll end the game. That's fine for a straight road that goes up and down or left and right, since I can change i to the y axis, but I also have curved roads. Here's an idea of what the assets are like:
Straight: https://i.stack.imgur.com/mThJH.png
Curved: https://i.stack.imgur.com/Nr0EI.png
Basically, I need the player to be able to go down the road or across where it allows, but if he hit the edges where he would go off road, I need to say so. This is easy with the straight road, but depending on where the player is on the curved road, being at, say 30 pixels high on the Y might be okay at a certain part of the road, but not okay at the another.
Essentially, I'm looking for some sort of equation that would allow this to be accurately represented for my game.
Any help would be appreciated.