Skip to content

Commit

Permalink
[game] car control is disabled when the player exits the car
Browse files Browse the repository at this point in the history
  • Loading branch information
PanosK92 committed Dec 4, 2024
1 parent ff1bdf6 commit 894f4b6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
6 changes: 5 additions & 1 deletion runtime/Game/Car.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,11 @@ namespace Spartan
m_parameters.movement_direction = CarMovementState::Stationary;
}

HandleInput();
if (m_parameters.control_enabled)
{
HandleInput();
}

ApplyForces();
UpdateTransforms();

Expand Down
3 changes: 3 additions & 0 deletions runtime/Game/Car.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ namespace Spartan
btRaycastVehicle* vehicle = nullptr;
btRigidBody* body = nullptr;
Entity* transform_steering_wheel = nullptr;
bool control_enabled = true;
std::vector<Entity*> transform_wheels;
};

Expand All @@ -93,6 +94,8 @@ namespace Spartan
float GetSpeedKilometersPerHour() const;
float GetSpeedMetersPerSecond() const;

void SetControlEnabled(const bool enabled) { m_parameters.control_enabled = enabled; }

private:
void HandleInput();
void ApplyForces();
Expand Down
15 changes: 10 additions & 5 deletions runtime/Game/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,10 @@ namespace Spartan
physics_body->SetCenterOfMass(Vector3(0.0f, 1.2f, 0.0f));
physics_body->SetBoundingBox(Vector3(3.0f, 1.9f, 7.0f));
physics_body->SetMass(960.0f); // http://www.j-garage.com/toyota/ae86.html


// disable car control (it's toggled via the gameplay code in Tick())
physics_body->GetCar()->SetControlEnabled(false);

// set the steering wheel to the physics body so that it can rotate it
if (Entity* entity_steering_wheel = entity_car->GetDescendantByName("SteeringWheel_SteeringWheel_0"))
{
Expand Down Expand Up @@ -1105,22 +1108,24 @@ namespace Spartan
camera->SetParent(m_default_car);
camera->SetPositionLocal(car_view_positions[static_cast<int>(current_view)]);
camera->SetRotationLocal(Quaternion::Identity);

inside_the_car = true;
}
else
{
camera = m_default_car->GetChildByName("component_camera");
camera->SetParent(m_default_physics_body_camera);

Vector3 exit_position = m_default_car->GetPosition() + m_default_car->GetLeft() * 3.0f + Vector3::Up * 2.0f;
m_default_physics_body_camera->GetComponent<PhysicsBody>()->SetPosition(exit_position);

camera->SetPositionLocal(Vector3(0.0f, 1.8f, 0.0f));
camera->SetRotationLocal(Quaternion::Identity);

// place the camera on the left of the driver's door
m_default_physics_body_camera->GetComponent<PhysicsBody>()->SetPosition(m_default_car->GetPosition() + m_default_car->GetLeft() * 3.0f + Vector3::Up * 2.0f);

inside_the_car = false;
}

camera->GetComponent<Camera>()->SetFlag(CameraFlags::CanBeControlled, !inside_the_car);
m_default_car->AddComponent<PhysicsBody>()->GetCar()->SetControlEnabled(inside_the_car);
}

// change car view
Expand Down

0 comments on commit 894f4b6

Please sign in to comment.