Search code examples
javaopengl3dlwjgl

lwjgl rotate object according to the slope of the ground


Good evening, I have a problem with the LWJGL library, my idea is to make a procedural ground where a vehicle can move, I've already built both ground and vehicle management to make it stick to the ground each time it moves, To do this, the height of the ground is calculated according to the new position and the object is placed at the altitude just calculated but what I can not do is to rotate the vehicle according to the slope of the ground. I have already tried to calculate the altitudes at the edge of the vehicle and use them to get the rotation angles (x axis, z) but I managed to run it only for one direction but if the object rotates around the axis y it will not work more. I tried to use quaternions but they also failed.

Here is the class that realizes the terrain https://github.com/maurizioterreni/OpenGL/blob/master/src/com/unifi/ing/engine/terrains/Terrain.java

While here the class that manages the vehicle https://github.com/maurizioterreni/OpenGL/blob/master/src/com/unifi/ing/engine/entity/Rover.java


Solution

  • The up vector of your object should be in line with the normal vector of the terrain. By doing this, the game object/entity will then be orthogonal/perpendicular to the terrain.