How to adjust rotation of Character to slope of terrain in OpenGL with Bullet library

414 views Asked by At

I am currently working on a small game using OpenGL (Windows, Visual Studio). For physics I use the BulletLibrary.

The Setup in OpenGL:

  • floor: A "hilly" terrain model (in Bullet btBvhTriangleMeshShape)
  • player: A model of a small animal (btCollisionSphere in Bullet)

Currently the controls work as follows:

// method lies in the Player class
if (glfwGetKey(window, 'W')) {

        // btRigidBody
        playerBody->setLinearVelocity(btVector3(dirWorld.x, -1, dirWorld.z) * timeDelta * moveSpeed);
        btTransform trans; 
        playerBody->getMotionState()->getWorldTransform(trans);//->getWorldTransform(trans);

        // sets the location in the current model matrix
        setLocation(glm::vec3(trans.getOrigin().getX(), trans.getOrigin().getY(), trans.getOrigin().getZ()));
}

where dirWorld is a normalized direction vector, pointing from my player model forward (looking direction). The -1 for the y-Coordinate is necessary so when moving down the player sticks to the terrain and does not float down.

What I would like now, is that the Model would rotate according to the slope of the terrain: - even -> stay horizontal - up -> rotate so head is higher than tail - down -> rotate so head is lower than tail

My idea would be to take the Rotation of the CollisionShape and "transfer" it to the actual Model.

What my question(s) is(are) now:

Can I achieve this with a CollisionSphere, or would I then have a rolling model as well?

Or should I use a completely different CollisionShape for my character?

Or is there another way to do this altogether?

0

There are 0 answers