Skip to content

Commit

Permalink
Stop bullets getting stuck in walls. See comment for details.
Browse files Browse the repository at this point in the history
  • Loading branch information
ChickenProp committed Dec 29, 2010
1 parent b23b0c3 commit d7e1d0d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
23 changes: 22 additions & 1 deletion src/bullet.cpp
Expand Up @@ -10,10 +10,31 @@ Bullet::Bullet(ph::vec3f pos, ph::vec3f vel) {
initialize(mass, inertia, state);

body->setGravity(btVector3(0,0,0));

// If we move further than this in a given timestep, use continuous
// collision detection.
body->setCcdMotionThreshold(0.2);
body->setCcdSweptSphereRadius(0.2);

// CCD sweeps a sphere along the path the body will take, then checks
// whether it collides with anything. This is the radius of that sphere.
// If it's too big, the bullet can get stuck in walls. I assume this
// happens because the bullet is stopped before the sweep-sphere
// collides, hence the bullet itself is not at that point colliding, so
// no collision resolution happens. I don't understand why .15 would be
// too big, given that the actual collision shape has a radius of .2,
// but when I tried this at .15, they did sometimes get stuck.
body->setCcdSweptSphereRadius(0.1);
}

void Bullet::update() {
ph::vec3f vel = body->getLinearVelocity();
printf("%f, %f, %f\n", vel.x, vel.y, vel.z);

btTransform trans;
body->getMotionState()->getWorldTransform(trans);
ph::vec3f pos = trans.getOrigin();
printf("%f, %f, %f\n", pos.x, pos.y, pos.z);
}



Expand Down
2 changes: 2 additions & 0 deletions src/bullet.h
Expand Up @@ -8,6 +8,8 @@ class Bullet : public Entity {
typedef Entity super;

Bullet(ph::vec3f pos, ph::vec3f vel);

void update();
};

#endif
6 changes: 4 additions & 2 deletions src/world.cpp
Expand Up @@ -23,8 +23,6 @@ World::World() {
groundBody = new btRigidBody(construct);

G::physics->addRigidBody(groundBody);

addEntity(new Bullet(ph::vec3f(0, 2, 2), ph::vec3f(0,0,0)));
}

World::~World () {
Expand Down Expand Up @@ -52,6 +50,10 @@ void World::update() {
rotate += 1.0f;

printf("%d\n", gNumClampedCcdMotions);

foreach(Entity *e, entities) {
e->update();
}
}

void World::addEntity(Entity *ent) {
Expand Down

0 comments on commit d7e1d0d

Please sign in to comment.