0

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.

2 Answers2

0

Is there any reason you cannot use Sprite Kit?

WolfLink
  • 3,308
  • 2
  • 26
  • 44
  • I'm currently writing this game using the Marmalade SDK to make it available for both iPhone and Android devices. I feel like this is for xcode/objective-c programming. However, a kit that provides the kind of collision I'm looking for would be appreciated and if this would still be of use to me, I may look into it. – user2829016 Sep 29 '13 at 18:50
  • I feel I should also mention this in Visual Studio on Windows. – user2829016 Sep 29 '13 at 18:58
  • SpriteKit is a port of Box2D so if you can find another port of Box2D that works with marmalade then that should be just as good as sprite kit. Cocos 2D is another port of Box2D but I think that is also iOS only. You can visit the Box2D website here: http://box2d.org – WolfLink Sep 29 '13 at 19:48
  • Otherwise, you can do it yourself with some basic geometry. Use the pythagorean theorem to check the edges of the semi circle parts and addition/subtraction to check straight parts. – WolfLink Sep 29 '13 at 19:53
0

Well For two dimensional collision detection, I'd recommend you to use Box2D physics engine. It's easy to setup in Marmalade and gives accurate result. I've used it in many of my marmalade games. There's a box2D extension in marmalade's github account. Have a look at it.

0xC0DED00D
  • 19,522
  • 20
  • 117
  • 184