I'm working on a game on my spare time. I have few things working but now it's time for player/player, player/level, ray shoot/player intersection and repulsion. So nothing highly "physical dynamics" actually.
I've choosed Bullet because it seems to deal with all of that (specially with capsule shapes for players) without all the highly dynamic stuff (using btCollisionWorld, btCollisionObject).
So I have:
- Players represented by btCollisionObject with a btCapsuleShape.
- A level (actually a simple static collision geometry).
- Players shoot using a simple ray (no physical balistics).
But now I'm a little lost. The documentation is not very big and even if I've looked at thoose many demos and tutorials, I'm not confident if I'm using Bullet in the good way (if there is one).
For now, I'm only on Player/Player repulsion.
I've used the btCollisionWorld->contactTest() with an overrided ContactResultCallback but I think it's a little tricky:
The struct:
Code: Select all
struct PlayerContactResultCallback : public btCollisionWorld::ContactResultCallback {
PlayerContactResultCallback( btCollisionObject &player_collision_obj )
: btCollisionWorld::ContactResultCallback(),
m_player_collision_obj( player_collision_obj ) ,
hit( false ) ,
distance( 0.0 ) {
}
btCollisionObject& m_player_collision_obj;
bool hit;
float distance;
virtual btScalar addSingleResult( btManifoldPoint& cp,
const btCollisionObjectWrapper* colObj0, int partId0, int index0,
const btCollisionObjectWrapper* colObj1, int partId1, int index1)
{
if( colObj0->m_collisionObject == &m_player_collision_obj) {
hit = true;
distance = (float) cp.getDistance();
std::printf( "collide dist: %f\n", distance);
}
return 0; // Return what actually?
}
};
Code: Select all
// move the capsule collider to the player position
btTransform world_transform = btTransform();
btVector3 pos = btVector3();
pos.setX( myPlayer->position().X );
pos.setY( myPlayer->position().Y );
pos.setZ( myPlayer->position().Z );
world_transform.setOrigin( pos );
m_player_collider_objects[ i ]->setWorldTransform( world_transform );
// set the player velocity to the capsule to anticipate collision
btVector3 btVelocity = btVector3();
btVelocity.setX( player_velocity.X );
btVelocity.setY( player_velocity.Y );
btVelocity.setZ( player_velocity.Z );
m_player_collider_objects[ i ]->setInterpolationLinearVelocity( btVelocity );
PlayerContactResultCallback resultCallback = PlayerContactResultCallback( *m_player_collider_objects[ i ] );
m_bt_collision_world->contactTest( m_player_collider_objects[ i ] , resultCallback);
if( resultCallback.hit ) {
float repulsion = (float) resultCallback.distance;
player_velocity.setLength( player_velocity.getLength() - repulsion );
}
I'm sure I'm doing thinks bad.
If you had to deal my three simple repulsion (player/player, player/level, ray shoot/(player and level)) what are the firsts classes and methods you are thinking about?
How would you do that?
A big thanks in advance to you guys!
