Laifsjo
Laifsjo

Reputation: 21

How do i make 3D collision detection with my terrain and my character? LWJGL

I have a problem with collision detection 3D. I made a heightmap as a .bmp image, and im loading it in the game. Then i have a .obj model as my player. But how would i check for collision with the player and the terrain? I searched alot on google, but i couldn't find it. I know i have to find the normals and stuff, but i'm not a really good programmer, so i have no idea how to find the normals, and if i have them, how to use them. Could someone explain this with code? I'm using opengl to draw the terrain.

Upvotes: 2

Views: 1875

Answers (1)

Chechulin
Chechulin

Reputation: 2496

I'd recommend you to use an existing game engine for the physics. It is because writing a collision detection (and collision resolving) may be very tricky. Especially in 3D. You also need to know trigonometry very well.

If you don't want to use third party products and want to write your own engine, you'll have your collision detection look like this:

Contact contact = Engine.getFirstHit(Terrain.getNearestPolygons(player), player);
if (contact.isHit) {
    player.move(player.getVelocity().multiply(contact.getMoveAmount()));
    contact.resolve();
} else {
    player.move(player.getVelocity());
}

where Engine.getFirstHit() and contact.resolve() will do all the work. You can check references and "see also" section on the Collision Detection wiki page.

Upvotes: 1

Related Questions