views:

342

answers:

3

I want to make a 2D game in C++ using the Irrlicht engine. In this game, you will control a tiny ship in a cave of some sort. This cave will be created automatically (the game will have random levels) and will look like this:

Cave

Suppose I already have the the points of the polygon of the inside of the cave (the white part). How should I render this shape on the screen and use it for collision detection? From what I've read around different sites, I should use a triangulation algorithm to make meshes of the walls of the cave (the black part) using the polygon of the inside of the cave (the white part). Then, I can also use these meshes for collision detection. Is this really the best way to do it? Do you know if Irrlicht has some built-in functions that can help me achieve this?

Any advice will be apreciated.

+3  A: 

Describing how to get an arbitrary polygonal shape to render using a given 3D engine is quite a lengthy process. Suffice to say that pretty much all 3D rendering is done in terms of triangles, and if you didn't use a tool to generate a model that is already composed of triangles, you'll need to generate triangles from whatever data you have there. Triangulating either the black space or the white space is probably the best way to do it, yes. Then you can build up a mesh or vertex list from that, and render those triangles that way. The triangles in the list then also double up for collision detection purposes.

I doubt Irrlicht has anything for triangulation as it's quite specific to your game design and not a general approach most people would take. (Typically they would have a tool which permits generation of the game geometry and the navigation geometry side by side.) It looks like it might be quite tricky given the shapes you have there.

Kylotan
Thanks for you answer. I found some libraries that should do the job: GPC (http://www.cs.man.ac.uk/~toby/alan/software/) and Triangle (http://www.cs.cmu.edu/~quake/triangle.html). I'll make a post if I succeed. :)
bilygates
Just in case you want to know, I've managed to use GPC and poly2tri (http://code.google.com/p/poly2tri/) to generate a polygon and then triangulate it. Here's a pic: http://imgur.com/154kd.png . Cheers!
bilygates
Cool, great to hear that you've made progress.
Kylotan
A: 

One option is to use the map (image mask) directly to test for collision.

For example,

if map_points[sprite.x sprite.y] is black then
   collision detected

assuming that your objects are images and they aren't real polygons.

In case you use real polygons you can have a "points sample" for every object shape,
and check the sample for collisions.

Nick D
A: 

To check whether a point is inside or outside your polygon, you can simply count crossings. You know (0,0) is outside your polygon. Now draw a line from there to your test point (X,Y). If this line crosses an odd number of polygon edges (e.g. 1), it's inside the polygon . If the line crosses an even number of edges (e.g. 0 or 2), the point (X,Y) is outside the polygon. It's useful to run this algorithm on paper once to convince yourself.

MSalters