3
\$\begingroup\$

Me and my friends are creating a simple 3D game in LibGDX and are currently trying to implement simple collision detection with the terrain (ground). For this I thought the best way would be to simply get the y-value (height) of the piece of terrain the player is currently standing on, and limit the player's y-position to that value.

For this I need some way to get the 3 vertices that make up the triangle the player is standing on and then use some type of interpolation (probably barycentric) to get the correct height. The problem is that LibGDX's Model, ModelInstance and ModelData classes only contains a raw vertex array(s) of floats with no built in way to acquire the information we need. So my question is fairly straight forward:

Is there a way to acquire the three vertices that we need to perform this interpolation or Is there another way to acquire the height of the terrain given the coordinates (x, z)?


PS. I know using a heightmap instead of a model could solve this problem, but we'd much rather use LibGDX's built-in systems unless absolutely necessary.

\$\endgroup\$
  • 1
    \$\begingroup\$ If you can assume the terrain is just a 2D grid, you can search every triangle for the three vertices that enclose the position of your player disregarding the y coordinate then do the interpolation. Of course this is super slow but if you chuck up all the geometry into a quadtree it should be much faster to find. It gets much more complex if you're not using a physics engine and you can't chunk up the geometry. \$\endgroup\$ – Coburn Aug 29 '16 at 23:37
  • \$\begingroup\$ We discussed using Bullet as our physics engine, but since we don't need collision detection with anything other than our terrain we find it unnecessary to implement it into our game logic. \$\endgroup\$ – Charanor Aug 29 '16 at 23:44
  • \$\begingroup\$ I can write an answer based on the above comment but I'm not super familiar with LibGDX. Will give it a look in a couple hours/a day if no one else write an answer \$\endgroup\$ – Coburn Aug 30 '16 at 13:48
  • \$\begingroup\$ I would dynamically generate a heigh map (interpolation on regular grid of a delaunay triangulation) from the terrain just once and then perform collision detection always on this height map. Should be very fast. \$\endgroup\$ – Trilarion Aug 30 '16 at 14:34
  • \$\begingroup\$ How do you create a terrain mesh? Is it based on some image or just a regular triangle mesh? Your options depend on this. \$\endgroup\$ – kolenda Sep 6 '16 at 15:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.