Browse Source

Now points move towards a cursor via a click

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

10
src/app.cpp

@ -24,13 +24,13 @@ App* app_init(App_config 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->cloth = new_cloth(
conf.cloth_startpos,
conf.cloth_dimensions,
conf.cloth_spacing,
conf.efficiency_factor,
conf.efficiency_factor,
conf.friction_factor,
Vec2{0, 0}, Vec2{app->window->dimensions.x-5, app->window->dimensions.y-5}
);
@ -66,13 +66,13 @@ void app_handle_input(App* app) {
break;
case SDLK_LEFT:
if (app->conf.timestep >= 0.25) {
app->conf.timestep -= 0.25;
if (app->conf.timestep >= 0.15f) {
app->conf.timestep -= 0.15f;
}
break;
case SDLK_RIGHT:
app->conf.timestep += 0.25;
app->conf.timestep += 0.15f;
break;
}

41
src/cloth.cpp

@ -37,7 +37,7 @@ Cloth* new_cloth(
};
new_cloth->points.push_back(new_point);
if (x > 0) {
if (x != 0) {
Point* p0_left = new_cloth->points[new_cloth->points.size() - 2];
Connection* new_conn = new Connection{*p0_left, *new_point};
new_cloth->connections.push_back(new_conn);
@ -77,11 +77,7 @@ void destroy_cloth(Cloth* cloth) {
// calculate forces, move points and satisfy constraints
void cloth_step(Cloth* cloth, Mouse* mouse, Vec2f gravity_vec, float timedelta) {
for (Point* p : cloth->points) {
if (p->frozen) {
continue;
}
// handle mouse
// check if this point is in pointer's area
// ((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));
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;
}
if (p->selected && mouse->left_button_clicked) {
p->pos = Vec2f{(float) mouse->pos.x, (float) mouse->pos.y};
// skip any calculations if this point is frozen
if (p->frozen) {
continue;
}
// calculate velocity
p->velocity.x = (p->velocity.x + (gravity_vec.x/p->mass)) * timedelta;
p->velocity.y = (p->velocity.y + (gravity_vec.y/p->mass)) * timedelta;
p->acceleration.x = gravity_vec.x / p->mass;
p->acceleration.y = gravity_vec.y / p->mass;
// move point
p->pos.x += p->velocity.x;
p->pos.y += p->velocity.y;
if (p->selected && mouse->left_button_clicked) {
Vec2f difference = {
(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
// bottom
if (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->pos.y = cloth->constraint_bottom_right.y;
p->velocity.x = p->velocity.x * cloth->friction_factor; // friction on the floor
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->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 = {
.win_name = window_name.c_str(),
.window_dimensions = Vec2{800, 700},
.window_dimensions = Vec2{750, 700},
.cloth_startpos = Vec2{50, 50},
.cloth_dimensions = Vec2{400, 400},
.cloth_spacing = 20,
.gravity = Vec2f{0.5f, 0.75f},
.gravity = Vec2f{0.65f, 0.85f},
.efficiency_factor = 0.25f,
.timestep = 1.0f,
.background_color = RGB{205, 222, 242},

1
src/point.hpp

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