Skip to content

Commit

Permalink
Merge pull request PanosK92#136 from PanosKolyvakis/main
Browse files Browse the repository at this point in the history
added water drag physics along y direction
  • Loading branch information
PanosK92 authored Dec 12, 2023
2 parents 549493c + 430ac2b commit 4386a9d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
23 changes: 18 additions & 5 deletions runtime/World/Components/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -530,11 +530,12 @@ namespace Spartan
// swim
if (is_underwater)
{

// buoyancy
{
float water_density = 1.03f * 0.001f; // to newton
float object_density = 0.8f * 0.001f; // to newton
float total_volume = m_physics_body_to_control->GetVolume();
float water_density = 1.03f;
float object_density = 0.8f;
float total_volume = m_physics_body_to_control->GetCapsuleVolume();

// calculate the submerged portion
float submerged_height = -GetEntity()->GetPosition().y;
Expand All @@ -545,9 +546,21 @@ namespace Spartan
float displacement_volume = total_volume * submerged_fraction * (object_density / water_density);
Vector3 buoyancy_force = -(water_density * m_physics_body_to_control->GetGravity() * displacement_volume);

m_physics_body_to_control->ApplyForce(buoyancy_force * 1000000.0f, PhysicsForce::Constant);
// compute drag factor
float drag_coefficient = 0.34f;
float frontal_area = std::pow(Helper::PI * m_physics_body_to_control->GetCapsuleRadius(), 2.0f);
float linear_velocity_y = water_density * m_physics_body_to_control->GetLinearVelocity().y;
float drag_force_y = 0.5f * water_density * linear_velocity_y * linear_velocity_y * drag_coefficient;

// Making drag force opposite to the velocity direction
if (linear_velocity_y > 0)
{
drag_force_y = -drag_force_y;
}

m_physics_body_to_control->ApplyForce(buoyancy_force * 1000.0f, PhysicsForce::Constant);
m_physics_body_to_control->ApplyForce(Vector3(0.0f, drag_force_y, 0.0f) * 200.0f, PhysicsForce::Constant);
}

// movement
Vector3 velocity_current = m_physics_body_to_control->GetLinearVelocity();
Vector3 velocity_new = Vector3(m_movement_speed.x * 20.0f, velocity_current.y, m_movement_speed.z * 20.0f);
Expand Down
9 changes: 8 additions & 1 deletion runtime/World/Components/PhysicsBody.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ namespace Spartan
return (is_scalable && is_above) ? hit_position : Vector3::Infinity;
}

float PhysicsBody::GetVolume()
float PhysicsBody::GetCapsuleVolume()
{
btCapsuleShape* capsule_shape = static_cast<btCapsuleShape*>(m_shape);

Expand All @@ -779,6 +779,13 @@ namespace Spartan
// total volume is the sum of the cylinder and two hemispheres
return cylinder_volume + hemisphere_volume;
}

float PhysicsBody::GetCapsuleRadius()
{
btCapsuleShape* capsule_shape = static_cast<btCapsuleShape*>(m_shape);

return capsule_shape->getRadius();
}

void PhysicsBody::UpdateShape()
{
Expand Down
7 changes: 4 additions & 3 deletions runtime/World/Components/PhysicsBody.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,10 @@ namespace Spartan
bool RayTraceIsGrounded() const;
Math::Vector3 RayTraceIsNearStairStep(const Math::Vector3& forward) const;

// volume
float GetVolume();

// dimensional properties
float GetCapsuleVolume();
float GetCapsuleRadius();

// misc
void ClearForces() const;
void Activate() const;
Expand Down

0 comments on commit 4386a9d

Please sign in to comment.