Skip to content

Commit f4a3a20

Browse files
committed
Add tests, Sim Data
1 parent 987a796 commit f4a3a20

22 files changed

Lines changed: 1804 additions & 4 deletions

include/fzx/body/.gitkeep

Whitespace-only changes.

include/fzx/collision/.gitkeep

Whitespace-only changes.

include/fzx/common/.gitkeep

Whitespace-only changes.

include/fzx/engine/.gitkeep

Whitespace-only changes.

include/fzx/mathematics/.gitkeep

Whitespace-only changes.

include/fzx/memory/.gitkeep

Whitespace-only changes.

include/fzx/memory/AllocatorHub.h

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*********************************************************************************
2+
* FZX-library *
3+
* Copyright (c) 2025 Fahd Seddik *
4+
*********************************************************************************
5+
* *
6+
* This software is provided 'as-is', without any express or implied warranty. *
7+
* In no event will the authors be held liable for any damages arising from the *
8+
* use of this software. *
9+
* *
10+
* Permission is granted to anyone to use this software for any purpose, *
11+
* including commercial applications, and to alter it and redistribute it *
12+
* freely, subject to the following restrictions: *
13+
* *
14+
* 1. The origin of this software must not be misrepresented; you must not claim *
15+
* that you wrote the original software. If you use this software in a *
16+
* product, an acknowledgment in the product documentation would be *
17+
* appreciated but is not required. *
18+
* *
19+
* 2. Altered source versions must be plainly marked as such, and must not be *
20+
* misrepresented as being the original software. *
21+
* *
22+
* 3. This notice may not be removed or altered from any source distribution. *
23+
* *
24+
********************************************************************************/
25+
#pragma once
26+
27+
#include <fzx/memory/DefaultAllocator.h>
28+
29+
namespace fzx {
30+
31+
class AllocatorHub {
32+
public:
33+
enum class AllocationType {
34+
Default,
35+
// Add more types as needed, e.g., Pool, Stack, etc.
36+
};
37+
38+
static AllocatorHub& getInstance() {
39+
static AllocatorHub instance;
40+
return instance;
41+
}
42+
43+
void* malloc(uint64_t size, AllocationType type = AllocationType::Default) {
44+
void* ptr = nullptr;
45+
switch (type) {
46+
case AllocationType::Default:
47+
ptr = _defaultAllocator.malloc(size);
48+
break;
49+
// Add more cases for other allocators if needed
50+
}
51+
if (ptr && _initializeMemory) {
52+
std::memset(ptr, 0, size);
53+
}
54+
return ptr;
55+
}
56+
57+
void free(void* ptr, uint64_t size, AllocationType type = AllocationType::Default) {
58+
switch (type) {
59+
case AllocationType::Default:
60+
_defaultAllocator.free(ptr, size);
61+
break;
62+
// Add more cases for other allocators if needed
63+
}
64+
}
65+
66+
void setInitializeMemory(bool initialize) {
67+
_initializeMemory = initialize;
68+
}
69+
70+
DefaultAllocator& getDefaultAllocator() { return _defaultAllocator; }
71+
// Add more getters for other allocators if needed
72+
73+
private:
74+
DefaultAllocator _defaultAllocator;
75+
bool _initializeMemory;
76+
77+
AllocatorHub() : _initializeMemory(false) {}
78+
79+
~AllocatorHub() = default;
80+
};
81+
82+
} // namespace fzx

include/fzx/sim/SimConfig.h

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*********************************************************************************
2+
* FZX-library *
3+
* Copyright (c) 2025 Fahd Seddik *
4+
*********************************************************************************
5+
* *
6+
* This software is provided 'as-is', without any express or implied warranty. *
7+
* In no event will the authors be held liable for any damages arising from the *
8+
* use of this software. *
9+
* *
10+
* Permission is granted to anyone to use this software for any purpose, *
11+
* including commercial applications, and to alter it and redistribute it *
12+
* freely, subject to the following restrictions: *
13+
* *
14+
* 1. The origin of this software must not be misrepresented; you must not claim *
15+
* that you wrote the original software. If you use this software in a *
16+
* product, an acknowledgment in the product documentation would be *
17+
* appreciated but is not required. *
18+
* *
19+
* 2. Altered source versions must be plainly marked as such, and must not be *
20+
* misrepresented as being the original software. *
21+
* *
22+
* 3. This notice may not be removed or altered from any source distribution. *
23+
* *
24+
********************************************************************************/
25+
#pragma once
26+
27+
#include <fzx/mathematics/Vec3.h>
28+
29+
#include <sstream>
30+
#include <string>
31+
32+
namespace fzx {
33+
34+
/**
35+
* @brief Simulation configuration struct for FZX physics engine.
36+
*/
37+
struct SimConfig {
38+
std::string simName;
39+
Vec3 gravity;
40+
real staticFriction;
41+
real dynamicFriction;
42+
real restitution;
43+
real restitutionVelocityThreshold;
44+
bool allowSleep;
45+
real sleepLinearVelocity;
46+
real sleepAngularVelocity;
47+
int sleepSteps;
48+
real timeStep;
49+
int solverIterations;
50+
51+
// Default constructor with reasonable defaults
52+
SimConfig()
53+
: simName("FZX Simulation"),
54+
gravity(0, real(-9.81), 0),
55+
staticFriction(real(0.5)),
56+
dynamicFriction(real(0.3)),
57+
restitution(real(0.2)),
58+
restitutionVelocityThreshold(real(1.0)),
59+
allowSleep(true),
60+
sleepLinearVelocity(real(0.05)),
61+
sleepAngularVelocity(real(0.05)),
62+
sleepSteps(60),
63+
timeStep(real(1.0 / 60.0)),
64+
solverIterations(10) {}
65+
66+
// Returns a string representation of the config
67+
std::string toString() const {
68+
std::ostringstream oss;
69+
oss << "SimConfig{name=\"" << simName
70+
<< "\", gravity=(" << gravity.x << "," << gravity.y << "," << gravity.z << ")"
71+
<< ", staticFriction=" << staticFriction
72+
<< ", dynamicFriction=" << dynamicFriction
73+
<< ", restitution=" << restitution
74+
<< ", restVelThresh=" << restitutionVelocityThreshold
75+
<< ", allowSleep=" << (allowSleep ? "true" : "false")
76+
<< ", sleepLinVel=" << sleepLinearVelocity
77+
<< ", sleepAngVel=" << sleepAngularVelocity
78+
<< ", sleepSteps=" << sleepSteps
79+
<< ", timeStep=" << timeStep
80+
<< ", solverIters=" << solverIterations
81+
<< "}";
82+
return oss.str();
83+
}
84+
};
85+
86+
} // namespace fzx

include/fzx/sim/SimManager.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*********************************************************************************
2+
* FZX-library *
3+
* Copyright (c) 2025 Fahd Seddik *
4+
*********************************************************************************
5+
* *
6+
* This software is provided 'as-is', without any express or implied warranty. *
7+
* In no event will the authors be held liable for any damages arising from the *
8+
* use of this software. *
9+
* *
10+
* Permission is granted to anyone to use this software for any purpose, *
11+
* including commercial applications, and to alter it and redistribute it *
12+
* freely, subject to the following restrictions: *
13+
* *
14+
* 1. The origin of this software must not be misrepresented; you must not claim *
15+
* that you wrote the original software. If you use this software in a *
16+
* product, an acknowledgment in the product documentation would be *
17+
* appreciated but is not required. *
18+
* *
19+
* 2. Altered source versions must be plainly marked as such, and must not be *
20+
* misrepresented as being the original software. *
21+
* *
22+
* 3. This notice may not be removed or altered from any source distribution. *
23+
* *
24+
********************************************************************************/
25+
#pragma once
26+
27+
#include <fzx/common/Set.h>
28+
#include <fzx/engine/SimWorld.h>
29+
30+
namespace fzx {
31+
32+
class SimManager {
33+
private:
34+
Set<SimWorld*> _worlds;
35+
};
36+
} // namespace fzx

include/fzx/sim/SimWorld.h

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*********************************************************************************
2+
* FZX-library *
3+
* Copyright (c) 2025 Fahd Seddik *
4+
*********************************************************************************
5+
* *
6+
* This software is provided 'as-is', without any express or implied warranty. *
7+
* In no event will the authors be held liable for any damages arising from the *
8+
* use of this software. *
9+
* *
10+
* Permission is granted to anyone to use this software for any purpose, *
11+
* including commercial applications, and to alter it and redistribute it *
12+
* freely, subject to the following restrictions: *
13+
* *
14+
* 1. The origin of this software must not be misrepresented; you must not claim *
15+
* that you wrote the original software. If you use this software in a *
16+
* product, an acknowledgment in the product documentation would be *
17+
* appreciated but is not required. *
18+
* *
19+
* 2. Altered source versions must be plainly marked as such, and must not be *
20+
* misrepresented as being the original software. *
21+
* *
22+
* 3. This notice may not be removed or altered from any source distribution. *
23+
* *
24+
********************************************************************************/
25+
#pragma once
26+
27+
#include <fzx/collision/RayHitResult.h>
28+
#include <fzx/ecs/Body.h>
29+
#include <fzx/ecs/EntityPool.h>
30+
#include <fzx/engine/SimConfig.h>
31+
32+
#include <string>
33+
34+
namespace fzx {
35+
36+
// Callback types
37+
typedef bool (*OverlapCallback)(const Body* bodyA, const Body* bodyB, void* userData);
38+
typedef bool (*CollisionCallback)(const Body* bodyA, const Body* bodyB, void* userData);
39+
40+
class SimWorld {
41+
public:
42+
// Constructors
43+
SimWorld();
44+
explicit SimWorld(const SimConfig& config, const std::string& name = "SimWorld");
45+
46+
// Getters
47+
const std::string& getName() const;
48+
const SimConfig& getConfig() const;
49+
EntityPool* getEntityPool();
50+
real getTimeBeforeSleep() const;
51+
52+
// Setters
53+
void setName(const std::string& name);
54+
void setConfig(const SimConfig& config);
55+
void setTimeBeforeSleep(real time);
56+
57+
// Entity/Body management
58+
void disableBody(Body* body);
59+
60+
// Ray casting
61+
bool rayCast(const Ray& ray, RayCastCallback callback, void* userData = nullptr) const;
62+
63+
// Overlap tests
64+
bool testOverlap(const Body* bodyA, const Body* bodyB) const;
65+
bool testOverlap(const Body* body, OverlapCallback callback, void* userData = nullptr) const;
66+
67+
// Collision tests
68+
bool testCollision(const Body* bodyA, const Body* bodyB, CollisionCallback callback, void* userData = nullptr) const;
69+
bool testCollision(const Body* body, CollisionCallback callback, void* userData = nullptr) const;
70+
bool testCollision(CollisionCallback callback, void* userData = nullptr) const;
71+
72+
// Simulation update
73+
void update(real duration);
74+
75+
private:
76+
std::string _name;
77+
SimConfig _config;
78+
EntityPool* _entityPool;
79+
real _timeBeforeSleep;
80+
};
81+
82+
} // namespace fzx

0 commit comments

Comments
 (0)