开发者

Terrain and Collision in a Game

开发者 https://www.devze.com 2023-02-25 04:19 出处:网络
I am attempting to create a new Game in which a vehicle drives along the ground, but also has the capability to use thrusters to become temporarily airborne.

I am attempting to create a new Game in which a vehicle drives along the ground, but also has the capability to use thrusters to become temporarily airborne.

I am relatively new to game programming, but I am rather good at coding in Java, C, C++ etc., so I do not think that should be an issue (thought the f开发者_JAVA技巧irst iteration of the game is in Java). My problem is that I am starting from scratch, and can not think of a way for the terrain to be a barrier so the vehicles do not fall right through it. And how can I have them possibly take fall damage?

In the past people have directed me to the Replica Island source since it has this already in it, and I see the whole Collision System, but so far I have not been able to make sense of it and I was hoping somebody would be willing to lend me a hand.

Thank you

-Roflha


What are you using to develop the game? An engine or a graphics library? There are basically three levels (as I like to think about them): graphics libraries, graphics engines, and game engines.

  • Graphics libraries are pretty much as low level as you can get: OpenGL or DirectX. When you want to draw out your terrain, you will be drawing as a mesh of triangles, and defining the vertices of those triangles, etc.

  • Graphics engines are one layer on top of that. Examples of those (for C++ at least) are Ogre3D, Horde3D, and Irrlicht. They will take care of a lot of the lower level commands, and let you focus on graphics from more of a higher level standpoint.

  • Game engines will pretty much take care of anything graphics related. Examples of these (again C++) are Unity and UDK. They can generally do a lot of the game programming for you. You would probably draw your terrain in a modeling program (3dsmax, etc), export it to some format, then the engine will slurp it up and know exactly how to handle it.

I have played with all three, and find the graphics libraries most interesting. However, they'll probably take the longest to make a game. They also involve a lot more math (linear algebra).

As far as collision detection with a terrain. You have to know about the triangles that compose that terrain. If you are using a game engine, you should be able to store the mesh for your terrain and the mesh for your character in some sort of "Scene Node", and it will handle collision detection automagically. However, if you are dealing with everything yourself, there are many popular algorithms to do this:

  • Bounding Box: even though your character is shaped like a person (or an animal, etc), you treat them as a box and check when that box intersects with the triangles of the mesh that makes up your terrain.
  • Bounding Sphere: Treat your character as a sphere because spheres are much easier to see if they have collided. You just check the "radius of the sphere" with the "distance from the center of the sphere to the other mesh". Then, if that sphere is colliding, you break it into smaller spheres that more accurately represent your character. You recurse from there 3 or 4 iterations, or as accurately as you'd like to represent your character. Remember though, everything in games needs to be fast fast fast, so that's why you limit recursions.

An excellent article on bounding sphere collision detection

I've written a sample OpenGL program that basically lets a character, which is represented by a bounding box, walk around a terrain, which requires collision detection. Let me know if you're more interested in how it works.

You can handle the fall damage several ways:

  1. If the character isn't flying, you can use a simple state machine. Once they enter the air, change the state to "IN AIR", and while the game look is running, set a timer and keep track of how long they are in the air. If they are in the air for too long, then calculate different amounts of damage based on how long they are there for.
  2. A more accurate way is to keep track of the character's y velocity. This is accurate to real life. If you are falling slowly, it doesn't make sense to have fall damage. If you are falling quickly, you will get hurt more the faster you are going. So come up with some sort of equation that relates falling_speed to damage. Then, once they hit the ground, change the state machine back to "ON_GROUND" and check the fall_speed (probably y_velocity).


You might also want to try Nvidia PhysX, aside from that which its name implies (you wanting to model car physics is what made me think of it), there is also a lot of functionality exposed by certain utilities, such as collision detection, quaternion math etc.

I'm using it with Ogre, an open source graphics engine (which so far ;-)... makes switching between OpenGL-DirectX, and Linux-Microsoft relatively easy) which also, has a lot under the hood.

Since you already know how to write code I would suggest the above as viable, not alternatives but open ended sources; so that you only reinvent the parts of the wheel you want.

0

精彩评论

暂无评论...
验证码 换一张
取 消