How do you control a player character in Bullet Physics?

痞子三分冷 提交于 2019-12-24 00:58:26

问题


I am not sure how you are supposed to control a player character in Bullet. The methods that I read were to use the provided btKinematicCharacterController. I also saw methods that use btDynamicCharacterController from the demos. However, in the manual it is stated that kinematic controller has several outstanding issues. Is this still the preferred path? If so, are there any tutorials or documentations for this? All I found are snippets of code from the demo, and the usage of controllers with Ogre, which I do not use.

If this is not the path that should be tread, then someone point me to the correct solution. I am new to bullet and would like a straightforward, easy solution. What I currently have is hacked together bits of a btKinematicCharacterController.

This is the code I used to set up the controller:

playerShape = new btCapsuleShape(0.25, 1);
ghostObject= new btPairCachingGhostObject();
ghostObject->setWorldTransform(btTransform(btQuaternion(0,0,0,1),btVector3(0,20,0)));
physics.getWorld()->getPairCache()->setInternalGhostPairCallback(new btGhostPairCallback());
ghostObject->setCollisionShape(playerShape);
ghostObject->setCollisionFlags(btCollisionObject::CF_CHARACTER_OBJECT);
controller = new btKinematicCharacterController(ghostObject,playerShape,0.5);
physics.getWorld()->addCollisionObject(ghostObject,btBroadphaseProxy::CharacterFilter, btBroadphaseProxy::StaticFilter|btBroadphaseProxy::DefaultFilter);
physics.getWorld()->addAction(controller);

This is the code I use to access the controller's position:

trans = controller->getGhostObject()->getWorldTransform();
camPosition.z = trans.getOrigin().z();
camPosition.y = trans.getOrigin().y()+0.5;
camPosition.x = trans.getOrigin().x();

The way I control it is through setWalkDirection() and jump() (if canJump() is true).

The issue right now is that the character spazzes out a little, then drops through the static floor. Clearly this is not intended. Is this due to the lack of a rigid body? How does one integrate that?

Actually, now it just falls as it should, but then slowly sinks through the floor.

I have moved this line to be right after the dynamic world is created

physics.getWorld()->getPairCache()->setInternalGhostPairCallback(new btGhostPairCallback());

It is now this:

broadphase->getOverlappingPairCache()->setInternalGhostPairCallback(new btGhostPairCallback());

I am also using a .bullet file imported from blender, if that is relevant.

The issue was with the bullet file, which has since been fixed(the collision boxes weren't working). However, I still experience jitteryness, unable to step up occasionally, instant step down from to high a height, and other issues.


回答1:


My answer to this question here tells you what worked well for me and apparently also for the person who asked.

Avoid ground collision with Bullet

The character controller implementations in bullet are very "basic" unfortunately. To get good character controller, you'll need to invest this much.



来源:https://stackoverflow.com/questions/18345710/how-do-you-control-a-player-character-in-bullet-physics

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!