Skip to content

Commit

Permalink
[physics] added support for compound shapes and therefore instanced g…
Browse files Browse the repository at this point in the history
…eometry can now have collision
  • Loading branch information
PanosK92 committed Jan 7, 2025
1 parent 44dedcf commit f2b97b9
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 85 deletions.
4 changes: 2 additions & 2 deletions runtime/Game/Car.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ namespace Spartan
if (debug::enabled && wheel_info->m_bIsFrontWheel)
{
const float arrow_size = 0.02f;
Vector3 start = ToVector3(*force_position);
Vector3 start = bt_to_vector(*force_position);

// draw fz force
Math::Vector3 fz_end = start + Math::Vector3(parameters.pacejka_fz[wheel_index] * wheel_forward_dir) * 0.2f;
Expand Down Expand Up @@ -818,7 +818,7 @@ namespace Spartan
btTransform& transform_bt = m_parameters.vehicle->getWheelInfo(wheel_index).m_worldTransform;

// set the bullet transform to the wheel transform
transform->SetPosition(ToVector3(transform_bt.getOrigin()));
transform->SetPosition(bt_to_vector(transform_bt.getOrigin()));

// ToQuaternion() works with everything but the wheels, I suspect that this is because bullet uses a different
// rotation order since it's using a right-handed coordinate system, hence a simple quaternion conversion won't work
Expand Down
5 changes: 5 additions & 0 deletions runtime/Game/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,11 @@ namespace Spartan
// generate instances
terrain->GenerateTransforms(&instances, 5000, TerrainProp::Tree);
renderable->SetInstances(instances);

// make the bark collidable
shared_ptr<PhysicsBody> rigid_body = bark->AddComponent<PhysicsBody>();
rigid_body->SetMass(0.0f);
rigid_body->SetShapeType(PhysicsShape::Box);
}

if (Entity* branches = entity->GetDescendantByName("Branches"))
Expand Down
8 changes: 4 additions & 4 deletions runtime/Physics/BulletPhysicsHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,22 @@ SP_WARNINGS_OFF
SP_WARNINGS_ON
//==================================

inline Spartan::Math::Vector3 ToVector3(const btVector3& vector)
inline Spartan::Math::Vector3 bt_to_vector(const btVector3& vector)
{
return Spartan::Math::Vector3(vector.getX(), vector.getY(), vector.getZ());
}

inline btVector3 ToBtVector3(const Spartan::Math::Vector3& vector)
inline btVector3 vector_to_bt(const Spartan::Math::Vector3& vector)
{
return btVector3(vector.x, vector.y, vector.z);
}

inline btQuaternion ToBtQuaternion(const Spartan::Math::Quaternion& quaternion)
inline btQuaternion quaternion_to_bt(const Spartan::Math::Quaternion& quaternion)
{
return btQuaternion(quaternion.x, quaternion.y, quaternion.z, quaternion.w);
}

inline Spartan::Math::Quaternion ToQuaternion(const btQuaternion& quaternion)
inline Spartan::Math::Quaternion bt_to_quaternion(const btQuaternion& quaternion)
{
return Spartan::Math::Quaternion(quaternion.getX(), quaternion.getY(), quaternion.getZ(), quaternion.getW());
}
22 changes: 11 additions & 11 deletions runtime/Physics/Physics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ namespace Spartan
world_info->water_density = 0;
world_info->water_offset = 0;
world_info->water_normal = btVector3(0, 0, 0);
world_info->m_gravity = ToBtVector3(gravity);
world_info->m_gravity = vector_to_bt(gravity);
}
else
{
Expand All @@ -107,7 +107,7 @@ namespace Spartan
}

// setup
world->setGravity(ToBtVector3(gravity));
world->setGravity(vector_to_bt(gravity));
world->getDispatchInfo().m_useContinuous = true;
world->getSolverInfo().m_splitImpulse = false;
world->getSolverInfo().m_numIterations = max_solve_iterations;
Expand Down Expand Up @@ -195,8 +195,8 @@ namespace Spartan

vector<btRigidBody*> Physics::RayCast(const Vector3& start, const Vector3& end)
{
btVector3 bt_start = ToBtVector3(start);
btVector3 bt_end = ToBtVector3(end);
btVector3 bt_start = vector_to_bt(start);
btVector3 bt_end = vector_to_bt(end);

btCollisionWorld::AllHitsRayResultCallback ray_callback(bt_start, bt_end);
world->rayTest(bt_start, bt_end, ray_callback);
Expand All @@ -218,15 +218,15 @@ namespace Spartan

Vector3 Physics::RayCastFirstHitPosition(const Math::Vector3& start, const Math::Vector3& end)
{
btVector3 bt_start = ToBtVector3(start);
btVector3 bt_end = ToBtVector3(end);
btVector3 bt_start = vector_to_bt(start);
btVector3 bt_end = vector_to_bt(end);

btCollisionWorld::ClosestRayResultCallback ray_callback(bt_start, bt_end);
world->rayTest(bt_start, bt_end, ray_callback);

if (ray_callback.hasHit())
{
return ToVector3(ray_callback.m_hitPointWorld);
return bt_to_vector(ray_callback.m_hitPointWorld);
}

return Vector3::Infinity;
Expand Down Expand Up @@ -316,8 +316,8 @@ namespace Spartan
Vector3 ray_direction = picking_ray.GetDirection();
Vector3 ray_end = ray_start + ray_direction * camera->GetFarPlane();

btVector3 bt_ray_start = ToBtVector3(ray_start);
btVector3 bt_ray_end = ToBtVector3(ray_end);
btVector3 bt_ray_start = vector_to_bt(ray_start);
btVector3 bt_ray_end = vector_to_bt(ray_end);
btCollisionWorld::ClosestRayResultCallback ray_callback(bt_ray_start, bt_ray_end);

ray_callback.m_flags |= btTriangleRaycastCallback::kF_UseGjkConvexCastRaytest;
Expand Down Expand Up @@ -345,7 +345,7 @@ namespace Spartan
}
}

hit_position = ToVector3(pick_position);
hit_position = bt_to_vector(pick_position);
picking_distance_previous = (hit_position - ray_start).Length();
}
}
Expand Down Expand Up @@ -379,7 +379,7 @@ namespace Spartan
// keep it at the same picking distance
ray_direction *= picking_distance_previous;
Vector3 new_pivot_b = ray_start + ray_direction;
pick_constraint->setPivotB(ToBtVector3(new_pivot_b));
pick_constraint->setPivotB(vector_to_bt(new_pivot_b));
}
}
}
Expand Down
30 changes: 15 additions & 15 deletions runtime/World/Components/Constraint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,34 +233,34 @@ namespace Spartan
case POINT2POINT_CONSTRAINT_TYPE:
{
auto* point_constraint = dynamic_cast<btPoint2PointConstraint*>(m_constraint);
point_constraint->setPivotA(ToBtVector3(own_body_scaled_position));
point_constraint->setPivotB(ToBtVector3(other_body_scaled_position));
point_constraint->setPivotA(vector_to_bt(own_body_scaled_position));
point_constraint->setPivotB(vector_to_bt(other_body_scaled_position));
}
break;

case HINGE_CONSTRAINT_TYPE:
{
auto* hinge_constraint = dynamic_cast<btHingeConstraint*>(m_constraint);
btTransform own_frame(ToBtQuaternion(m_rotation), ToBtVector3(own_body_scaled_position));
btTransform other_frame(ToBtQuaternion(m_rotationOther), ToBtVector3(other_body_scaled_position));
btTransform own_frame(quaternion_to_bt(m_rotation), vector_to_bt(own_body_scaled_position));
btTransform other_frame(quaternion_to_bt(m_rotationOther), vector_to_bt(other_body_scaled_position));
hinge_constraint->setFrames(own_frame, other_frame);
}
break;

case SLIDER_CONSTRAINT_TYPE:
{
auto* slider_constraint = dynamic_cast<btSliderConstraint*>(m_constraint);
btTransform own_frame(ToBtQuaternion(m_rotation), ToBtVector3(own_body_scaled_position));
btTransform other_frame(ToBtQuaternion(m_rotationOther), ToBtVector3(other_body_scaled_position));
btTransform own_frame(quaternion_to_bt(m_rotation), vector_to_bt(own_body_scaled_position));
btTransform other_frame(quaternion_to_bt(m_rotationOther), vector_to_bt(other_body_scaled_position));
slider_constraint->setFrames(own_frame, other_frame);
}
break;

case CONETWIST_CONSTRAINT_TYPE:
{
auto* cone_twist_constraint = dynamic_cast<btConeTwistConstraint*>(m_constraint);
btTransform own_frame(ToBtQuaternion(m_rotation), ToBtVector3(own_body_scaled_position));
btTransform other_frame(ToBtQuaternion(m_rotationOther), ToBtVector3(other_body_scaled_position));
btTransform own_frame(quaternion_to_bt(m_rotation), vector_to_bt(own_body_scaled_position));
btTransform other_frame(quaternion_to_bt(m_rotationOther), vector_to_bt(other_body_scaled_position));
cone_twist_constraint->setFrames(own_frame, other_frame);
}
break;
Expand Down Expand Up @@ -310,30 +310,30 @@ namespace Spartan
{
case ConstraintType_Point:
{
m_constraint = new btPoint2PointConstraint(*bt_own_body, *bt_other_body, ToBtVector3(own_body_scaled_position), ToBtVector3(other_body_scaled_position));
m_constraint = new btPoint2PointConstraint(*bt_own_body, *bt_other_body, vector_to_bt(own_body_scaled_position), vector_to_bt(other_body_scaled_position));
}
break;

case ConstraintType_Hinge:
{
btTransform own_frame(ToBtQuaternion(m_rotation), ToBtVector3(own_body_scaled_position));
btTransform other_frame(ToBtQuaternion(m_rotationOther), ToBtVector3(other_body_scaled_position));
btTransform own_frame(quaternion_to_bt(m_rotation), vector_to_bt(own_body_scaled_position));
btTransform other_frame(quaternion_to_bt(m_rotationOther), vector_to_bt(other_body_scaled_position));
m_constraint = new btHingeConstraint(*bt_own_body, *bt_other_body, own_frame, other_frame);
}
break;

case ConstraintType_Slider:
{
btTransform own_frame(ToBtQuaternion(m_rotation), ToBtVector3(own_body_scaled_position));
btTransform other_frame(ToBtQuaternion(m_rotationOther), ToBtVector3(other_body_scaled_position));
btTransform own_frame(quaternion_to_bt(m_rotation), vector_to_bt(own_body_scaled_position));
btTransform other_frame(quaternion_to_bt(m_rotationOther), vector_to_bt(other_body_scaled_position));
m_constraint = new btSliderConstraint(*bt_own_body, *bt_other_body, own_frame, other_frame, false);
}
break;

case ConstraintType_ConeTwist:
{
btTransform own_frame(ToBtQuaternion(m_rotation), ToBtVector3(own_body_scaled_position));
btTransform other_frame(ToBtQuaternion(m_rotationOther), ToBtVector3(other_body_scaled_position));
btTransform own_frame(quaternion_to_bt(m_rotation), vector_to_bt(own_body_scaled_position));
btTransform other_frame(quaternion_to_bt(m_rotationOther), vector_to_bt(other_body_scaled_position));
m_constraint = new btConeTwistConstraint(*bt_own_body, *bt_other_body, own_frame, other_frame);
}
break;
Expand Down
Loading

0 comments on commit f2b97b9

Please sign in to comment.