Search code examples
c++opengl-3bulletphysics

Rotation and Movement with rigid body in Bullet Physics


I have made a rigid body for the player and have been trying to get the rigid body moving along with the player's controls.

What I mean is that whenever I press forward I want the rigid body to move forward in the direction the player is facing, same with back, left, right. So far I'm able to use apply force to move the rigid body in static directions.

My straight question is how do I move the player's rigid body in the direction the player is facing.

Other Details:

  • I don't really want to use kinematic bodies if not necessary mostly because their very fiddly at the moment

  • I'm using glfw3 for input


Solution

  • This is quite amazing that you would not see how to do that after you actually managed to apply forces in static directions to something you configured over bullet.

    Come on, you HAVE the skill to figure it out.

    Here, just a push in the direction (hehe), hem. Just take the vector of the facing direction (which could be determined by camera, 1st or 3rd view, or even something else...).
    Congrats, this vector is your force by a k factor.

    You should also modulate this force according to speed, you don't need to accelerate to infinite speed, just accelerate lots at first and then regulate force to tend to desired walk speed.

    Then, the side directions are obtained by rotating the facing vector by 90 degrees around the standing axis (most surely the vertical). You can obtain that by simply swapping components and multiplying by -1 one of them. x,y,z becomes y,-x,z

    To go backward, its just -x, -y, -z on the facing vector.

    So your up key is not bound to 0,1,0 but to facing_dir actually. This facing dir can change with mouse or some other view controls, like numeric keys 2,6,8,4 for example. Or you could drop up,left,right,down for movement and use w,a,s,d like everybody else, and use direction keys to rotate facing direction. (+mouse)

    It is much more difficult to obtain the facing vector from mouse movement or direction keys than finding out how to apply the force, so if you already have the facing vector I'm puzzled that you even have a problem.