Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions include/weird-engine/Scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ namespace WeirdEngine

void get2DShapesData(WeirdRenderer::Dot2D*& data, uint32_t& size);

Scene(const Scene&) = default; // Deleted copy constructor
Scene(const Scene&) = default; // Deleted copy constructor
Scene& operator=(const Scene&) = default; // Deleted copy assignment operator
Scene(Scene&&) = default; // Defaulted move constructor
Scene& operator=(Scene&&) = default; // Defaulted move assignment operator
Scene(Scene&&) = default; // Defaulted move constructor
Scene& operator=(Scene&&) = default; // Defaulted move assignment operator

WeirdRenderer::Camera& getCamera();
std::vector<WeirdRenderer::Light>& getLigths();
Expand All @@ -39,23 +39,22 @@ namespace WeirdEngine

void fillShapeDataBuffer(WeirdRenderer::Dot2D*& data, uint32_t& size);

enum class RenderMode
enum class RenderMode
{
Simple3D,
RayMarching3D,
RayMarching2D,
RayMarchingBoth
};



RenderMode getRenderMode() const;

protected:
virtual void onCreate() {};
virtual void onStart() = 0;
virtual void onUpdate(float delta) = 0;
virtual void onRender(WeirdRenderer::RenderTarget& renderTarget) {};
virtual void onCollision(WeirdEngine::CollisionEvent& event) {};
virtual void onDestroy() {};

ECSManager m_ecs;
Expand Down Expand Up @@ -94,7 +93,6 @@ namespace WeirdEngine
void loadFont(const char* imagePath, int charWidth, int charHeight, const char* characters);

private:

// Char lookup table
std::array<int, 256> m_CharLookUpTable{};

Expand All @@ -108,9 +106,10 @@ namespace WeirdEngine

bool m_runSimulationInThread;


void updateCustomShapesShader(WeirdRenderer::Shader& shader);

std::vector<WeirdRenderer::Light> m_lights;

static void handleCollision(CollisionEvent& event, void* userData);
};
}
85 changes: 48 additions & 37 deletions include/weird-physics/Simulation2D.h
Original file line number Diff line number Diff line change
@@ -1,34 +1,30 @@
#pragma once

#include<glm/glm.hpp>

#include "../weird-engine/ecs/Entity.h"
// #include "../weird-engine/ecs/ComponentArray.h"
#include "../weird-engine/ecs/Components/Transform.h"

#include "../weird-engine/ecs/Components/CustomShape.h"

#include <vector>
#include <thread>
#include <unordered_set>
#include <unordered_map>
#include <cstdint>
#include <bitset>

#include "CollisionDetection/UniformGrid2D.h"
#include "CollisionDetection/DynamicAABBTree2D.h"

#include "CollisionDetection/SpatialHash.h"
#include "../weird-engine/Input.h"
#include "CollisionDetection/Octree.h"

#include <chrono>
#include <immintrin.h>
#include <mutex>
#include <set>

#include <glm/glm.hpp>
#include <glm/gtx/norm.hpp>

#include "../weird-engine/math/MathExpressions.h"
#include "weird-engine/ecs/Entity.h"
#include "weird-engine/ecs/Components/Transform.h"
#include "weird-engine/ecs/Components/CustomShape.h"
#include "weird-engine/Input.h"
#include "weird-engine/math/MathExpressions.h"

#include "CollisionDetection/UniformGrid2D.h"
#include "CollisionDetection/DynamicAABBTree2D.h"
#include "CollisionDetection/SpatialHash.h"
#include "CollisionDetection/Octree.h"


namespace WeirdEngine
{
Expand All @@ -39,22 +35,23 @@ namespace WeirdEngine

using vec2 = glm::vec2;


class CustomBitset
{
public:
CustomBitset(size_t size) : bits((size + 63) / 64, 0), size(size) {}

void set(SimulationID pos)
{
if (pos < size) {
if (pos < size)
{
bits[pos / 64] |= (1ULL << (pos % 64));
}
}

void clear(SimulationID pos)
{
if (pos < size) {
if (pos < size)
{
bits[pos / 64] &= ~(1ULL << (pos % 64));
}
}
Expand All @@ -73,13 +70,19 @@ namespace WeirdEngine
size_t size;
};

struct CollisionEvent
{
SimulationID bodyA;
SimulationID bodyB;
// bool firstContact; TODO
};


// Define the function pointer type and include a user data pointer
using CollisionCallbackFn = void (*)(CollisionEvent&, void*);

class Simulation2D
{


public:
Simulation2D(size_t size);
~Simulation2D();
Expand All @@ -92,7 +95,6 @@ namespace WeirdEngine
void startSimulationThread();
void stopSimulationThread();


void update(double delta);

double getSimulationTime();
Expand All @@ -117,7 +119,6 @@ namespace WeirdEngine
void updateTransform(Transform& transform, SimulationID id);
void setMass(SimulationID id, float mass);


void setSDFs(std::vector<std::shared_ptr<IMathExpression>>& sdfs);

void updateShape(CustomShape& shape);
Expand All @@ -136,30 +137,30 @@ namespace WeirdEngine
void solveConstraints();
void step(float timeStep);


struct Collision
{
public:
Collision() {
Collision()
{
A = -1;
B = -1;
AB = vec2();
}


Collision(int a, int b, vec2 ab) {
Collision(SimulationID a, SimulationID b, vec2 ab)
{
A = a;
B = b;
AB = ab;
}


bool operator==(const Collision& other) const {
bool operator==(const Collision& other) const
{
return (A == other.A && B == other.B) || (A == other.B && B == other.A);
}

int A;
int B;
SimulationID A;
SimulationID B;
vec2 AB;
};

Expand Down Expand Up @@ -199,7 +200,6 @@ namespace WeirdEngine
Distance = 1.0f;
}


DistanceConstraint(int a, int b, float distance)
{
A = a;
Expand All @@ -222,7 +222,6 @@ namespace WeirdEngine
g = 1.0f;
}


GravitationalConstraint(int a, int b, float gravity)
{
A = a;
Expand All @@ -235,8 +234,10 @@ namespace WeirdEngine
float g;
};

struct CollisionHash {
std::size_t operator()(const Collision& s) const {
struct CollisionHash
{
std::size_t operator()(const Collision& s) const
{
bool flip = s.A < s.B;
int first = flip ? s.B : s.A;
int last = flip ? s.A : s.B;
Expand All @@ -251,7 +252,6 @@ namespace WeirdEngine
MethodTree
};


struct DistanceFieldObject2D
{
Entity owner;
Expand Down Expand Up @@ -325,6 +325,17 @@ namespace WeirdEngine
bool m_attracttionEnabled = false;
bool m_repulsionEnabled = false;
bool m_liftEnabled = false;

private:
CollisionCallbackFn m_collisionCallback = nullptr;
void* m_callbackUserData = nullptr;

public:
void setCollisionCallback(CollisionCallbackFn callback, void* userData)
{
m_collisionCallback = callback;
m_callbackUserData = userData;
}
};

}
26 changes: 12 additions & 14 deletions src/weird-engine/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,21 @@
#include <stb/stb_image.h>
#include <stb/stb_image_write.h>


namespace WeirdEngine
{

void Scene::handleCollision(CollisionEvent &event, void *userData)
{
// Unsafe cast! Prone to error.
Scene* self = static_cast<Scene*>(userData);
self->onCollision(event);
}

// vec3 g_cameraPosition(15.0f, 50.f, 60.0f);
vec3 g_cameraPosition(0, 1, 20);

Scene::Scene()
: m_simulation2D(MAX_ENTITIES)
, m_sdfRenderSystem(m_ecs)
, m_sdfRenderSystem2D(m_ecs)
, m_renderSystem(m_ecs)
, m_instancedRenderSystem(m_ecs)
, m_rbPhysicsSystem2D(m_ecs)
, m_physicsInteractionSystem(m_ecs)
, m_playerMovementSystem(m_ecs)
, m_cameraSystem(m_ecs)
, m_runSimulationInThread(true)
: m_simulation2D(MAX_ENTITIES), m_sdfRenderSystem(m_ecs), m_sdfRenderSystem2D(m_ecs), m_renderSystem(m_ecs), m_instancedRenderSystem(m_ecs), m_rbPhysicsSystem2D(m_ecs), m_physicsInteractionSystem(m_ecs), m_playerMovementSystem(m_ecs), m_cameraSystem(m_ecs), m_runSimulationInThread(true)
{

// Custom component managers
Expand All @@ -44,6 +42,8 @@ namespace WeirdEngine
{
m_simulation2D.startSimulationThread();
}

m_simulation2D.setCollisionCallback(&handleCollision, this);
}

Scene::~Scene()
Expand Down Expand Up @@ -73,15 +73,13 @@ namespace WeirdEngine
case WeirdEngine::Scene::RenderMode::RayMarching2D:
{
FlyMovement2D& fly = m_ecs.addComponent<FlyMovement2D>(m_mainCamera);
fly.targetPosition = g_cameraPosition;
// fly.targetPosition = g_cameraPosition;
break;
}
default:
break;
}
}


}

// TODO: pass render target instead of shader. Shaders should be accessed in a different way, through the resource manager
Expand Down
Loading