Skip to content

Commit

Permalink
[World] Added physics controlled wheels (they render and physics don'…
Browse files Browse the repository at this point in the history
…t sync correctly yet), and changed the song of the default car world to something from NFS
  • Loading branch information
PanosK92 committed Oct 8, 2023
1 parent 4c5f331 commit 565d4c1
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 49 deletions.
Binary file added assets/models/wheel/albedo.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/models/wheel/metalness.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/models/wheel/model.blend
Binary file not shown.
Binary file added assets/models/wheel/normal.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/models/wheel/roughness.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed assets/music/isola_any_day.mp3
Binary file not shown.
Binary file not shown.
99 changes: 61 additions & 38 deletions runtime/World/Components/PhysicsBody.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ namespace Spartan
constexpr float k_default_restitution = 0.0f;
constexpr float k_default_friction = 0.5f;
constexpr float k_default_friction_rolling = 0.5f;
constexpr float k_default_vehicle_torque = 149.0f;
constexpr float k_default_vehicle_torque = 1000.0f;
}

class MotionState : public btMotionState
Expand Down Expand Up @@ -117,6 +117,7 @@ namespace Spartan
m_center_of_mass = Vector3::Zero;
m_size = Vector3::One;
m_shape = nullptr;
m_wheel_transforms.fill(nullptr);

SP_REGISTER_ATTRIBUTE_VALUE_VALUE(m_mass, float);
SP_REGISTER_ATTRIBUTE_VALUE_VALUE(m_friction, float);
Expand Down Expand Up @@ -188,44 +189,61 @@ namespace Spartan
}
}

// vehicle control
if (vehicle)
{
// compute torque
if (Input::GetKey(KeyCode::Arrow_Up))
// control
{
m_torque_newtons = m_torque_max_newtons;
}
else if(Input::GetKey(KeyCode::Arrow_Down))
{
m_torque_newtons = -m_torque_max_newtons;
}
else
{
m_torque_newtons = 0.0f;
}
// compute torque
if (Input::GetKey(KeyCode::Arrow_Up))
{
m_torque_newtons = m_torque_max_newtons;
}
else if (Input::GetKey(KeyCode::Arrow_Down))
{
m_torque_newtons = -m_torque_max_newtons;
}
else
{
m_torque_newtons = 0.0f;
}

// compute steering angle
if (Input::GetKey(KeyCode::Arrow_Left))
{
m_steering_angle_radians = -0.3f;
}
else if (Input::GetKey(KeyCode::Arrow_Right))
{
m_steering_angle_radians = 0.3f;
}
else
{
m_steering_angle_radians = 0.0f;
// compute steering angle
if (Input::GetKey(KeyCode::Arrow_Left))
{
m_steering_angle_radians = -0.3f;
}
else if (Input::GetKey(KeyCode::Arrow_Right))
{
m_steering_angle_radians = 0.3f;
}
else
{
m_steering_angle_radians = 0.0f;
}

// apply torque
vehicle->applyEngineForce(m_torque_newtons, 0); // wheel front-left
vehicle->applyEngineForce(m_torque_newtons, 1); // wheel front-right

// apply steering angle
vehicle->setSteeringValue(m_steering_angle_radians, 0); // wheel front-left
vehicle->setSteeringValue(m_steering_angle_radians, 1); // wheel front-left
}

// apply torque
vehicle->applyEngineForce(m_torque_newtons, 0); // wheel front-left
vehicle->applyEngineForce(m_torque_newtons, 1); // wheel front-right
// update wheel transforms
for (int i = 0; i < vehicle->getNumWheels(); ++i)
{
if (Transform* wheel_transform = m_wheel_transforms[i])
{
// get the bt transform of wheel i
vehicle->updateWheelTransform(i, true);
btTransform& wheel_transform_bt = vehicle->getWheelInfo(i).m_worldTransform;

// apply steering angle
vehicle->setSteeringValue(m_steering_angle_radians, 0); // wheel front-left
vehicle->setSteeringValue(m_steering_angle_radians, 1); // wheel front-left
// set the bt transform to the wheel transform
wheel_transform->SetPosition(ToVector3(wheel_transform_bt.getOrigin()));
wheel_transform->SetRotation(ToQuaternion(wheel_transform_bt.getRotation()));
}
}
}
}

Expand Down Expand Up @@ -628,13 +646,13 @@ namespace Spartan

// add wheels
{
btVector3 wheel_direction = btVector3(0, -1, 0);
btVector3 wheel_axle = btVector3(-1, 0, 0);
btScalar suspension_rest_length = 0.6f;
btScalar wheel_radius = 0.5;
btVector3 wheel_direction = btVector3(0, -1, 0);
btVector3 wheel_axle = btVector3(-1, 0, 0);
float suspension_rest_length = 0.6f;
float wheel_radius = 0.6;

const float extent_forward = 2.5f;
const float extent_sideways = 1.2f;
const float extent_forward = 2.5f;
const float extent_sideways = 1.2f;
btVector3 wheel_positions[4] =
{
btVector3(-extent_sideways, 0, extent_forward), // front-left
Expand Down Expand Up @@ -798,6 +816,11 @@ namespace Spartan
AddBodyToWorld();
}

void PhysicsBody::SetWheelTransform(Transform* transform, uint32_t wheel_index)
{
m_wheel_transforms[wheel_index] = transform;
}

bool PhysicsBody::IsGrounded() const
{
// get the lowest point of the AABB
Expand Down
4 changes: 4 additions & 0 deletions runtime/World/Components/PhysicsBody.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ namespace Spartan
float GetTorqueMaxNewtons() const { return m_torque_max_newtons; }
void SetTorqueMaxNewtons(float torque_newtons) { m_torque_max_newtons = torque_newtons; }

// Wheels
void SetWheelTransform(Transform* transform, uint32_t wheel_index);

// Misc
bool IsGrounded() const;
void ClearForces() const;
Expand Down Expand Up @@ -185,6 +188,7 @@ namespace Spartan
float m_torque_newtons = 0.0f;
float m_torque_max_newtons = 0.0f;
float m_steering_angle_radians = 0.0f;
std::array<Transform*, 4> m_wheel_transforms;
std::vector<Constraint*> m_constraints;
};
}
71 changes: 60 additions & 11 deletions runtime/World/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ namespace Spartan
static shared_ptr<Mesh> m_default_model_sponza = nullptr;
static shared_ptr<Mesh> m_default_model_sponza_curtains = nullptr;
static shared_ptr<Mesh> m_default_model_car = nullptr;
static shared_ptr<Mesh> m_default_model_wheel = nullptr;
static shared_ptr<Mesh> m_default_model_helmet_flight = nullptr;
static shared_ptr<Mesh> m_default_model_helmet_damaged = nullptr;

Expand Down Expand Up @@ -211,6 +212,7 @@ namespace Spartan
m_default_model_sponza = nullptr;
m_default_model_sponza_curtains = nullptr;
m_default_model_car = nullptr;
m_default_model_wheel = nullptr;
m_default_model_helmet_flight = nullptr;
m_default_model_helmet_damaged = nullptr;
m_default_cube = nullptr;
Expand Down Expand Up @@ -569,7 +571,7 @@ namespace Spartan
{
Vector3 camera_position = Vector3(0.0f, 1.0f, -10.0f);
Vector3 camera_rotation = Vector3(0.0f, 0.0f, 0.0f);
create_default_world_common(camera_position, camera_rotation, LightIntensity::sky_twilight, "project\\music\\isola_any_day.mp3");
create_default_world_common(camera_position, camera_rotation, LightIntensity::sky_twilight, "project\\music\\riders_on_the_storm_fredwreck_remix.mp3");

// point light - side of car
{
Expand Down Expand Up @@ -614,7 +616,7 @@ namespace Spartan
Entity* entity_car = m_default_model_car->GetRootEntity();
entity_car->SetObjectName("geometry");

entity_car->GetTransform()->SetPosition(Vector3(0.0f, -0.75f, 0.0f));
entity_car->GetTransform()->SetPosition(Vector3(0.0f, 0.0f, 0.0f));
entity_car->GetTransform()->SetRotation(Quaternion::FromEulerAngles(90.0f, 0.0f, -180.0f));
entity_car->GetTransform()->SetScale(Vector3(0.02f, 0.02f, 0.02f));

Expand Down Expand Up @@ -698,6 +700,16 @@ namespace Spartan

// add physics body
{
entity_root->GetTransform()->SetPosition(Vector3(0.0f, 2.0f, 0.0f));
PhysicsBody* physics_body = entity_root->AddComponent<PhysicsBody>().get();
physics_body->SetBodyType(PhysicsBodyType::Vehicle);
physics_body->SetCenterOfMass(Vector3(0.0f, 0.7f, 0.0f));
physics_body->SetBoundingBox(Vector3(3.0f, 1.5f, 8.4f));
physics_body->SetFriction(1.0f);
physics_body->SetFrictionRolling(1.0f);
physics_body->SetMass(1000.0f); // 900 – 1,045 kg -> https://en.wikipedia.org/wiki/Toyota_AE86
physics_body->SetTorqueMaxNewtons(5000.0f); // 149 Nm -> https://en.wikipedia.org/wiki/Toyota_AE86, however it's too weak for bullet for some reason

// remove all the wheels since they have weird rotations, we will add our own
{
RemoveEntity(entity_car->GetTransform()->GetDescendantPtrWeakByName("FL_Wheel_RimMaterial_0").lock());
Expand All @@ -721,15 +733,52 @@ namespace Spartan
RemoveEntity(entity_car->GetTransform()->GetDescendantPtrWeakByName("RR_Caliper_BrakeCaliper_0").lock());
}

entity_root->GetTransform()->SetPosition(Vector3(0.0f, 2.0f, 0.0f));
PhysicsBody* physics_body = entity_root->AddComponent<PhysicsBody>().get();
physics_body->SetBodyType(PhysicsBodyType::Vehicle);
physics_body->SetCenterOfMass(Vector3(0.0f, 0.7f, 0.0f));
physics_body->SetBoundingBox(Vector3(3.0f, 1.5f, 8.4f));
physics_body->SetFriction(1.0f);
physics_body->SetFrictionRolling(1.0f);
physics_body->SetMass(1000.0f); // 900 – 1,045 kg -> https://en.wikipedia.org/wiki/Toyota_AE86
physics_body->SetTorqueMaxNewtons(5000.0f); // 149 Nm -> https://en.wikipedia.org/wiki/Toyota_AE86, however it's too weak for bullet for some reason
// load our own wheel
if (m_default_model_wheel = ResourceCache::Load<Mesh>("project\\models\\wheel\\model.blend"))
{
Entity* entity_wheel_root = m_default_model_wheel->GetRootEntity();
entity_wheel_root->GetTransform()->SetScale(Vector3(0.35f));

if (Entity* entity_wheel = entity_wheel_root->GetTransform()->GetDescendantPtrByName("wheel Low"))
{
// create material
shared_ptr<Material> material = make_shared<Material>();
material->SetTexture(MaterialTexture::Color, "project\\models\\wheel\\albedo.jpeg");
material->SetTexture(MaterialTexture::Normal, "project\\models\\wheel\\normal.png");
material->SetTexture(MaterialTexture::Roughness, "project\\models\\wheel\\roughness.png");
material->SetTexture(MaterialTexture::Metalness, "project\\models\\wheel\\metalness.png");

// create a file path for this material (required for the material to be able to be cached by the resource cache)
const string file_path = "project\\models\\wheel" + string(EXTENSION_MATERIAL);
material->SetResourceFilePath(file_path);

// set material
entity_wheel->GetComponent<Renderable>()->SetMaterial(material);
}

// add the wheels to the body
{
Entity* wheel = entity_wheel_root;
wheel->SetObjectName("wheel_fl");
wheel->GetTransform()->SetParent(entity_root->GetTransform());
physics_body->SetWheelTransform(wheel->GetTransform().get(), 0);

wheel = entity_wheel_root->Clone();
wheel->SetObjectName("wheel_fr");
wheel->GetTransform()->SetParent(entity_root->GetTransform());
physics_body->SetWheelTransform(wheel->GetTransform().get(), 1);

wheel = entity_wheel_root->Clone();
wheel->SetObjectName("wheel_rl");
wheel->GetTransform()->SetParent(entity_root->GetTransform());
physics_body->SetWheelTransform(wheel->GetTransform().get(), 2);

wheel = entity_wheel_root->Clone();
wheel->SetObjectName("wheel_rr");
wheel->GetTransform()->SetParent(entity_root->GetTransform());
physics_body->SetWheelTransform(wheel->GetTransform().get(), 3);
}
}
}
}

Expand Down

0 comments on commit 565d4c1

Please sign in to comment.