|
@@ -30,8 +30,12 @@ void application::setup()
|
|
this->millisecsPreviousFrame = SDL_GetTicks();
|
|
this->millisecsPreviousFrame = SDL_GetTicks();
|
|
|
|
|
|
this->anchor = vec2(graphics::width() / 2.0,30);
|
|
this->anchor = vec2(graphics::width() / 2.0,30);
|
|
- this->particles.push_back(new particle(graphics::width() / 2.0, graphics::height() / 2.0,
|
|
|
|
- 2.0, 10));
|
|
|
|
|
|
+
|
|
|
|
+ for(int i = 0; i < this->numParticules; i++)
|
|
|
|
+ {
|
|
|
|
+ this->particles.push_back(new particle(graphics::width() / 2.0, 30 + (i+1) * this->restLenght,
|
|
|
|
+ 2.0, 5));
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
void application::input()
|
|
void application::input()
|
|
@@ -94,9 +98,9 @@ void application::input()
|
|
{
|
|
{
|
|
if (this->leftButtonPressed)
|
|
if (this->leftButtonPressed)
|
|
{
|
|
{
|
|
- vec2 impulseDirection = (particles[0]->position - this->mouseCursor).unitVector();
|
|
|
|
- double impulseMagnitude = (particles[0]->position - this->mouseCursor).magnitude() * 5.0;
|
|
|
|
- particles[0]->velocity = impulseDirection * impulseMagnitude;
|
|
|
|
|
|
+ vec2 impulseDirection = (particles[this->numParticules-1]->position - this->mouseCursor).unitVector();
|
|
|
|
+ double impulseMagnitude = (particles[this->numParticules-1]->position - this->mouseCursor).magnitude() * 5.0;
|
|
|
|
+ particles[this->numParticules-1]->velocity = impulseDirection * impulseMagnitude;
|
|
}
|
|
}
|
|
this->leftButtonPressed = false;
|
|
this->leftButtonPressed = false;
|
|
}
|
|
}
|
|
@@ -139,30 +143,40 @@ void application::update()
|
|
{
|
|
{
|
|
this->syncAndDeltatime();
|
|
this->syncAndDeltatime();
|
|
updateTime.start();
|
|
updateTime.start();
|
|
|
|
+ particle *lastPart;
|
|
|
|
|
|
for (auto part:this->particles)
|
|
for (auto part:this->particles)
|
|
{
|
|
{
|
|
part->addForce(pushForce);
|
|
part->addForce(pushForce);
|
|
|
|
|
|
- vec2 friction = force::generateFrictionForce(*part, 20);
|
|
|
|
- part->addForce(friction);
|
|
|
|
-
|
|
|
|
- vec2 drag = force::generateDragForce(*part, 0.001);
|
|
|
|
|
|
+ vec2 drag = force::generateDragForce(*part, 0.002);
|
|
part->addForce(drag);
|
|
part->addForce(drag);
|
|
|
|
|
|
vec2 weight = vec2(0, part->mass * 9.81 * PIXELS_PER_METER);
|
|
vec2 weight = vec2(0, part->mass * 9.81 * PIXELS_PER_METER);
|
|
part->addForce(weight);
|
|
part->addForce(weight);
|
|
|
|
+
|
|
|
|
+ vec2 spring;
|
|
|
|
+ if (part == this->particles[0])
|
|
|
|
+ {
|
|
|
|
+ spring = force::generateSpringForce(*part,this->anchor, this->restLenght, this->k);
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ spring = force::generateSpringForce(*part, *lastPart, this->restLenght, this->k);
|
|
|
|
+ lastPart->addForce(-spring);
|
|
|
|
+ }
|
|
|
|
+ part->addForce(spring);
|
|
|
|
+
|
|
|
|
+ lastPart = part;
|
|
}
|
|
}
|
|
|
|
|
|
- vec2 spring = force::generateSpringForce(*this->particles[0],
|
|
|
|
- this->anchor, this->restLenght, this->k);
|
|
|
|
- this->particles[0]->addForce(spring);
|
|
|
|
|
|
+
|
|
|
|
|
|
for (auto part:this->particles)
|
|
for (auto part:this->particles)
|
|
{
|
|
{
|
|
part->integrate(deltaTime);
|
|
part->integrate(deltaTime);
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+#if ENABLE_BOUNDINGBOX
|
|
for (auto part:this->particles)
|
|
for (auto part:this->particles)
|
|
{
|
|
{
|
|
// check the particles position and keep the particle in the window.
|
|
// check the particles position and keep the particle in the window.
|
|
@@ -196,29 +210,41 @@ void application::update()
|
|
part->velocity.x = -part->velocity.x;
|
|
part->velocity.x = -part->velocity.x;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+#endif /* ENABLE_BOUNDINGBOX */
|
|
updateTime.stop();
|
|
updateTime.stop();
|
|
}
|
|
}
|
|
|
|
|
|
void application::render()
|
|
void application::render()
|
|
{
|
|
{
|
|
renderTime.start();
|
|
renderTime.start();
|
|
|
|
+ particle *lastPart;
|
|
|
|
|
|
graphics::clearScreen(0xFF0F0721);
|
|
graphics::clearScreen(0xFF0F0721);
|
|
if (this->leftButtonPressed)
|
|
if (this->leftButtonPressed)
|
|
{
|
|
{
|
|
- graphics::draw::line(this->particles[0]->position.x, this->particles[0]->position.y,
|
|
|
|
|
|
+ graphics::draw::line(this->particles[this->numParticules-1]->position.x,
|
|
|
|
+ this->particles[this->numParticules-1]->position.y,
|
|
this->mouseCursor.x, this->mouseCursor.y, 0xFFFFFFFF);
|
|
this->mouseCursor.x, this->mouseCursor.y, 0xFFFFFFFF);
|
|
}
|
|
}
|
|
|
|
|
|
- graphics::draw::line(this->anchor.x, this->anchor.y,
|
|
|
|
- this->particles[0]->position.x, this->particles[0]->position.y,
|
|
|
|
- 0xFF313131);
|
|
|
|
- graphics::draw::fillCircle(this->anchor.x, this->anchor.y, 5, 0xFF001155);
|
|
|
|
-
|
|
|
|
for(auto part: this->particles)
|
|
for(auto part: this->particles)
|
|
{
|
|
{
|
|
|
|
+ if (part == this->particles[0])
|
|
|
|
+ {
|
|
|
|
+ graphics::draw::line(this->anchor.x, this->anchor.y,
|
|
|
|
+ this->particles[0]->position.x, this->particles[0]->position.y,
|
|
|
|
+ 0xFF313131);
|
|
|
|
+ graphics::draw::fillCircle(this->anchor.x, this->anchor.y, 5, 0xFF001155);
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ graphics::draw::line( lastPart->position.x, lastPart->position.y,
|
|
|
|
+ part->position.x, part->position.y,
|
|
|
|
+ 0xFF313131);
|
|
|
|
+ }
|
|
graphics::draw::fillCircle(part->position.x, part->position.y, part->radius, 0xFFAA3300);
|
|
graphics::draw::fillCircle(part->position.x, part->position.y, part->radius, 0xFFAA3300);
|
|
|
|
+
|
|
|
|
+ lastPart = part;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|