123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- /*
- * 2D Physic Engine
- * body.cpp:
- * Based on pikuma.com Learn Game Physics Engine Programming course.
- * Copyright (c) 2022 986-Studio. All rights reserved.
- *
- * Created by Manoël Trapier on 07/06/2022.
- */
- #include <graphics.h>
- #include <physics/body.h>
- uint32_t forceColours[] =
- {
- 0xFF0000FF, 0xFF00FF00, 0xFFFF0000,
- 0xFF00FFFF, 0xFFFF00FF, 0xFFFFFF00,
- 0xFFFFFFFF,
- };
- void body::integrate(double dt)
- {
- this->acceleration = this->sum_of_forces * this->invMass;
- this->velocity += this->acceleration * dt;
- this->position += this->velocity * dt;
- this->frame_sum = this->sum_of_forces;
- this->clearForces();
- }
- void body::addForce(const vec2 &force)
- {
- this->sum_of_forces += force;
- this->forces.push_back(force);
- }
- void body::forceDebug(bool showVelocity, bool showSum, bool showAll)
- {
- int c = 0;
- if (showAll)
- {
- for (auto f: this->forces)
- {
- graphics::draw::arrow(this->position.x, this->position.y, f.x, f.y, forceColours[c]);
- c = (c + 1) % 7;
- }
- }
- if (showSum)
- {
- graphics::draw::arrow(this->position.x, this->position.y,
- this->frame_sum.x, this->frame_sum.y,
- 0xFF00FFCC);
- }
- if (showVelocity)
- {
- graphics::draw::arrow(this->position.x, this->position.y,
- this->velocity.x, this->velocity.y,
- 0xFF9966ff);
- }
- this->forces.clear();
- }
|