Browse Source

Somewhat cloth-like feel

master
Gitea 2 years ago
parent
commit
764f6dfb6d
  1. 2
      src/app.cpp
  2. 1
      src/app.hpp
  3. 45
      src/cloth.cpp
  4. 1
      src/cloth.hpp
  5. 3
      src/connection.hpp
  6. 3
      src/main.cpp

2
src/app.cpp

@ -24,7 +24,7 @@ App* app_init(App_config conf) {
}
app->conf = conf;
app->mouse = new_mouse(Vec2{0, 0}, 50);
app->mouse = new_mouse(Vec2{0, 0}, conf.cursor_radius);
app->window = new_window(conf.win_name, conf.window_dimensions);
app->cloth = new_cloth(
conf.cloth_startpos,

1
src/app.hpp

@ -15,6 +15,7 @@ struct App_config {
float efficiency_factor;
float friction_factor;
float timestep;
float cursor_radius;
RGB background_color;
RGB point_color;
RGB point_selected_color;

45
src/cloth.cpp

@ -4,6 +4,7 @@
#include <cmath>
#include <cstddef>
#include <cstdio>
#include <math.h>
#include <vector>
Cloth* new_cloth(
@ -39,13 +40,33 @@ Cloth* new_cloth(
if (x != 0) {
Point* p0_left = new_cloth->points[new_cloth->points.size() - 2];
Connection* new_conn = new Connection{*p0_left, *new_point};
float length = std::sqrt(
pow(p0_left->pos.x - new_point->pos.x, 2.0f)+
pow(p0_left->pos.y - new_point->pos.y, 2.0f)
);
Connection* new_conn = new Connection{
.p0 = *p0_left,
.p1 = *new_point,
.initial_length = length,
.length = length,
};
new_cloth->connections.push_back(new_conn);
}
if (y != 0) {
Point* p0_up = new_cloth->points[new_cloth->points.size()-1 - dimensions.x/spacing];
Connection* new_conn = new Connection{*p0_up, *new_point};
float length = std::sqrt(
pow(p0_up->pos.x - new_point->pos.x, 2.0f)+
pow(p0_up->pos.y - new_point->pos.y, 2.0f)
);
Connection* new_conn = new Connection{
.p0 = *p0_up,
.p1 = *new_point,
.initial_length = length,
.length = length,
};
new_cloth->connections.push_back(new_conn);
}
}
@ -139,4 +160,24 @@ void cloth_step(Cloth* cloth, Mouse* mouse, Vec2f gravity_vec, float timedelta)
p->pos.x += p->velocity.x;
p->pos.y += p->velocity.y;
}
for (Connection* c : cloth->connections) {
if (c->p0.frozen || c->p1.frozen) {
continue;
}
float distance = std::sqrt(
std::pow(c->p0.pos.x - c->p1.pos.x, 2.0f) +
std::pow(c->p0.pos.y - c->p1.pos.y, 2.0f)
);
float diff_factor = (c->length - distance) / distance;
Vec2f offset = {
(c->p0.pos.x - c->p1.pos.x) * diff_factor * 0.75f,
(c->p0.pos.y - c->p1.pos.y) * diff_factor * 0.75f
};
c->p0.pos = Vec2f{c->p0.pos.x + offset.x, c->p0.pos.y + offset.y};
c->p1.pos = Vec2f{c->p1.pos.x - offset.x, c->p1.pos.y - offset.y};
}
}

1
src/cloth.hpp

@ -9,6 +9,7 @@
struct Cloth {
float efficiency_factor;
float friction_factor;
float rigidity;
Vec2 startpos;
Vec2 dimensions;
Vec2 constraint_top_left;

3
src/connection.hpp

@ -5,4 +5,7 @@
struct Connection {
Point& p0;
Point& p1;
float rigidity;
float initial_length;
float length;
};

3
src/main.cpp

@ -68,9 +68,10 @@ int main(const int argc, char** argv) {
.cloth_startpos = Vec2{50, 50},
.cloth_dimensions = Vec2{400, 400},
.cloth_spacing = 20,
.gravity = Vec2f{0.65f, 0.85f},
.gravity = Vec2f{0.45f, 0.65f},
.efficiency_factor = 0.25f,
.timestep = 1.0f,
.cursor_radius = 75.0f,
.background_color = RGB{205, 222, 242},
.point_color = RGB{2, 10, 1},
.point_selected_color = RGB{200, 40, 12},

Loading…
Cancel
Save