implement numerically unstable mass spring system
This commit is contained in:
20
include/config.hpp
Normal file
20
include/config.hpp
Normal file
@ -0,0 +1,20 @@
|
||||
#ifndef __CONFIG_HPP_
|
||||
#define __CONFIG_HPP_
|
||||
|
||||
#include <raylib.h>
|
||||
|
||||
constexpr int WIDTH = 800;
|
||||
constexpr int HEIGHT = 800;
|
||||
|
||||
constexpr float VERTEX_SIZE = 5.0;
|
||||
constexpr Color VERTEX_COLOR = {27, 188, 104, 255};
|
||||
constexpr Color EDGE_COLOR = {20, 133, 38, 255};
|
||||
|
||||
constexpr float SIM_SPEED = 2.0;
|
||||
constexpr float CAMERA_DISTANCE = 2.2;
|
||||
|
||||
constexpr float DEFAULT_SPRING_CONSTANT = 1.5;
|
||||
constexpr float DEFAULT_DAMPENING_CONSTANT = 0.1;
|
||||
constexpr float DEFAULT_REST_LENGTH = 0.5;
|
||||
|
||||
#endif
|
||||
115
include/mass_springs.hpp
Normal file
115
include/mass_springs.hpp
Normal file
@ -0,0 +1,115 @@
|
||||
#ifndef __MASS_SPRINGS_HPP_
|
||||
#define __MASS_SPRINGS_HPP_
|
||||
|
||||
#include <cstddef>
|
||||
#include <raylib.h>
|
||||
#include <vector>
|
||||
|
||||
class Mass {
|
||||
public:
|
||||
float mass;
|
||||
Vector3 position;
|
||||
Vector3 velocity;
|
||||
Vector3 force;
|
||||
bool fixed;
|
||||
|
||||
public:
|
||||
Mass(float mass, Vector3 position, bool fixed)
|
||||
: mass(mass), position(position), fixed(fixed) {}
|
||||
|
||||
Mass(const Mass ©)
|
||||
: mass(copy.mass), position(copy.position), fixed(copy.fixed) {};
|
||||
|
||||
Mass &operator=(const Mass ©) = delete;
|
||||
|
||||
Mass(Mass &&move)
|
||||
: mass(move.mass), position(move.position), fixed(move.fixed) {};
|
||||
|
||||
Mass &operator=(Mass &&move) = delete;
|
||||
|
||||
~Mass() {}
|
||||
|
||||
public:
|
||||
auto ClearForce() -> void;
|
||||
|
||||
auto CalculateVelocity(const float delta_time) -> void;
|
||||
|
||||
auto CalculatePosition(const float delta_time) -> void;
|
||||
};
|
||||
|
||||
using MassList = std::vector<Mass>;
|
||||
|
||||
class Spring {
|
||||
public:
|
||||
Mass &massA;
|
||||
Mass &massB;
|
||||
int indexB;
|
||||
float spring_constant;
|
||||
float dampening_constant;
|
||||
float rest_length;
|
||||
|
||||
public:
|
||||
Spring(Mass &massA, Mass &massB, float spring_constant,
|
||||
float dampening_constant, float rest_length)
|
||||
: massA(massA), massB(massB), spring_constant(spring_constant),
|
||||
dampening_constant(dampening_constant), rest_length(rest_length) {}
|
||||
|
||||
Spring(const Spring ©)
|
||||
: massA(copy.massA), massB(copy.massB),
|
||||
spring_constant(copy.spring_constant),
|
||||
dampening_constant(copy.dampening_constant),
|
||||
rest_length(copy.rest_length) {};
|
||||
|
||||
Spring &operator=(const Spring ©) = delete;
|
||||
|
||||
Spring(Spring &&move)
|
||||
: massA(move.massA), massB(move.massB),
|
||||
spring_constant(move.spring_constant),
|
||||
dampening_constant(move.dampening_constant),
|
||||
rest_length(move.rest_length) {}
|
||||
|
||||
Spring &operator=(Spring &&move) = delete;
|
||||
|
||||
~Spring() {}
|
||||
|
||||
public:
|
||||
auto CalculateSpringForce() -> void;
|
||||
};
|
||||
|
||||
using SpringList = std::vector<Spring>;
|
||||
|
||||
class MassSpringSystem {
|
||||
public:
|
||||
MassList masses;
|
||||
SpringList springs;
|
||||
|
||||
public:
|
||||
MassSpringSystem() {};
|
||||
|
||||
MassSpringSystem(const MassSpringSystem ©) = delete;
|
||||
MassSpringSystem &operator=(const MassSpringSystem ©) = delete;
|
||||
MassSpringSystem(MassSpringSystem &move) = delete;
|
||||
MassSpringSystem &operator=(MassSpringSystem &&move) = delete;
|
||||
|
||||
~MassSpringSystem() {};
|
||||
|
||||
public:
|
||||
auto AddMass(float mass, Vector3 position, bool fixed) -> void;
|
||||
|
||||
auto GetMass(const size_t index) -> Mass &;
|
||||
|
||||
auto AddSpring(int massA, int massB, float spring_constant,
|
||||
float dampening_constant, float rest_length) -> void;
|
||||
|
||||
auto GetSpring(const size_t index) -> Spring &;
|
||||
|
||||
auto ClearForces() -> void;
|
||||
|
||||
auto CalculateSpringForces() -> void;
|
||||
|
||||
auto IntegrateVelocities(const float delta_time) -> void;
|
||||
|
||||
auto IntegratePositions(const float delta_time) -> void;
|
||||
};
|
||||
|
||||
#endif
|
||||
49
include/renderer.hpp
Normal file
49
include/renderer.hpp
Normal file
@ -0,0 +1,49 @@
|
||||
#ifndef __RENDERER_HPP_
|
||||
#define __RENDERER_HPP_
|
||||
|
||||
#include <immintrin.h>
|
||||
#include <raylib.h>
|
||||
#include <raymath.h>
|
||||
#include <vector>
|
||||
|
||||
#include "mass_springs.hpp"
|
||||
|
||||
using Edge2Set = std::vector<std::pair<Vector2, Vector2>>;
|
||||
using Edge3Set = std::vector<std::pair<Vector3, Vector3>>;
|
||||
|
||||
class Renderer {
|
||||
private:
|
||||
int width;
|
||||
int height;
|
||||
RenderTexture2D render_target;
|
||||
|
||||
public:
|
||||
Renderer(int width, int height) : width(width), height(height) {
|
||||
render_target = LoadRenderTexture(width, height);
|
||||
}
|
||||
|
||||
Renderer(const Renderer ©) = delete;
|
||||
Renderer &operator=(const Renderer ©) = delete;
|
||||
Renderer(Renderer &&move) = delete;
|
||||
Renderer &operator=(Renderer &&move) = delete;
|
||||
|
||||
~Renderer() { UnloadRenderTexture(render_target); }
|
||||
|
||||
private:
|
||||
auto Rotate(const Vector3 &a, const float cos_angle, const float sin_angle)
|
||||
-> Vector3;
|
||||
|
||||
auto Translate(const Vector3 &a, const float distance) -> Vector3;
|
||||
|
||||
auto Project(const Vector3 &a) -> Vector2;
|
||||
|
||||
auto Map(const Vector2 &a) -> Vector2;
|
||||
|
||||
public:
|
||||
auto Transform(Edge2Set &edges, const MassSpringSystem &mass_springs,
|
||||
const float angle, const float distance) -> void;
|
||||
|
||||
auto Draw(const Edge2Set &edges) -> void;
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user