Browse Source

Constraints

master
Gitea 3 years ago
parent
commit
6403b87fb0
  1. 2
      .gitignore
  2. 7
      src/app.cpp
  3. 47
      src/cloth.cpp
  4. 3
      src/cloth.hpp
  5. 2
      src/main.cpp
  6. 7
      src/point.hpp

2
.gitignore vendored

@ -1 +1 @@
./cloth_sim cloth_sim

7
src/app.cpp

@ -68,14 +68,13 @@ void app_update(App* app) {
return; return;
} }
compute_cloth_forces(app->cloth, app->conf.gravity);
app->conf.gravity = Vec2f{app->conf.gravity.x-0.0005f, app->conf.gravity.y-0.001f};
int updated_window_w; int updated_window_w;
int updated_window_h; int updated_window_h;
SDL_GetWindowSize(app->window->sdl_win, &updated_window_w, &updated_window_h); SDL_GetWindowSize(app->window->sdl_win, &updated_window_w, &updated_window_h);
app->window->dimensions = Vec2{(unsigned int) updated_window_w, (unsigned int) updated_window_h}; app->window->dimensions = Vec2{(unsigned int) updated_window_w, (unsigned int) updated_window_h};
compute_cloth_forces(app->cloth, app->conf.gravity);
satisfy_cloth_constraints(app->cloth, Vec2{0, 0}, Vec2{app->window->dimensions.x-5, app->window->dimensions.y-5});
} }
void destroy_app(App* app) { void destroy_app(App* app) {

47
src/cloth.cpp

@ -11,9 +11,24 @@ Cloth* new_cloth(Vec2 startpos, Vec2 dimensions, unsigned int spacing) {
new_cloth->points = std::vector<Point*>(); new_cloth->points = std::vector<Point*>();
new_cloth->connections = std::vector<Connection*>(); new_cloth->connections = std::vector<Connection*>();
bool frozen_point = false;
for (unsigned int y = 0; y < dimensions.y; y += spacing) { for (unsigned int y = 0; y < dimensions.y; y += spacing) {
if (y == 0) {
frozen_point = true;
} else {
frozen_point = false;
}
for (unsigned int x = 0; x < dimensions.x; x += spacing) { for (unsigned int x = 0; x < dimensions.x; x += spacing) {
Point* new_point = new Point{static_cast<float>(startpos.x + x), static_cast<float>(startpos.y + y), startpos.x + x, startpos.y + y, false}; Point* new_point = new Point{
.x = static_cast<float>(startpos.x + x),
.y = static_cast<float>(startpos.y + y),
.prev_x = static_cast<float>(startpos.x + x),
.prev_y = static_cast<float>(startpos.y + y),
.mass = 10.0f,
.frozen = frozen_point,
.selected = false,
};
new_cloth->points.push_back(new_point); new_cloth->points.push_back(new_point);
if (x > 0) { if (x > 0) {
@ -55,7 +70,35 @@ void destroy_cloth(Cloth* cloth) {
void compute_cloth_forces(Cloth* cloth, Vec2f gravity_vec) { void compute_cloth_forces(Cloth* cloth, Vec2f gravity_vec) {
for (Point* p : cloth->points) { for (Point* p : cloth->points) {
if (p->frozen) {
continue;
}
p->x += gravity_vec.x; p->x += gravity_vec.x;
p->y += gravity_vec.y; p->y += gravity_vec.y;
}
}
void satisfy_cloth_constraints(Cloth* cloth, Vec2 top_left, Vec2 bottom_right) {
for (Point* p : cloth->points) {
// bottom
if (p->y > bottom_right.y) {
p->y = bottom_right.y;
}
// right
if (p->x > bottom_right.x) {
p->x = bottom_right.x;
}
// left
if (p->x < top_left.x) {
p->x = top_left.x;
}
// up
if (p->y < top_left.y) {
p->y = top_left.y;
}
} }
} }

3
src/cloth.hpp

@ -14,4 +14,5 @@ struct Cloth {
Cloth* new_cloth(Vec2 startpos, Vec2 dimensions, unsigned int spacing); Cloth* new_cloth(Vec2 startpos, Vec2 dimensions, unsigned int spacing);
void destroy_cloth(Cloth* cloth); void destroy_cloth(Cloth* cloth);
void compute_cloth_forces(Cloth* cloth, Vec2f gravity_vec); void compute_cloth_forces(Cloth* cloth, Vec2f gravity_vec);
void satisfy_cloth_constraints(Cloth* cloth, Vec2 top_left, Vec2 bottom_right);

2
src/main.cpp

@ -68,7 +68,7 @@ int main(const int argc, char** argv) {
.cloth_startpos = Vec2{50, 50}, .cloth_startpos = Vec2{50, 50},
.cloth_dimensions = Vec2{400, 400}, .cloth_dimensions = Vec2{400, 400},
.cloth_spacing = 20, .cloth_spacing = 20,
.gravity = Vec2f{0.3f, 0.8f}, .gravity = Vec2f{0.0f, 1.5f},
.background_color = RGB{80, 100, 255}, .background_color = RGB{80, 100, 255},
.point_color = RGB{10, 10, 10}, .point_color = RGB{10, 10, 10},
.point_selected_color = RGB{200, 40, 12}, .point_selected_color = RGB{200, 40, 12},

7
src/point.hpp

@ -12,8 +12,9 @@
struct Point { struct Point {
float x; float x;
float y; float y;
unsigned int prev_x; float prev_x;
unsigned int prev_y; float prev_y;
unsigned int mass; float mass;
bool frozen;
bool selected; bool selected;
}; };
Loading…
Cancel
Save