Fix collision of scaled entities

This commit is contained in:
tovjemam 2025-08-15 17:05:44 +02:00
parent edee72e4b8
commit ccc275c0f6
4 changed files with 17 additions and 5 deletions

View File

@ -74,13 +74,13 @@ void game::Entity::Move(glm::vec3& velocity, float dt)
{
// Primary occurrence hit firstly
hit_fraction = occu_hit_fraction;
hit_normal = occu_->inv_basis_ * occu_hit_normal;
hit_normal = occu_->inv_normal_basis_ * occu_hit_normal;
}
else if (other_hit)
{
// Other occurrence hit firstly
hit_fraction = other_hit_fraction;
hit_normal = other_occu_->inv_basis_ * other_hit_normal;
hit_normal = other_occu_->inv_normal_basis_ * other_hit_normal;
}
// Update the position based on the hit fraction
@ -153,6 +153,14 @@ game::EntityOccurrence::EntityOccurrence(Entity* entity, const CreateOccurrenceP
bt_capsule_(entity->GetCapsuleShape().radius * params.scale, entity->GetCapsuleShape().height * params.scale)
{
// compute normalized basis
for (size_t i = 0; i < 3; ++i)
{
normal_basis_[i] = glm::normalize(basis_[i]);
}
// compute inverse normalized basis
inv_normal_basis_ = glm::inverse(normal_basis_);
}
bool game::EntityOccurrence::Sweep(const glm::vec3& target_position, float& hit_fraction, glm::vec3& hit_normal, const Portal** hit_portal)
@ -169,7 +177,7 @@ bool game::EntityOccurrence::Sweep(const glm::vec3& target_position, float& hit_
return sector_->SweepCapsule(
bt_capsule_,
basis_,
normal_basis_,
position_,
target_position,
hit_fraction,

View File

@ -30,9 +30,12 @@ namespace game
glm::vec3 position_;
float scale_;
glm::mat3 basis_; // Orthonormal basis in sector space
glm::mat3 basis_; // Orthogonal basis in sector space
glm::mat3 inv_basis_;
glm::mat3 normal_basis_; // Normalized basis for collision detection
glm::mat3 inv_normal_basis_;
btCapsuleShapeZ bt_capsule_;
friend class Entity;

View File

@ -87,7 +87,7 @@ void game::Player::Update(float dt)
glm::vec3 velocity = glm::vec3(
velocity_xy,
-1.0f // No vertical movement for now
velocity_.z - gravity_ * dt // No vertical movement for now
);
velocity_ = velocity;

View File

@ -20,6 +20,7 @@ namespace game
float max_speed_ = 4.0f;
float acceleration_ = 50.0f;
float deceleration_ = 20.0f;
float gravity_ = 9.81f; // Gravity acceleration
float time_ = 0.0f;
float current_speed_ = 0.0f;