Skip to content

Commit

Permalink
[Car] Fixed crash when loading the car world
Browse files Browse the repository at this point in the history
  • Loading branch information
PanosK92 committed Dec 18, 2023
1 parent 2b397c6 commit d3541b3
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 41 deletions.
2 changes: 1 addition & 1 deletion runtime/World/Components/Terrain.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ namespace Spartan
uint32_t GetVertexCount() const { return m_vertex_count; }
uint32_t GetIndexCount() const { return m_index_count; }
uint64_t GetHeightSampleCount() const { return m_height_samples; }
float* GetHeightData() { return &m_height_data[0]; }
float* GetHeightData() { return !m_height_data.empty() ? &m_height_data[0] : nullptr; }

void GenerateAsync(std::function<void()> on_complete = nullptr);

Expand Down
4 changes: 2 additions & 2 deletions runtime/World/Entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ namespace Spartan

bool Entity::IsActiveRecursively()
{
if (Entity* parent = GetParent())
if (shared_ptr<Entity> parent = GetParent())
{
return m_is_active && parent->IsActiveRecursively();
}
Expand Down Expand Up @@ -515,7 +515,7 @@ namespace Spartan
shared_ptr<Entity> new_parent = new_parent_in.lock();
shared_ptr<Entity> parent = m_parent.lock();

if (!new_parent)
if (new_parent)
{
// early exit if the parent is this entity
if (GetObjectId() == new_parent->GetObjectId())
Expand Down
16 changes: 8 additions & 8 deletions runtime/World/Entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ namespace Spartan
bool HasRotationChangedThisFrame() const { return m_rotation_changed_this_frame; }
//================================================================================

//= HIERARCHY =============================================================================
//= HIERARCHY ===================================================================================
void SetParent(std::weak_ptr<Entity> new_parent);
Entity* GetChildByIndex(uint32_t index);
Entity* GetChildByName(const std::string& name);
Expand All @@ -157,13 +157,13 @@ namespace Spartan
bool IsDescendantOf(Entity* transform) const;
void GetDescendants(std::vector<Entity*>* descendants);
Entity* GetDescendantByName(const std::string& name);
bool HasParent() const { return !m_parent.expired(); }
bool HasChildren() const { return GetChildrenCount() > 0 ? true : false; }
uint32_t GetChildrenCount() const { return static_cast<uint32_t>(m_children.size()); }
Entity* GetRoot() { return HasParent() ? GetParent()->GetRoot() : this; }
Entity* GetParent() const { return m_parent.lock().get(); }
std::vector<Entity*>& GetChildren() { return m_children; }
//=========================================================================================
bool HasParent() const { return !m_parent.expired(); }
bool HasChildren() const { return GetChildrenCount() > 0 ? true : false; }
uint32_t GetChildrenCount() const { return static_cast<uint32_t>(m_children.size()); }
Entity* GetRoot() { return HasParent() ? GetParent()->GetRoot() : this; }
std::shared_ptr<Entity> GetParent() const { return m_parent.lock(); }
std::vector<Entity*>& GetChildren() { return m_children; }
//===============================================================================================

const Math::Matrix& GetMatrix() const { return m_matrix; }
const Math::Matrix& GetLocalMatrix() const { return m_matrix_local; }
Expand Down
63 changes: 33 additions & 30 deletions runtime/World/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -421,14 +421,14 @@ namespace Spartan

// remove entities using a single loop
m_entities.erase(remove_if(m_entities.begin(), m_entities.end(),
[&](const shared_ptr<Entity>& entity)
{
return ids_to_remove.count(entity->GetObjectId()) > 0;
}),
m_entities.end());
[&](const shared_ptr<Entity>& entity)
{
return ids_to_remove.count(entity->GetObjectId()) > 0;
}),
m_entities.end());

// if there was a parent, update it
if (Entity* parent = entity_to_remove->GetParent())
if (shared_ptr<Entity> parent = entity_to_remove->GetParent())
{
parent->AcquireChildren();
}
Expand Down Expand Up @@ -673,29 +673,6 @@ namespace Spartan
physics_body->GetCar()->SetSteeringWheelTransform(entity_steering_wheel);
}

// remove all the wheels since they have weird rotations, we will add our own
{
World::RemoveEntity(entity_car->GetDescendantByName("FL_Wheel_RimMaterial_0"));
World::RemoveEntity(entity_car->GetDescendantByName("FL_Wheel_Brake Disc_0"));
World::RemoveEntity(entity_car->GetDescendantByName("FL_Wheel_TireMaterial_0"));
World::RemoveEntity(entity_car->GetDescendantByName("FL_Caliper_BrakeCaliper_0"));

World::RemoveEntity(entity_car->GetDescendantByName("FR_Wheel_RimMaterial_0"));
World::RemoveEntity(entity_car->GetDescendantByName("FR_Wheel_Brake Disc_0"));
World::RemoveEntity(entity_car->GetDescendantByName("FR_Wheel_TireMaterial_0"));
World::RemoveEntity(entity_car->GetDescendantByName("FR_Caliper_BrakeCaliper_0"));

World::RemoveEntity(entity_car->GetDescendantByName("RL_Wheel_RimMaterial_0"));
World::RemoveEntity(entity_car->GetDescendantByName("RL_Wheel_Brake Disc_0"));
World::RemoveEntity(entity_car->GetDescendantByName("RL_Wheel_TireMaterial_0"));
World::RemoveEntity(entity_car->GetDescendantByName("RL_Caliper_BrakeCaliper_0"));

World::RemoveEntity(entity_car->GetDescendantByName("RR_Wheel_RimMaterial_0"));
World::RemoveEntity(entity_car->GetDescendantByName("RR_Wheel_Brake Disc_0"));
World::RemoveEntity(entity_car->GetDescendantByName("RR_Wheel_TireMaterial_0"));
World::RemoveEntity(entity_car->GetDescendantByName("RR_Caliper_BrakeCaliper_0"));
}

// load our own wheel
if (m_default_model_wheel = ResourceCache::Load<Mesh>("project\\models\\wheel\\model.blend"))
{
Expand Down Expand Up @@ -792,8 +769,34 @@ namespace Spartan
}
}

// remove all the wheels since they have weird rotations, we will add our own
{
auto entity_car = m_default_model_car->GetRootEntity().lock();

World::RemoveEntity(entity_car->GetDescendantByName("FL_Wheel_RimMaterial_0"));
World::RemoveEntity(entity_car->GetDescendantByName("FL_Wheel_Brake Disc_0"));
World::RemoveEntity(entity_car->GetDescendantByName("FL_Wheel_TireMaterial_0"));
World::RemoveEntity(entity_car->GetDescendantByName("FL_Caliper_BrakeCaliper_0"));

World::RemoveEntity(entity_car->GetDescendantByName("FR_Wheel_RimMaterial_0"));
World::RemoveEntity(entity_car->GetDescendantByName("FR_Wheel_Brake Disc_0"));
World::RemoveEntity(entity_car->GetDescendantByName("FR_Wheel_TireMaterial_0"));
World::RemoveEntity(entity_car->GetDescendantByName("FR_Caliper_BrakeCaliper_0"));

World::RemoveEntity(entity_car->GetDescendantByName("RL_Wheel_RimMaterial_0"));
World::RemoveEntity(entity_car->GetDescendantByName("RL_Wheel_Brake Disc_0"));
World::RemoveEntity(entity_car->GetDescendantByName("RL_Wheel_TireMaterial_0"));
World::RemoveEntity(entity_car->GetDescendantByName("RL_Caliper_BrakeCaliper_0"));

World::RemoveEntity(entity_car->GetDescendantByName("RR_Wheel_RimMaterial_0"));
World::RemoveEntity(entity_car->GetDescendantByName("RR_Wheel_Brake Disc_0"));
World::RemoveEntity(entity_car->GetDescendantByName("RR_Wheel_TireMaterial_0"));
World::RemoveEntity(entity_car->GetDescendantByName("RR_Caliper_BrakeCaliper_0"));

SP_LOG_INFO("Take control: Use the arrow keys to steer the car and space for handbreak!");
}

Engine::AddFlag(EngineMode::Game);
SP_LOG_INFO("Take control: Use the arrow keys to steer the car and space for handbreak!");
}

void World::CreateDefaultWorldForest()
Expand Down

0 comments on commit d3541b3

Please sign in to comment.