|
|
@ -4,27 +4,37 @@ |
|
|
|
#include <cstdio> |
|
|
|
#include <cstdio> |
|
|
|
#include <vector> |
|
|
|
#include <vector> |
|
|
|
|
|
|
|
|
|
|
|
Cloth* new_cloth(Vec2 startpos, Vec2 dimensions, unsigned int spacing) { |
|
|
|
Cloth* new_cloth( |
|
|
|
|
|
|
|
Vec2 startpos, |
|
|
|
|
|
|
|
Vec2 dimensions, |
|
|
|
|
|
|
|
unsigned int spacing, |
|
|
|
|
|
|
|
float efficiency_factor, |
|
|
|
|
|
|
|
float friction_factor, |
|
|
|
|
|
|
|
Vec2 constraint_top_left, Vec2 constraint_bottom_right |
|
|
|
|
|
|
|
) { |
|
|
|
|
|
|
|
|
|
|
|
Cloth* new_cloth = new Cloth; |
|
|
|
Cloth* new_cloth = new Cloth; |
|
|
|
|
|
|
|
new_cloth->efficiency_factor = efficiency_factor; |
|
|
|
|
|
|
|
new_cloth->friction_factor = friction_factor; |
|
|
|
new_cloth->startpos = startpos; |
|
|
|
new_cloth->startpos = startpos; |
|
|
|
new_cloth->dimensions = dimensions; |
|
|
|
new_cloth->dimensions = dimensions; |
|
|
|
|
|
|
|
new_cloth->constraint_top_left = constraint_top_left; |
|
|
|
|
|
|
|
new_cloth->constraint_bottom_right = constraint_bottom_right; |
|
|
|
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; |
|
|
|
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) { |
|
|
|
|
|
|
|
// if (y == 0 && x == new_cloth->dimensions.x/2) {
|
|
|
|
|
|
|
|
// frozen_point = true;
|
|
|
|
|
|
|
|
// } else {
|
|
|
|
|
|
|
|
// frozen_point = false;
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
Point* new_point = new Point{ |
|
|
|
Point* new_point = new Point{ |
|
|
|
.x = static_cast<float>(startpos.x + x), |
|
|
|
.x = static_cast<float>(startpos.x + x), |
|
|
|
.y = static_cast<float>(startpos.y + y), |
|
|
|
.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, |
|
|
|
.mass = 10.0f, |
|
|
|
.frozen = frozen_point, |
|
|
|
.frozen = frozen_point, |
|
|
|
.selected = false, |
|
|
|
.selected = false, |
|
|
@ -68,37 +78,58 @@ void destroy_cloth(Cloth* cloth) { |
|
|
|
delete cloth; |
|
|
|
delete cloth; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void compute_cloth_forces(Cloth* cloth, Vec2f gravity_vec) { |
|
|
|
// calculate forces and actually MOVE points
|
|
|
|
|
|
|
|
void cloth_step(Cloth* cloth, Vec2f gravity_vec, float timedelta) { |
|
|
|
for (Point* p : cloth->points) { |
|
|
|
for (Point* p : cloth->points) { |
|
|
|
if (p->frozen) { |
|
|
|
if (p->frozen) { |
|
|
|
continue; |
|
|
|
continue; |
|
|
|
} |
|
|
|
} |
|
|
|
p->x += gravity_vec.x; |
|
|
|
|
|
|
|
p->y += gravity_vec.y; |
|
|
|
// calculate velocity
|
|
|
|
|
|
|
|
if (p->y == cloth->constraint_bottom_right.y) { |
|
|
|
|
|
|
|
// if this point's x velocity is too small -> just stop it
|
|
|
|
|
|
|
|
// else -> handle friction
|
|
|
|
|
|
|
|
if (p->velocity.x <= 0.5) { |
|
|
|
|
|
|
|
p->velocity.x = 0; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
p->velocity.x = p->velocity.x*cloth->friction_factor * timedelta; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
p->velocity.x = p->velocity.x+(gravity_vec.x/p->mass) * timedelta; |
|
|
|
|
|
|
|
p->velocity.y = p->velocity.y+(gravity_vec.y/p->mass) * timedelta; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// move point
|
|
|
|
|
|
|
|
p->x += p->velocity.x; |
|
|
|
|
|
|
|
p->y += p->velocity.y; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void satisfy_cloth_constraints(Cloth* cloth, Vec2 top_left, Vec2 bottom_right) { |
|
|
|
// handle constraints, bounces
|
|
|
|
|
|
|
|
void satisfy_cloth_constraints(Cloth* cloth) { |
|
|
|
for (Point* p : cloth->points) { |
|
|
|
for (Point* p : cloth->points) { |
|
|
|
// bottom
|
|
|
|
// bottom
|
|
|
|
if (p->y > bottom_right.y) { |
|
|
|
if (p->y > cloth->constraint_bottom_right.y) { |
|
|
|
p->y = bottom_right.y; |
|
|
|
p->y = cloth->constraint_bottom_right.y; |
|
|
|
|
|
|
|
p->velocity.y = -p->velocity.y*cloth->efficiency_factor; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// right
|
|
|
|
// right
|
|
|
|
if (p->x > bottom_right.x) { |
|
|
|
if (p->x > cloth->constraint_bottom_right.x) { |
|
|
|
p->x = bottom_right.x; |
|
|
|
p->x = cloth->constraint_bottom_right.x; |
|
|
|
|
|
|
|
p->velocity.x = -p->velocity.x*cloth->efficiency_factor; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// left
|
|
|
|
// left
|
|
|
|
if (p->x < top_left.x) { |
|
|
|
if (p->x < cloth->constraint_top_left.x) { |
|
|
|
p->x = top_left.x; |
|
|
|
p->x = cloth->constraint_top_left.x; |
|
|
|
|
|
|
|
p->velocity.x = -p->velocity.x*cloth->efficiency_factor; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// up
|
|
|
|
// up
|
|
|
|
if (p->y < top_left.y) { |
|
|
|
if (p->y < cloth->constraint_top_left.y) { |
|
|
|
p->y = top_left.y; |
|
|
|
p->y = cloth->constraint_top_left.y; |
|
|
|
|
|
|
|
p->velocity.y = -p->velocity.y*cloth->efficiency_factor; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |