Skip to content

Commit

Permalink
Fixed some bugs with Capsules and character controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
willcassella committed Apr 17, 2016
1 parent ba733de commit 3674e24
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 4 deletions.
Expand Up @@ -65,5 +65,6 @@ namespace willow

Handle<PrimitiveColliderComponent> _collider;
Settings _settings;
bool _is_active = false;
};
}
2 changes: 2 additions & 0 deletions Modules/Engine/include/Engine/Systems/PhysicsSystem.h
Expand Up @@ -130,6 +130,8 @@ namespace willow
Handle<PrimitiveColliderComponent> collider,
CharacterControllerComponent::Settings settings) = 0;

virtual void set_character_controller_collider(Handle<CharacterControllerComponent> component, Handle<PrimitiveColliderComponent> collider) = 0;

virtual void character_controller_jump(Handle<CharacterControllerComponent> component) = 0;

virtual void character_controller_on_ground(Handle<CharacterControllerComponent> component, bool& out) = 0;
Expand Down
Expand Up @@ -35,14 +35,22 @@ namespace willow
void CharacterControllerComponent::set_collider(PrimitiveColliderComponent* collider)
{
// If the collider exists and its not connected to the same Entity
if (collider && &collider->get_entity() == &this->get_entity())
if (collider && &collider->get_entity() != &this->get_entity())
{
// Invalid configuration
return;
}

this->_collider = collider;
// TODO: Notify physics system

if (this->_is_active)
{
this->get_world().get_system<PhysicsSystem>()->set_character_controller_collider(*this, this->_collider);
}
else
{
this->create();
}
}

void CharacterControllerComponent::on_initialize()
Expand All @@ -56,6 +64,7 @@ namespace willow
if (!this->_collider.is_null())
{
this->get_world().get_system<PhysicsSystem>()->create_character_controller(*this, this->get_entity(), this->_collider, this->_settings);
this->_is_active = true;
}
}
}
Expand Up @@ -92,6 +92,8 @@ namespace willow
Handle<PrimitiveColliderComponent> collider,
CharacterControllerComponent::Settings settings) override;

void set_character_controller_collider(Handle<CharacterControllerComponent> component, Handle<PrimitiveColliderComponent> collider) override;

void character_controller_jump(Handle<CharacterControllerComponent> component) override;

void character_controller_on_ground(Handle<CharacterControllerComponent> component, bool& out) override;
Expand Down
2 changes: 2 additions & 0 deletions Systems/BulletPhysics/private/CharacterController.h
Expand Up @@ -25,6 +25,8 @@ namespace willow
/** Reimplementation of 'canJump', allows for double-jumping (engine API only exposes 'on_ground()'). */
bool canJump() const override;

void set_collider(btConvexShape& collider);

////////////////
/// Data ///
private:
Expand Down
40 changes: 38 additions & 2 deletions Systems/BulletPhysics/source/BulletPhysicsSystem.cpp
Expand Up @@ -432,9 +432,36 @@ namespace willow
// TODO
}

void BulletPhysicsSystem::set_collider_shape(Handle<CapsuleColliderComponent> /*component*/, CapsuleColliderComponent::Shape /*shape*/)
void BulletPhysicsSystem::set_collider_shape(Handle<CapsuleColliderComponent> component, CapsuleColliderComponent::Shape shape)
{
// TODO
// Get the collider
auto* collider = _capsule_collider_table[component];
auto localScaling = collider->getLocalScaling();

// Get the entity
auto* entityData = static_cast<EntityPhysicsData*>(collider->getUserPointer());

// Destroy the old collider
collider->~btCapsuleShape();

// Rebuild it
switch (shape.axis)
{
case ColliderComponent::ShapeAxis::X:
new (collider) btCapsuleShapeX{ shape.radius, shape.height };
break;
case ColliderComponent::ShapeAxis::Y:
new (collider) btCapsuleShape{ shape.radius, shape.height };
break;
case ColliderComponent::ShapeAxis::Z:
new (collider) btCapsuleShapeZ{ shape.radius, shape.height };
break;
}
collider->setLocalScaling(localScaling);
collider->setUserPointer(entityData);

// Update entity
entityData->update_inertia();
}

void BulletPhysicsSystem::set_collider_shape(Handle<StaticMeshColliderComponent> /*component*/, StaticMeshColliderComponent::Shape /*shape*/)
Expand Down Expand Up @@ -463,6 +490,15 @@ namespace willow
_physics_world->GetDynamicsWorld().addAction(controller);
}

void BulletPhysicsSystem::set_character_controller_collider(Handle<CharacterControllerComponent> component, Handle<PrimitiveColliderComponent> collider)
{
auto* controller = _character_controller_table[component];
btConvexShape* bCollider = nullptr;
_capsule_collider_table.Find(collider.cast_to<CapsuleColliderComponent>(), bCollider);
_sphere_collider_table.Find(collider.cast_to<SphereColliderComponent>(), bCollider);
controller->set_collider(*bCollider);
}

void BulletPhysicsSystem::character_controller_jump(Handle<CharacterControllerComponent> component)
{
auto* controller = _character_controller_table[component];
Expand Down
6 changes: 6 additions & 0 deletions Systems/BulletPhysics/source/CharacterController.cpp
Expand Up @@ -34,4 +34,10 @@ namespace willow
{
return true;
}

void CharacterController::set_collider(btConvexShape& collider)
{
this->m_convexShape = &collider;
// TODO: Is there more I need to do here?
}
}

0 comments on commit 3674e24

Please sign in to comment.