Skip to content

Commit

Permalink
[Car] Fixed an issue related to entity cloning and updated the song t…
Browse files Browse the repository at this point in the history
…o something cooler
  • Loading branch information
PanosK92 committed Nov 29, 2023
1 parent f1d9e1a commit f7665f5
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 24 deletions.
Binary file added assets/music/car.mp3
Binary file not shown.
Binary file removed assets/music/riders_on_the_storm_fredwreck_remix.mp3
Binary file not shown.
31 changes: 18 additions & 13 deletions runtime/World/Components/Terrain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,18 @@ namespace Spartan
}
}

// bytes per pixel
uint32_t bytes_per_pixel = (height_texture->GetChannelCount() * height_texture->GetBitsPerChannel()) / 8;

// normalize and scale height data
height_data_out.resize(height_data.size() / bytes_per_pixel);
for (uint32_t i = 0; i < height_data.size(); i += bytes_per_pixel)
// read from the red channel and save a normalized height value
{
// assuming the height is stored in the red channel (first channel)
height_data_out[i / bytes_per_pixel] = min_y + (static_cast<float>(height_data[i]) / 255.0f) * (max_y - min_y);
// bytes per pixel
uint32_t bytes_per_pixel = (height_texture->GetChannelCount() * height_texture->GetBitsPerChannel()) / 8;

// normalize and scale height data
height_data_out.resize(height_data.size() / bytes_per_pixel);
for (uint32_t i = 0; i < height_data.size(); i += bytes_per_pixel)
{
// assuming the height is stored in the red channel (first channel)
height_data_out[i / bytes_per_pixel] = min_y + (static_cast<float>(height_data[i]) / 255.0f) * (max_y - min_y);
}
}

// smooth out the height map values, this will reduce hard terrain edges
Expand All @@ -76,13 +79,13 @@ namespace Spartan
const uint32_t width = height_texture->GetWidth();
const uint32_t height = height_texture->GetHeight();

for (uint32_t iteration = 0; iteration < smoothing_iterations; ++iteration)
for (uint32_t iteration = 0; iteration < smoothing_iterations; iteration++)
{
vector<float> smoothed_height_data = height_data_out; // create a copy to store the smoothed data

for (uint32_t y = 0; y < height; ++y)
for (uint32_t y = 0; y < height; y++)
{
for (uint32_t x = 0; x < width; ++x)
for (uint32_t x = 0; x < width; x++)
{
float sum = height_data_out[y * width + x];
uint32_t count = 1;
Expand All @@ -92,7 +95,9 @@ namespace Spartan
{
for (int nx = -1; nx <= 1; ++nx)
{
if (nx == 0 && ny == 0) continue; // skip the center pixel
// skip self/center pixel
if (nx == 0 && ny == 0)
continue;

uint32_t neighbor_x = x + nx;
uint32_t neighbor_y = y + ny;
Expand All @@ -101,7 +106,7 @@ namespace Spartan
if (neighbor_x >= 0 && neighbor_x < width && neighbor_y >= 0 && neighbor_y < height)
{
sum += height_data_out[neighbor_y * width + neighbor_x];
++count;
count++;
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions runtime/World/Entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ namespace Spartan
clone->SetObjectName(entity->GetObjectName());
clone->SetActive(entity->IsActive());
clone->SetHierarchyVisibility(entity->IsVisibleInHierarchy());
clone->SetPosition(entity->GetPosition());
clone->SetRotation(entity->GetRotation());
clone->SetScale(entity->GetScale());
clone->SetPosition(entity->GetPositionLocal());
clone->SetRotation(entity->GetRotationLocal());
clone->SetScale(entity->GetScaleLocal());

// clone all the components
for (shared_ptr<Component> component_original : entity->GetAllComponents())
Expand Down
16 changes: 8 additions & 8 deletions runtime/World/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -633,29 +633,29 @@ namespace Spartan

lock_guard<mutex> lock(m_entity_access_mutex);

// Remove the entity and all of its children
// remove the entity and all of its children
{
// Get the root entity and its descendants
// get the root entity and its descendants
vector<Entity*> entities_to_remove;
entities_to_remove.push_back(entity_to_remove); // Add the root entity
entity_to_remove->GetDescendants(&entities_to_remove); // Get descendants
entities_to_remove.push_back(entity_to_remove); // add the root entity
entity_to_remove->GetDescendants(&entities_to_remove); // get descendants

// Create a set containing the object IDs of entities to remove
// create a set containing the object IDs of entities to remove
set<uint64_t> ids_to_remove;
for (Entity* entity : entities_to_remove)
{
ids_to_remove.insert(entity->GetObjectId());
}

// Remove entities using a single loop
// 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());

// If there was a parent, update it
// if there was a parent, update it
if (Entity* parent = entity_to_remove->GetParent())
{
parent->AcquireChildren();
Expand Down Expand Up @@ -786,7 +786,7 @@ namespace Spartan
{
Vector3 camera_position = Vector3(8.7844f, 2.0f, -4.1412f);
Vector3 camera_rotation = Vector3(7.4f, -65.5f, 0.0f);
create_default_world_common(camera_position, camera_rotation, LightIntensity::sky_sunlight_noon, "project\\music\\riders_on_the_storm_fredwreck_remix.mp3");
create_default_world_common(camera_position, camera_rotation, LightIntensity::sky_sunlight_noon, "project\\music\\car.mp3");
create_default_car();

Engine::AddFlag(EngineMode::Game);
Expand Down

0 comments on commit f7665f5

Please sign in to comment.