Search code examples
cocos2d-iphonegame-physics

What collision detection method to use with hand drawn surface?


I have a lunar lander type game. I don't use any physics engines. My lander keeps falling if you do not use thruster and eventually lands on the ground. Ground is hand drawn, it is not a line, more like curve, and the land can be of any configuration or color. How do I properly use collision detection and its results?


Solution

  • Well it depends on what you want to do. I would recommend one of the following:

    1. Use physics engines. They are there for something. You could create different shapes what was drawn. You could mix in a rectangle if theres a straight line, or a lot of circles for curves, etc.

    2. Use your own custom circle collision detector. You represent the lander with a bounding box sized circle. Then, for each of the handdrawn lines, create a bunch of adjacent circles representing the line. When you check your lander position, you are just basically looping through the circles representing the lines and checking for collisions. Incoming pseudocode

      for (CollisionCircle* circle in collisions)
      {
          if (circle.collidesWith(lander.collisionCircle))
          {
              // 1. Calculate edge distance from lander to circle (position + radius distance)
              // 2. Remove distance from lander position to fix position.
          }
      }