mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2025-12-21 11:59:37 -06:00
Compare commits
2 Commits
dpGrid_spe
...
physicsSim
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4d6f11fed1 | ||
|
|
0bbc8e6643 |
@@ -156,83 +156,42 @@ PhantomPhysicsComponent::PhantomPhysicsComponent(Entity* parent) : PhysicsCompon
|
||||
//temp test
|
||||
if (info->physicsAsset == "miscellaneous\\misc_phys_10x1x5.hkx") {
|
||||
m_dpEntity = new dpEntity(m_Parent->GetObjectID(), 10.0f, 5.0f, 1.0f);
|
||||
|
||||
m_dpEntity->SetScale(m_Scale);
|
||||
m_dpEntity->SetRotation(m_Rotation);
|
||||
m_dpEntity->SetPosition(m_Position);
|
||||
|
||||
dpWorld::AddEntity(m_dpEntity);
|
||||
} else if (info->physicsAsset == "miscellaneous\\misc_phys_640x640.hkx") {
|
||||
// Move this down by 13.521004 units so it is still effectively at the same height as before
|
||||
m_Position = m_Position - NiPoint3Constant::UNIT_Y * 13.521004f;
|
||||
// TODO Fix physics simulation to do simulation at high velocities due to bullet through paper problem...
|
||||
m_dpEntity = new dpEntity(m_Parent->GetObjectID(), 1638.4f, 13.521004f * 2.0f, 1638.4f);
|
||||
|
||||
m_dpEntity->SetScale(m_Scale);
|
||||
m_dpEntity->SetRotation(m_Rotation);
|
||||
m_dpEntity->SetPosition(m_Position);
|
||||
|
||||
dpWorld::AddEntity(m_dpEntity);
|
||||
// Move this down by 13.521004 units so it is still effectively at the same height as before
|
||||
m_Position = m_Position - NiPoint3Constant::UNIT_Y * 13.521004f;
|
||||
} else if (info->physicsAsset == "env\\trigger_wall_tall.hkx") {
|
||||
m_dpEntity = new dpEntity(m_Parent->GetObjectID(), 10.0f, 25.0f, 1.0f);
|
||||
m_dpEntity->SetScale(m_Scale);
|
||||
m_dpEntity->SetRotation(m_Rotation);
|
||||
m_dpEntity->SetPosition(m_Position);
|
||||
dpWorld::AddEntity(m_dpEntity);
|
||||
} else if (info->physicsAsset == "env\\env_gen_placeholderphysics.hkx") {
|
||||
m_dpEntity = new dpEntity(m_Parent->GetObjectID(), 20.0f, 20.0f, 20.0f);
|
||||
m_dpEntity->SetScale(m_Scale);
|
||||
m_dpEntity->SetRotation(m_Rotation);
|
||||
m_dpEntity->SetPosition(m_Position);
|
||||
dpWorld::AddEntity(m_dpEntity);
|
||||
} else if (info->physicsAsset == "env\\POI_trigger_wall.hkx") {
|
||||
m_dpEntity = new dpEntity(m_Parent->GetObjectID(), 1.0f, 12.5f, 20.0f); // Not sure what the real size is
|
||||
m_dpEntity->SetScale(m_Scale);
|
||||
m_dpEntity->SetRotation(m_Rotation);
|
||||
m_dpEntity->SetPosition(m_Position);
|
||||
dpWorld::AddEntity(m_dpEntity);
|
||||
} else if (info->physicsAsset == "env\\NG_NinjaGo\\env_ng_gen_gate_chamber_puzzle_ceiling_tile_falling_phantom.hkx") {
|
||||
m_dpEntity = new dpEntity(m_Parent->GetObjectID(), 18.0f, 5.0f, 15.0f);
|
||||
m_dpEntity->SetScale(m_Scale);
|
||||
m_dpEntity->SetRotation(m_Rotation);
|
||||
m_dpEntity->SetPosition(m_Position + m_Rotation.GetForwardVector() * 7.5f);
|
||||
dpWorld::AddEntity(m_dpEntity);
|
||||
m_Position += m_Rotation.GetForwardVector() * 7.5f;
|
||||
} else if (info->physicsAsset == "env\\NG_NinjaGo\\ng_flamejet_brick_phantom.HKX") {
|
||||
m_dpEntity = new dpEntity(m_Parent->GetObjectID(), 1.0f, 1.0f, 12.0f);
|
||||
m_dpEntity->SetScale(m_Scale);
|
||||
m_dpEntity->SetRotation(m_Rotation);
|
||||
m_dpEntity->SetPosition(m_Position + m_Rotation.GetForwardVector() * 6.0f);
|
||||
dpWorld::AddEntity(m_dpEntity);
|
||||
m_Position += m_Rotation.GetForwardVector() * 6.0f;
|
||||
} else if (info->physicsAsset == "env\\Ring_Trigger.hkx") {
|
||||
m_dpEntity = new dpEntity(m_Parent->GetObjectID(), 6.0f, 6.0f, 6.0f);
|
||||
m_dpEntity->SetScale(m_Scale);
|
||||
m_dpEntity->SetRotation(m_Rotation);
|
||||
m_dpEntity->SetPosition(m_Position);
|
||||
dpWorld::AddEntity(m_dpEntity);
|
||||
} else if (info->physicsAsset == "env\\vfx_propertyImaginationBall.hkx") {
|
||||
m_dpEntity = new dpEntity(m_Parent->GetObjectID(), 4.5f);
|
||||
m_dpEntity->SetScale(m_Scale);
|
||||
m_dpEntity->SetRotation(m_Rotation);
|
||||
m_dpEntity->SetPosition(m_Position);
|
||||
dpWorld::AddEntity(m_dpEntity);
|
||||
} else if (info->physicsAsset == "env\\env_won_fv_gas-blocking-volume.hkx") {
|
||||
m_dpEntity = new dpEntity(m_Parent->GetObjectID(), 390.496826f, 111.467964f, 600.821534f, true);
|
||||
m_dpEntity->SetScale(m_Scale);
|
||||
m_dpEntity->SetRotation(m_Rotation);
|
||||
m_Position.y -= (111.467964f * m_Scale) / 2;
|
||||
m_dpEntity->SetPosition(m_Position);
|
||||
dpWorld::AddEntity(m_dpEntity);
|
||||
} else {
|
||||
//LOG("This one is supposed to have %s", info->physicsAsset.c_str());
|
||||
// LOG_DEBUG("This one is supposed to have %s", info->physicsAsset.c_str());
|
||||
|
||||
//add fallback cube:
|
||||
m_dpEntity = new dpEntity(m_Parent->GetObjectID(), 2.0f, 2.0f, 2.0f);
|
||||
m_dpEntity->SetScale(m_Scale);
|
||||
m_dpEntity->SetRotation(m_Rotation);
|
||||
m_dpEntity->SetPosition(m_Position);
|
||||
dpWorld::AddEntity(m_dpEntity);
|
||||
}
|
||||
|
||||
|
||||
m_dpEntity->SetScale(m_Scale);
|
||||
m_dpEntity->SetRotation(m_Rotation);
|
||||
m_dpEntity->SetPosition(m_Position);
|
||||
dpWorld::AddEntity(m_dpEntity);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -76,7 +76,7 @@ void dpEntity::CheckCollision(dpEntity* other) {
|
||||
return;
|
||||
}
|
||||
|
||||
bool wasFound = m_CurrentlyCollidingObjects.contains(other->GetObjectID());
|
||||
bool wasFound = (m_CurrentlyCollidingObjects.find(other->GetObjectID()) != m_CurrentlyCollidingObjects.end());
|
||||
|
||||
bool isColliding = m_CollisionShape->IsColliding(other->GetShape());
|
||||
|
||||
|
||||
@@ -8,14 +8,24 @@ dpGrid::dpGrid(int numCells, int cellSize) {
|
||||
CELL_SIZE = cellSize;
|
||||
m_DeleteGrid = true;
|
||||
|
||||
m_Cells.resize(NUM_CELLS, std::vector<std::vector<dpEntity*>>(NUM_CELLS));
|
||||
//fill x
|
||||
for (int i = 0; i < NUM_CELLS; i++) {
|
||||
m_Cells.push_back(std::vector<std::forward_list<dpEntity*>>());
|
||||
}
|
||||
|
||||
//fill z
|
||||
for (int i = 0; i < NUM_CELLS; i++) {
|
||||
for (int i = 0; i < NUM_CELLS; i++) {
|
||||
m_Cells[i].push_back(std::forward_list<dpEntity*>());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dpGrid::~dpGrid() {
|
||||
if (!this->m_DeleteGrid) return;
|
||||
for (auto& x : m_Cells) { //x
|
||||
for (auto& z : x) { //y
|
||||
for (auto en : z) {
|
||||
for (auto& y : x) { //y
|
||||
for (auto en : y) {
|
||||
if (!en) continue;
|
||||
delete en;
|
||||
en = nullptr;
|
||||
@@ -29,12 +39,13 @@ void dpGrid::Add(dpEntity* entity) {
|
||||
int cellX = (int)std::round(entity->m_Position.x) / dpGrid::CELL_SIZE + NUM_CELLS / 2;
|
||||
int cellZ = (int)std::round(entity->m_Position.z) / dpGrid::CELL_SIZE + NUM_CELLS / 2;
|
||||
|
||||
// Clamp values to the range [0, NUM_CELLS - 1]
|
||||
cellX = std::clamp(cellX, 0, NUM_CELLS - 1);
|
||||
cellZ = std::clamp(cellZ, 0, NUM_CELLS - 1);
|
||||
if (cellX < 0) cellX = 0;
|
||||
if (cellZ < 0) cellZ = 0;
|
||||
if (cellX >= NUM_CELLS) cellX = NUM_CELLS - 1;
|
||||
if (cellZ >= NUM_CELLS) cellZ = NUM_CELLS - 1;
|
||||
|
||||
//Add to cell:
|
||||
m_Cells[cellX][cellZ].push_back(entity);
|
||||
m_Cells[cellX][cellZ].push_front(entity);
|
||||
|
||||
//To verify that the object isn't gargantuan:
|
||||
if (entity->GetScale() >= CELL_SIZE * 2 || entity->GetIsGargantuan())
|
||||
@@ -48,27 +59,23 @@ void dpGrid::Move(dpEntity* entity, float x, float z) {
|
||||
int cellX = (int)std::round(x) / dpGrid::CELL_SIZE + NUM_CELLS / 2;
|
||||
int cellZ = (int)std::round(z) / dpGrid::CELL_SIZE + NUM_CELLS / 2;
|
||||
|
||||
// Clamp values to the range [0, NUM_CELLS - 1]
|
||||
cellX = std::clamp(cellX, 0, NUM_CELLS - 1);
|
||||
cellZ = std::clamp(cellZ, 0, NUM_CELLS - 1);
|
||||
if (cellX < 0) cellX = 0;
|
||||
if (cellZ < 0) cellZ = 0;
|
||||
if (cellX >= NUM_CELLS) cellX = NUM_CELLS - 1;
|
||||
if (cellZ >= NUM_CELLS) cellZ = NUM_CELLS - 1;
|
||||
|
||||
oldCellX = std::clamp(oldCellX, 0, NUM_CELLS - 1);
|
||||
oldCellZ = std::clamp(oldCellZ, 0, NUM_CELLS - 1);
|
||||
if (oldCellX < 0) oldCellX = 0;
|
||||
if (oldCellZ < 0) oldCellZ = 0;
|
||||
if (oldCellX >= NUM_CELLS) oldCellX = NUM_CELLS - 1;
|
||||
if (oldCellZ >= NUM_CELLS) oldCellZ = NUM_CELLS - 1;
|
||||
|
||||
if (oldCellX == cellX && oldCellZ == cellZ) return;
|
||||
|
||||
//Remove from prev cell:
|
||||
auto& cell = m_Cells[oldCellX][oldCellZ];
|
||||
|
||||
// For speed, find the single match and swap it with the last element, then pop_back.
|
||||
auto toRemove = std::find(cell.begin(), cell.end(), entity);
|
||||
if (toRemove != cell.end()) {
|
||||
*toRemove = cell.back();
|
||||
cell.pop_back();
|
||||
}
|
||||
//Remove from perv cell:
|
||||
m_Cells[oldCellX][oldCellZ].remove(entity);
|
||||
|
||||
//Add to the new cell
|
||||
m_Cells[cellX][cellZ].push_back(entity);
|
||||
m_Cells[cellX][cellZ].push_front(entity);
|
||||
}
|
||||
|
||||
void dpGrid::Delete(dpEntity* entity) {
|
||||
@@ -76,18 +83,15 @@ void dpGrid::Delete(dpEntity* entity) {
|
||||
int oldCellX = (int)std::round(entity->m_Position.x) / dpGrid::CELL_SIZE + NUM_CELLS / 2;
|
||||
int oldCellZ = (int)std::round(entity->m_Position.z) / dpGrid::CELL_SIZE + NUM_CELLS / 2;
|
||||
|
||||
// Clamp values to the range [0, NUM_CELLS - 1]
|
||||
oldCellX = std::clamp(oldCellX, 0, NUM_CELLS - 1);
|
||||
oldCellZ = std::clamp(oldCellZ, 0, NUM_CELLS - 1);
|
||||
if (oldCellX < 0) oldCellX = 0;
|
||||
if (oldCellZ < 0) oldCellZ = 0;
|
||||
if (oldCellX >= NUM_CELLS) oldCellX = NUM_CELLS - 1;
|
||||
if (oldCellZ >= NUM_CELLS) oldCellZ = NUM_CELLS - 1;
|
||||
|
||||
auto& cell = m_Cells[oldCellX][oldCellZ];
|
||||
auto toRemove = std::find(cell.begin(), cell.end(), entity);
|
||||
if (toRemove != cell.end()) {
|
||||
*toRemove = cell.back();
|
||||
cell.pop_back();
|
||||
}
|
||||
m_Cells[oldCellX][oldCellZ].remove(entity);
|
||||
|
||||
m_GargantuanObjects.erase(entity->m_ObjectID);
|
||||
if (m_GargantuanObjects.find(entity->m_ObjectID) != m_GargantuanObjects.end())
|
||||
m_GargantuanObjects.erase(entity->m_ObjectID);
|
||||
|
||||
if (entity) delete entity;
|
||||
entity = nullptr;
|
||||
@@ -96,8 +100,8 @@ void dpGrid::Delete(dpEntity* entity) {
|
||||
void dpGrid::Update(float deltaTime) {
|
||||
//Pre-update:
|
||||
for (auto& x : m_Cells) { //x
|
||||
for (auto& z : x) { //y
|
||||
for (auto en : z) {
|
||||
for (auto& y : x) { //y
|
||||
for (auto en : y) {
|
||||
if (!en) continue;
|
||||
en->PreUpdate();
|
||||
}
|
||||
@@ -106,8 +110,8 @@ void dpGrid::Update(float deltaTime) {
|
||||
|
||||
//Actual collision detection update:
|
||||
for (int x = 0; x < NUM_CELLS; x++) {
|
||||
for (int z = 0; z < NUM_CELLS; z++) {
|
||||
HandleCell(x, z, deltaTime);
|
||||
for (int y = 0; y < NUM_CELLS; y++) {
|
||||
HandleCell(x, y, deltaTime);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -153,7 +157,7 @@ void dpGrid::HandleCell(int x, int z, float deltaTime) {
|
||||
HandleEntity(en, other);
|
||||
}
|
||||
|
||||
for (auto& [id, entity] : m_GargantuanObjects)
|
||||
HandleEntity(en, entity);
|
||||
for (auto other : m_GargantuanObjects)
|
||||
HandleEntity(en, other.second);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
#pragma once
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
#include <list>
|
||||
#include <forward_list>
|
||||
#include <map>
|
||||
#include "dCommonVars.h"
|
||||
|
||||
class dpEntity;
|
||||
@@ -29,8 +30,7 @@ public:
|
||||
*/
|
||||
void SetDeleteGrid(bool value) { this->m_DeleteGrid = value; };
|
||||
|
||||
// Intentional copy since this is only used when we delete this class to re-create it.
|
||||
std::vector<std::vector<std::vector<dpEntity*>>> GetCells() { return this->m_Cells; };
|
||||
std::vector<std::vector<std::forward_list<dpEntity*>>> GetCells() { return this->m_Cells; };
|
||||
|
||||
private:
|
||||
void HandleEntity(dpEntity* entity, dpEntity* other);
|
||||
@@ -38,7 +38,7 @@ private:
|
||||
|
||||
private:
|
||||
//cells on X, cells on Y for that X, then another vector that contains the entities within that cell.
|
||||
std::vector<std::vector<std::vector<dpEntity*>>> m_Cells;
|
||||
std::vector<std::vector<std::forward_list<dpEntity*>>> m_Cells;
|
||||
std::map<LWOOBJID, dpEntity*> m_GargantuanObjects;
|
||||
bool m_DeleteGrid = true;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user