views:

148

answers:

3

I'd like to make a game where the terrain is not even and is based on a png. How s this done in theory, given the object's vec2 and its angle, because if for instance there is a hill, the character will rotate based on the angle of the hill. Thanks

2d like mario

A: 

You need a mapping function that converts the PNG data to a 3-d representation... This mapping function can be simple, as in simply interpreting greyscale values in the PNG as altitude or via human guidance, or it can be complex, as in shadow detection used in advanced computer vision algorithms. In either case, you would then move your character based on the data gleaned thru the mapping function.

Michael Bray
A: 

In c++, I would suggest looking for a 3d-gaming engine that support more than just plain terrains.
You could start with this list

That is of course, if you're not trying to start from scratch, in which case you need to look for algorithms first.

Edit: since it's the game and not the terrain that is 2d, if you want to make your environment out of an image, you're in for quite some edge-detection.

stefaanv
+2  A: 

I think you are talking about a heightmap which is you PNG which is then converted to a 3D triangle mesh. You need to use the information from the mesh (or PNG color value) to calculate the current height where you should place your character.

If this is a flying character your pretty much done here, but in your case you need to calculate the normal vector of the current triangle the character is standing on. This is pretty simple using the cross product of the two triangle vectors (V2 - V1) x (V3 - V1). That should be your characters angle as well. You could maybe average this vector by including normals from the surrounding trangles as well.

Btw, when you have the normals of the triangle you can apply some basic shading to the ground as well.


Added: The OP changed the question to be a 2D problem. The above approach still works, but it much easier in 2D.

Use the height values not as triangles but as lines (silhouette) and calculate the normal of the current line instead. That is, create a vector, v, between the current height value and the next. Then the normal of that vector is n = <-v.y, v.x>. Use that as the angle of your character.

Martin Wickman