diff --git a/src/app.cpp b/src/app.cpp index 3bc86c1..c5a7f75 100644 --- a/src/app.cpp +++ b/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, diff --git a/src/app.hpp b/src/app.hpp index 8f284de..0e09d78 100644 --- a/src/app.hpp +++ b/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; diff --git a/src/cloth.cpp b/src/cloth.cpp index 9ec4148..c0acfee 100644 --- a/src/cloth.cpp +++ b/src/cloth.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include 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}; + } } \ No newline at end of file diff --git a/src/cloth.hpp b/src/cloth.hpp index 0ac1c85..dba4228 100644 --- a/src/cloth.hpp +++ b/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; diff --git a/src/connection.hpp b/src/connection.hpp index 86450ae..7070075 100644 --- a/src/connection.hpp +++ b/src/connection.hpp @@ -5,4 +5,7 @@ struct Connection { Point& p0; Point& p1; + float rigidity; + float initial_length; + float length; }; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 9e8ff80..13303d8 100644 --- a/src/main.cpp +++ b/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},