2016-03-08 17:07:09 +00:00
|
|
|
#include "object.hpp"
|
2016-04-09 02:36:30 +00:00
|
|
|
#include "level.hpp"
|
2016-03-15 16:08:21 +00:00
|
|
|
#include "constants.hpp"
|
2016-03-25 18:15:51 +00:00
|
|
|
#include "collision.hpp"
|
2016-04-08 14:28:56 +00:00
|
|
|
#include <cmath>
|
2016-03-08 17:07:09 +00:00
|
|
|
|
2016-04-02 13:18:29 +00:00
|
|
|
const unsigned int Object::PROP_MASS = 1;
|
|
|
|
const unsigned int Object::PROP_CHARGE = 2;
|
|
|
|
const unsigned int Object::PROP_RESTITUTION = 3;
|
|
|
|
const unsigned int Object::PROP_STATIC_FRICTION = 4;
|
|
|
|
const unsigned int Object::PROP_DYNAMIC_FRICTION = 5;
|
|
|
|
const unsigned int Object::PROP_LAYER = 6;
|
|
|
|
|
|
|
|
Object::Object() :
|
|
|
|
acceleration(0, 0), velocity(0, 0), position(0, 0),
|
2016-04-07 09:22:20 +00:00
|
|
|
selected(false), inv_mass(-1.f),
|
2016-04-02 13:18:29 +00:00
|
|
|
|
|
|
|
// valeurs par défaut pour les propriétés
|
|
|
|
// de tous les objets du jeu
|
|
|
|
mass(1.f), charge(0.f),
|
|
|
|
restitution(0.4f),
|
2016-03-20 21:21:01 +00:00
|
|
|
static_friction(0.4f),
|
|
|
|
dynamic_friction(0.2f),
|
2016-04-02 13:18:29 +00:00
|
|
|
layer(0) {}
|
2016-03-15 16:08:21 +00:00
|
|
|
|
2016-03-28 18:02:23 +00:00
|
|
|
Object::~Object() {}
|
|
|
|
|
2016-04-06 11:54:03 +00:00
|
|
|
void Object::load(std::ifstream& file, ObjectPtr object) {
|
2016-04-02 13:18:29 +00:00
|
|
|
// lecture de la position de l'objet
|
|
|
|
float pos_x, pos_y;
|
|
|
|
|
|
|
|
file.read(reinterpret_cast<char*>(&pos_x), sizeof(pos_x));
|
|
|
|
file.read(reinterpret_cast<char*>(&pos_y), sizeof(pos_y));
|
|
|
|
|
|
|
|
object->setPosition(sf::Vector2f(
|
|
|
|
pos_x * Constants::GRID, pos_y * Constants::GRID
|
|
|
|
));
|
|
|
|
|
|
|
|
// lecture des propriétés facultatives
|
|
|
|
char prop_type = -1;
|
|
|
|
|
|
|
|
while (file.read(&prop_type, 1)) {
|
|
|
|
switch (prop_type) {
|
|
|
|
case Object::PROP_MASS:
|
|
|
|
float mass;
|
|
|
|
file.read(reinterpret_cast<char*>(&mass), sizeof(mass));
|
|
|
|
object->setMass(mass);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Object::PROP_CHARGE:
|
|
|
|
float charge;
|
|
|
|
file.read(reinterpret_cast<char*>(&charge), sizeof(charge));
|
|
|
|
object->setCharge(charge);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Object::PROP_RESTITUTION:
|
|
|
|
float restitution;
|
|
|
|
file.read(reinterpret_cast<char*>(&restitution), sizeof(restitution));
|
|
|
|
object->setRestitution(restitution);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Object::PROP_STATIC_FRICTION:
|
|
|
|
float static_friction;
|
|
|
|
file.read(reinterpret_cast<char*>(&static_friction), sizeof(static_friction));
|
|
|
|
object->setStaticFriction(static_friction);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Object::PROP_DYNAMIC_FRICTION:
|
|
|
|
float dynamic_friction;
|
|
|
|
file.read(reinterpret_cast<char*>(&dynamic_friction), sizeof(dynamic_friction));
|
|
|
|
object->setDynamicFriction(dynamic_friction);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Object::PROP_LAYER:
|
|
|
|
char layer;
|
|
|
|
file.read(&layer, 1);
|
|
|
|
object->setLayer((int) layer - 127);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// propriété de type inconnu : on recule
|
|
|
|
// d'un octet et on sort
|
|
|
|
file.seekg(-1, file.cur);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-09 02:36:30 +00:00
|
|
|
sf::Vector2f Object::getForces(const Level& level) const {
|
2016-03-15 16:08:21 +00:00
|
|
|
sf::Vector2f forces(0, 0);
|
2016-04-09 02:36:30 +00:00
|
|
|
const std::vector<ObjectPtr>& objects = level.getObjects();
|
2016-03-15 16:08:21 +00:00
|
|
|
|
|
|
|
// force de gravité
|
2016-04-09 02:57:54 +00:00
|
|
|
forces += getMass() * level.getGravity();
|
2016-03-15 16:08:21 +00:00
|
|
|
|
2016-03-19 14:38:08 +00:00
|
|
|
// force d'attraction entre objets chargés
|
|
|
|
if (getCharge() != 0) {
|
2016-03-28 15:23:47 +00:00
|
|
|
for (unsigned int j = 0; j < objects.size(); j++) {
|
2016-03-30 12:02:31 +00:00
|
|
|
ObjectPtr attractive = objects[j];
|
2016-03-19 14:38:08 +00:00
|
|
|
|
2016-03-30 12:02:31 +00:00
|
|
|
if (attractive.get() == this || attractive->getCharge() == 0) {
|
2016-03-19 14:38:08 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// vecteur allant de l'objet attracteur vers l'objet actuel
|
|
|
|
sf::Vector2f attraction(getPosition() - attractive->getPosition());
|
|
|
|
|
|
|
|
// la norme de ce vecteur est la distance entre les objets
|
2016-03-20 21:21:01 +00:00
|
|
|
float distance_squared = attraction.x * attraction.x +
|
2016-03-19 14:38:08 +00:00
|
|
|
attraction.y * attraction.y;
|
|
|
|
|
|
|
|
// éviter la division par zéro
|
2016-03-20 21:21:01 +00:00
|
|
|
if (distance_squared == 0) {
|
2016-03-19 14:38:08 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// normalisation du vecteur direction qui porte
|
|
|
|
// la force d'attraction, puis application de la norme
|
2016-03-20 21:21:01 +00:00
|
|
|
attraction /= std::sqrt(distance_squared);
|
2016-03-19 14:38:08 +00:00
|
|
|
attraction *= Constants::ATTRACTION * (
|
|
|
|
(getCharge() * attractive->getCharge()) /
|
2016-03-20 21:21:01 +00:00
|
|
|
distance_squared
|
2016-03-19 14:38:08 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
forces += attraction;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-15 16:08:21 +00:00
|
|
|
return forces;
|
|
|
|
}
|
|
|
|
|
2016-04-09 02:36:30 +00:00
|
|
|
void Object::updateVelocity(const Level& level) {
|
|
|
|
acceleration = getForces(level) * getMassInvert();
|
|
|
|
velocity += acceleration * Constants::PHYSICS_TIME.asSeconds();
|
2016-03-27 21:43:05 +00:00
|
|
|
}
|
2016-03-15 16:08:21 +00:00
|
|
|
|
2016-04-09 02:36:30 +00:00
|
|
|
void Object::updatePosition() {
|
|
|
|
position += velocity * Constants::PHYSICS_TIME.asSeconds();
|
2016-03-18 15:48:22 +00:00
|
|
|
}
|
|
|
|
|
2016-03-28 15:23:47 +00:00
|
|
|
bool Object::detectCollision(const Object& obj, CollisionData& data) const {
|
2016-03-18 15:48:22 +00:00
|
|
|
// si les objets ne sont pas sur la même couche,
|
|
|
|
// ils ne peuvent pas entrer en collision
|
|
|
|
if (getLayer() != obj.getLayer()) {
|
2016-03-28 00:03:56 +00:00
|
|
|
return false;
|
2016-03-18 15:48:22 +00:00
|
|
|
}
|
|
|
|
|
2016-03-28 00:03:56 +00:00
|
|
|
// si les deux boîtes englobantes des deux objets ne
|
2016-03-18 15:48:22 +00:00
|
|
|
// s'intersectent pas, il ne risque pas d'y avoir de collision
|
|
|
|
if (!getAABB()->intersects(*obj.getAABB())) {
|
2016-03-28 00:03:56 +00:00
|
|
|
return false;
|
2016-03-18 15:48:22 +00:00
|
|
|
}
|
|
|
|
|
2016-03-30 21:14:31 +00:00
|
|
|
return getCollisionData(data);
|
2016-03-28 00:03:56 +00:00
|
|
|
}
|
2016-03-18 15:48:22 +00:00
|
|
|
|
2016-04-09 03:43:40 +00:00
|
|
|
void Object::solveCollision(Level& level, Object& obj, const sf::Vector2f& normal) {
|
2016-03-27 21:43:05 +00:00
|
|
|
// si les deux objets sont de masse infinie, réinitialisation
|
|
|
|
// des vitesses en tant que collision
|
|
|
|
if (getMassInvert() == 0 && obj.getMassInvert() == 0) {
|
|
|
|
setVelocity(sf::Vector2f(0, 0));
|
|
|
|
obj.setVelocity(sf::Vector2f(0, 0));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-20 21:21:01 +00:00
|
|
|
sf::Vector2f rel_velo = obj.getVelocity() - getVelocity();
|
|
|
|
float dot_normal = rel_velo.x * normal.x + rel_velo.y * normal.y;
|
2016-03-18 15:48:22 +00:00
|
|
|
|
|
|
|
// si les directions sont divergentes, pas besoin
|
|
|
|
// de résoudre la collision
|
2016-04-08 13:53:30 +00:00
|
|
|
if (dot_normal > 0) {
|
2016-03-18 15:48:22 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-09 01:53:17 +00:00
|
|
|
// en ce point, on est bertins qu'une collision a eu lieu.
|
|
|
|
// activation réciproque des deux objets
|
2016-04-09 03:43:40 +00:00
|
|
|
activated(level, obj);
|
|
|
|
obj.activated(level, *this);
|
2016-04-09 01:53:17 +00:00
|
|
|
|
2016-04-08 13:53:30 +00:00
|
|
|
// on utilise le plus petit coefficient de friction entre les
|
|
|
|
// deux objets comme le coefficient de la collision
|
2016-03-18 15:48:22 +00:00
|
|
|
float restitution = std::min(getRestitution(), obj.getRestitution());
|
2016-04-08 13:53:30 +00:00
|
|
|
|
|
|
|
// calcule et applique l'impulsion de résolution de la collision
|
|
|
|
float collision_impulse = -(1.f + restitution) * std::min(dot_normal + .8f, 0.f) /
|
2016-03-20 21:21:01 +00:00
|
|
|
(getMassInvert() + obj.getMassInvert());
|
|
|
|
|
|
|
|
setVelocity(getVelocity() - getMassInvert() * collision_impulse * normal);
|
|
|
|
obj.setVelocity(obj.getVelocity() + obj.getMassInvert() * collision_impulse * normal);
|
|
|
|
|
|
|
|
// application des forces de frottement entre les deux objets
|
|
|
|
// on calcule le vecteur tangent qui porte la force de frottement.
|
|
|
|
// les coefficients de friction utilisés sont les moyennes de ceux des deux objets
|
|
|
|
rel_velo = obj.getVelocity() - getVelocity();
|
|
|
|
dot_normal = rel_velo.x * normal.x + rel_velo.y * normal.y;
|
|
|
|
|
|
|
|
sf::Vector2f tangent = rel_velo - dot_normal * normal;
|
|
|
|
float tangent_length = std::sqrt(tangent.x * tangent.x + tangent.y * tangent.y);
|
|
|
|
|
2016-03-23 21:30:20 +00:00
|
|
|
// la tangente est nulle : pas de frottement à générer
|
|
|
|
// on évite ainsi une division par zéro
|
2016-03-28 00:03:56 +00:00
|
|
|
if (tangent_length == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
tangent /= tangent_length;
|
2016-03-18 15:48:22 +00:00
|
|
|
|
2016-03-28 00:03:56 +00:00
|
|
|
float magnitude = -(rel_velo.x * tangent.x + rel_velo.y * tangent.y) /
|
|
|
|
(getMassInvert() + obj.getMassInvert());
|
|
|
|
float static_friction = (getStaticFriction() + obj.getStaticFriction()) / 2.f;
|
|
|
|
float dynamic_friction = (getDynamicFriction() + obj.getDynamicFriction()) / 2.f;
|
|
|
|
float friction_impulse;
|
|
|
|
|
|
|
|
// utilisation de la loi de Coulomb sur les frottements dynamiques/statiques
|
|
|
|
// cf https://fr.wikipedia.org/wiki/Loi_de_Coulomb_(m%C3%A9canique)
|
|
|
|
if (std::abs(magnitude) < collision_impulse * static_friction) {
|
|
|
|
friction_impulse = magnitude;
|
|
|
|
} else {
|
|
|
|
friction_impulse = -collision_impulse * dynamic_friction;
|
2016-03-23 21:30:20 +00:00
|
|
|
}
|
2016-03-18 15:48:22 +00:00
|
|
|
|
2016-03-28 00:03:56 +00:00
|
|
|
setVelocity(getVelocity() - getMassInvert() * friction_impulse * tangent);
|
|
|
|
obj.setVelocity(obj.getVelocity() + obj.getMassInvert() * friction_impulse * tangent);
|
|
|
|
}
|
|
|
|
|
2016-03-28 15:23:47 +00:00
|
|
|
void Object::positionalCorrection(Object& obj, const sf::Vector2f& normal, float depth) {
|
2016-04-08 13:53:30 +00:00
|
|
|
float position_correction = std::max(depth - Constants::CORRECTION_SLOP, 0.0f) /
|
|
|
|
(getMassInvert() + obj.getMassInvert()) *
|
2016-03-20 21:21:01 +00:00
|
|
|
Constants::CORRECTION_PERCENTAGE;
|
2016-03-18 15:48:22 +00:00
|
|
|
|
2016-03-20 21:21:01 +00:00
|
|
|
setPosition(getPosition() - getMassInvert() * position_correction * normal);
|
|
|
|
obj.setPosition(obj.getPosition() + obj.getMassInvert() * position_correction * normal);
|
2016-03-15 21:00:03 +00:00
|
|
|
}
|
|
|
|
|
2016-03-28 15:23:47 +00:00
|
|
|
sf::Vector2f Object::getAcceleration() const {
|
2016-03-15 16:08:21 +00:00
|
|
|
return acceleration;
|
|
|
|
}
|
|
|
|
|
2016-03-28 15:23:47 +00:00
|
|
|
sf::Vector2f Object::getVelocity() const {
|
2016-03-15 16:08:21 +00:00
|
|
|
return velocity;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Object::setVelocity(sf::Vector2f set_velocity) {
|
|
|
|
velocity = set_velocity;
|
|
|
|
}
|
2016-03-14 20:11:09 +00:00
|
|
|
|
2016-03-28 15:23:47 +00:00
|
|
|
sf::Vector2f Object::getPosition() const {
|
2016-03-08 17:07:09 +00:00
|
|
|
return position;
|
|
|
|
}
|
|
|
|
|
2016-03-18 15:48:22 +00:00
|
|
|
void Object::setPosition(sf::Vector2f set_position) {
|
|
|
|
position = set_position;
|
|
|
|
}
|
|
|
|
|
2016-04-07 09:22:20 +00:00
|
|
|
bool Object::isSelected() const {
|
|
|
|
return selected;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Object::setSelected(bool set_selected) {
|
|
|
|
selected = set_selected;
|
|
|
|
}
|
|
|
|
|
2016-03-28 15:23:47 +00:00
|
|
|
float Object::getMass() const {
|
2016-03-15 16:08:21 +00:00
|
|
|
return mass;
|
2016-03-14 20:41:20 +00:00
|
|
|
}
|
|
|
|
|
2016-03-28 15:23:47 +00:00
|
|
|
float Object::getMassInvert() const {
|
2016-03-15 21:09:40 +00:00
|
|
|
if (inv_mass >= 0) {
|
|
|
|
return inv_mass;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mass == 0) {
|
|
|
|
inv_mass = 0;
|
|
|
|
return inv_mass;
|
|
|
|
}
|
|
|
|
|
|
|
|
inv_mass = 1 / mass;
|
|
|
|
return inv_mass;
|
|
|
|
}
|
|
|
|
|
2016-03-15 16:08:21 +00:00
|
|
|
void Object::setMass(float set_mass) {
|
|
|
|
mass = set_mass;
|
2016-03-15 21:09:40 +00:00
|
|
|
inv_mass = -1.f;
|
2016-03-14 20:41:20 +00:00
|
|
|
}
|
|
|
|
|
2016-03-28 15:23:47 +00:00
|
|
|
float Object::getCharge() const {
|
2016-03-08 17:07:09 +00:00
|
|
|
return charge;
|
|
|
|
}
|
2016-03-09 18:53:08 +00:00
|
|
|
|
2016-03-14 20:43:27 +00:00
|
|
|
void Object::setCharge(float set_charge) {
|
2016-03-09 18:53:08 +00:00
|
|
|
charge = set_charge;
|
|
|
|
}
|
2016-03-14 20:11:09 +00:00
|
|
|
|
2016-03-28 15:23:47 +00:00
|
|
|
float Object::getRestitution() const {
|
2016-03-15 21:00:03 +00:00
|
|
|
return restitution;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Object::setRestitution(float set_restitution) {
|
|
|
|
restitution = set_restitution;
|
|
|
|
}
|
|
|
|
|
2016-03-28 15:23:47 +00:00
|
|
|
float Object::getStaticFriction() const {
|
2016-03-20 21:21:01 +00:00
|
|
|
return static_friction;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Object::setStaticFriction(float set_static_friction) {
|
|
|
|
static_friction = set_static_friction;
|
|
|
|
}
|
|
|
|
|
2016-03-28 15:23:47 +00:00
|
|
|
float Object::getDynamicFriction() const {
|
2016-03-20 21:21:01 +00:00
|
|
|
return dynamic_friction;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Object::setDynamicFriction(float set_dynamic_friction) {
|
|
|
|
dynamic_friction = set_dynamic_friction;
|
|
|
|
}
|
|
|
|
|
2016-03-28 15:23:47 +00:00
|
|
|
int Object::getLayer() const {
|
2016-03-15 16:08:21 +00:00
|
|
|
return layer;
|
|
|
|
}
|
|
|
|
|
2016-03-28 15:23:47 +00:00
|
|
|
void Object::setLayer(int set_layer) {
|
2016-03-15 16:08:21 +00:00
|
|
|
layer = set_layer;
|
|
|
|
}
|
|
|
|
|
2016-03-30 12:02:31 +00:00
|
|
|
bool ObjectCompare::operator()(ObjectPtr const &t1, ObjectPtr const &t2) const {
|
2016-04-06 15:04:03 +00:00
|
|
|
sf::Vector2f t1_pos = t1->getPosition();
|
|
|
|
sf::Vector2f t2_pos = t2->getPosition();
|
|
|
|
|
2016-04-06 23:21:18 +00:00
|
|
|
return t1_pos.x - t1_pos.y + t1->getLayer() >
|
|
|
|
t2_pos.x - t2_pos.y + t2->getLayer();
|
2016-03-14 20:11:09 +00:00
|
|
|
}
|