implement verlet integration - much more stable
This commit is contained in:
@ -10,7 +10,7 @@ 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 SIM_SPEED = 4.0;
|
||||
constexpr float CAMERA_DISTANCE = 2.2;
|
||||
|
||||
constexpr float DEFAULT_SPRING_CONSTANT = 1.5;
|
||||
|
||||
@ -3,27 +3,34 @@
|
||||
|
||||
#include <cstddef>
|
||||
#include <raylib.h>
|
||||
#include <raymath.h>
|
||||
#include <vector>
|
||||
|
||||
class Mass {
|
||||
public:
|
||||
float mass;
|
||||
Vector3 position;
|
||||
Vector3 previous_position; // for verlet integration
|
||||
Vector3 velocity;
|
||||
Vector3 force;
|
||||
bool fixed;
|
||||
|
||||
public:
|
||||
Mass(float mass, Vector3 position, bool fixed)
|
||||
: mass(mass), position(position), fixed(fixed) {}
|
||||
: mass(mass), position(position), previous_position(position),
|
||||
velocity(Vector3Zero()), force(Vector3Zero()), fixed(fixed) {}
|
||||
|
||||
Mass(const Mass ©)
|
||||
: mass(copy.mass), position(copy.position), fixed(copy.fixed) {};
|
||||
: mass(copy.mass), position(copy.position),
|
||||
previous_position(copy.previous_position), velocity(copy.velocity),
|
||||
force(copy.force), fixed(copy.fixed) {};
|
||||
|
||||
Mass &operator=(const Mass ©) = delete;
|
||||
|
||||
Mass(Mass &&move)
|
||||
: mass(move.mass), position(move.position), fixed(move.fixed) {};
|
||||
: mass(move.mass), position(move.position),
|
||||
previous_position(move.previous_position), velocity(move.velocity),
|
||||
force(move.force), fixed(move.fixed) {};
|
||||
|
||||
Mass &operator=(Mass &&move) = delete;
|
||||
|
||||
@ -35,6 +42,8 @@ public:
|
||||
auto CalculateVelocity(const float delta_time) -> void;
|
||||
|
||||
auto CalculatePosition(const float delta_time) -> void;
|
||||
|
||||
auto VerletUpdate(const float delta_time) -> void;
|
||||
};
|
||||
|
||||
using MassList = std::vector<Mass>;
|
||||
@ -43,7 +52,6 @@ class Spring {
|
||||
public:
|
||||
Mass &massA;
|
||||
Mass &massB;
|
||||
int indexB;
|
||||
float spring_constant;
|
||||
float dampening_constant;
|
||||
float rest_length;
|
||||
@ -110,6 +118,8 @@ public:
|
||||
auto IntegrateVelocities(const float delta_time) -> void;
|
||||
|
||||
auto IntegratePositions(const float delta_time) -> void;
|
||||
|
||||
auto VerletUpdate(const float delta_time) -> void;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user