|
I'm trying to make a character controller based on the information in docs/character.md, and running into some issues. Namely, my character only ever seems to collide correctly with the floor I've added, and never any other bodies that I add to the world, no matter how simple. I tried adding a small body with a b3BoxHull of half-size (1, 1, 1) at (0, 0, 0) in the world, and collision is.. weird. My debug log in collectPlaneResults (see below) seems to indicate I collide with it only after walking FAR away from the origin of the world and back, and then is extremely inconsistent, but never, ever actually has any effect on my movement. In short: The floor prevents me from falling, but no other bodies prevent me from moving or collide correctly with me at all. I think the position of the character capsule may be falling out of sync in some way with the camera, but I'm not sure. I had initially tried using the My movement is currently implemented like this: const float move_fraction = b3World_CastMover(
world,
b3Pos_zero,
&characterCapsule,
to_b3_vec3(desired_movement),
b3DefaultQueryFilter(),
nullptr,
nullptr
);
const auto motion = desired_movement * move_fraction;
camera.position += motion;
camera.target += motion;
// to_b3_vec3 just casts one library's vec3 type to another
characterCapsule.center1 += to_b3_vec3(motion);
characterCapsule.center2 += to_b3_vec3(motion);
static std::vector<b3CollisionPlane> results;
results.clear();
b3World_CollideMover(
world,
b3Pos_zero,
&characterCapsule,
b3DefaultQueryFilter(),
collectPlaneResults,
&results
);
const auto [delta, _] = b3SolvePlanes(b3Vec3_zero, results.data(),
results.size());
characterCapsule.center1 += delta;
characterCapsule.center2 += delta;
// to_ray_vec3 just casts one library's vec3 type to another
camera.position += to_ray_vec3(delta);
camera.target += to_ray_vec3(delta);The implementation of collectPlaneResults is: bool collectPlaneResults(b3ShapeId shape, const b3PlaneResult *result,
int planeCount, void *context)
{
auto *c = static_cast<std::vector<b3CollisionPlane> *>(context);
printf("Hit with: %s\n", b3Body_GetName(b3Shape_GetBody(shape)));
c->reserve(c->size() + planeCount);
for (int i = 0; i < planeCount; i++) {
const auto& r = result[i];
c->emplace_back(b3CollisionPlane{
.plane = r.plane,
.pushLimit = FLT_MAX,
.push = 0.0f,
.clipVelocity = true
});
}
return true;
} |
Replies: 2 comments
|
My code for creating the bodies: One that acts weird/wrong: b3BodyDef bodyDef = b3DefaultBodyDef();
bodyDef.position = { 0.0, 0.5, 0.0 };
bodyDef.type = b3_staticBody;
bodyDef.name = "IT WON'T COLLIDE WITH THIS ONE";
const b3BodyId body = b3CreateBody(world, &bodyDef);
const b3BoxHull hull = b3MakeBoxHull(0.5f, 0.5f, 0.5f);
const b3ShapeDef shape = b3DefaultShapeDef();
b3CreateHullShape(body, &shape, &hull.base);The floor (works): b3BodyDef bodyDef = b3DefaultBodyDef();
bodyDef.position = { 0.0, -0.5, 0.0 };
bodyDef.type = b3_staticBody;
bodyDef.name = "FLOOR";
const b3BodyId body = b3CreateBody(world, &bodyDef);
const b3BoxHull hull = b3MakeBoxHull(50.0f, 0.5f, 50.0f);
const b3ShapeDef shape = b3DefaultShapeDef();
b3CreateHullShape(body, &shape, &hull.base); |
|
It turns out that keeping the capsule in place and passing the character's position through the Perhaps it's a documentation issue that docs/character.md advises to pass in zero and move the capsule's points around? |
It turns out that keeping the capsule in place and passing the character's position through the
originparameter is the correct way to go - not sure why I didn't have success with it before.Perhaps it's a documentation issue that docs/character.md advises to pass in zero and move the capsule's points around?