Browse Source

Now points move towards a cursor via a click

master
Gitea 3 years ago
parent
commit
c7a7d65ad2
  1. 8
      src/app.cpp
  2. 39
      src/cloth.cpp
  3. 4
      src/main.cpp
  4. 1
      src/point.hpp

8
src/app.cpp

@ -24,7 +24,7 @@ App* app_init(App_config conf) {
} }
app->conf = conf; app->conf = conf;
app->mouse = new_mouse(Vec2{0, 0}, 30); app->mouse = new_mouse(Vec2{0, 0}, 50);
app->window = new_window(conf.win_name, conf.window_dimensions); app->window = new_window(conf.win_name, conf.window_dimensions);
app->cloth = new_cloth( app->cloth = new_cloth(
conf.cloth_startpos, conf.cloth_startpos,
@ -66,13 +66,13 @@ void app_handle_input(App* app) {
break; break;
case SDLK_LEFT: case SDLK_LEFT:
if (app->conf.timestep >= 0.25) { if (app->conf.timestep >= 0.15f) {
app->conf.timestep -= 0.25; app->conf.timestep -= 0.15f;
} }
break; break;
case SDLK_RIGHT: case SDLK_RIGHT:
app->conf.timestep += 0.25; app->conf.timestep += 0.15f;
break; break;
} }

39
src/cloth.cpp

@ -37,7 +37,7 @@ Cloth* new_cloth(
}; };
new_cloth->points.push_back(new_point); new_cloth->points.push_back(new_point);
if (x > 0) { if (x != 0) {
Point* p0_left = new_cloth->points[new_cloth->points.size() - 2]; Point* p0_left = new_cloth->points[new_cloth->points.size() - 2];
Connection* new_conn = new Connection{*p0_left, *new_point}; Connection* new_conn = new Connection{*p0_left, *new_point};
new_cloth->connections.push_back(new_conn); new_cloth->connections.push_back(new_conn);
@ -77,11 +77,7 @@ void destroy_cloth(Cloth* cloth) {
// calculate forces, move points and satisfy constraints // calculate forces, move points and satisfy constraints
void cloth_step(Cloth* cloth, Mouse* mouse, Vec2f gravity_vec, float timedelta) { void cloth_step(Cloth* cloth, Mouse* mouse, Vec2f gravity_vec, float timedelta) {
for (Point* p : cloth->points) { for (Point* p : cloth->points) {
if (p->frozen) { // check if this point is in pointer's area
continue;
}
// handle mouse
// ((x0 - x1)^2 + (y0 - y1)^2)^0.5 // ((x0 - x1)^2 + (y0 - y1)^2)^0.5
float distance_to_point = std::sqrt(pow(p->pos.x - mouse->pos.x, 2.0f)+pow(p->pos.y - mouse->pos.y, 2.0f)); float distance_to_point = std::sqrt(pow(p->pos.x - mouse->pos.x, 2.0f)+pow(p->pos.y - mouse->pos.y, 2.0f));
if (distance_to_point <= mouse->cursor_radius_size) { if (distance_to_point <= mouse->cursor_radius_size) {
@ -90,23 +86,34 @@ void cloth_step(Cloth* cloth, Mouse* mouse, Vec2f gravity_vec, float timedelta)
p->selected = false; p->selected = false;
} }
if (p->selected && mouse->left_button_clicked) { // skip any calculations if this point is frozen
p->pos = Vec2f{(float) mouse->pos.x, (float) mouse->pos.y}; if (p->frozen) {
continue;
} }
// calculate velocity // calculate velocity
p->velocity.x = (p->velocity.x + (gravity_vec.x/p->mass)) * timedelta; p->acceleration.x = gravity_vec.x / p->mass;
p->velocity.y = (p->velocity.y + (gravity_vec.y/p->mass)) * timedelta; p->acceleration.y = gravity_vec.y / p->mass;
// move point if (p->selected && mouse->left_button_clicked) {
p->pos.x += p->velocity.x; Vec2f difference = {
p->pos.y += p->velocity.y; (float) (mouse->pos.x - p->pos.x) * cloth->efficiency_factor,
(float) (mouse->pos.y - p->pos.y) * cloth->efficiency_factor,
};
p->acceleration.x = difference.x / p->mass;
p->acceleration.y = difference.y / p->mass;
}
// apply calculated velocity
p->velocity.x = p->velocity.x + p->acceleration.x * timedelta;
p->velocity.y = p->velocity.y + p->acceleration.y * timedelta;
// now handle constraints // now handle constraints
// bottom // bottom
if (p->pos.y >= cloth->constraint_bottom_right.y) { if (p->pos.y >= cloth->constraint_bottom_right.y) {
p->pos.y = cloth->constraint_bottom_right.y; p->pos.y = cloth->constraint_bottom_right.y;
p->velocity.x = (p->velocity.x * cloth->friction_factor); p->velocity.x = p->velocity.x * cloth->friction_factor; // friction on the floor
p->velocity.y = -p->velocity.y * cloth->efficiency_factor; p->velocity.y = -p->velocity.y * cloth->efficiency_factor;
} }
@ -127,5 +134,9 @@ void cloth_step(Cloth* cloth, Mouse* mouse, Vec2f gravity_vec, float timedelta)
p->pos.y = cloth->constraint_top_left.y; p->pos.y = cloth->constraint_top_left.y;
p->velocity.y = -p->velocity.y * cloth->efficiency_factor; p->velocity.y = -p->velocity.y * cloth->efficiency_factor;
} }
// move point
p->pos.x += p->velocity.x;
p->pos.y += p->velocity.y;
} }
} }

4
src/main.cpp

@ -64,11 +64,11 @@ int main(const int argc, char** argv) {
App_config config = { App_config config = {
.win_name = window_name.c_str(), .win_name = window_name.c_str(),
.window_dimensions = Vec2{800, 700}, .window_dimensions = Vec2{750, 700},
.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.5f, 0.75f}, .gravity = Vec2f{0.65f, 0.85f},
.efficiency_factor = 0.25f, .efficiency_factor = 0.25f,
.timestep = 1.0f, .timestep = 1.0f,
.background_color = RGB{205, 222, 242}, .background_color = RGB{205, 222, 242},

1
src/point.hpp

@ -9,5 +9,6 @@ struct Point {
bool frozen; bool frozen;
bool selected; bool selected;
float radius; float radius;
Vec2f acceleration;
Vec2f velocity; Vec2f velocity;
}; };
Loading…
Cancel
Save