Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion tracking/include/collision_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class CollisionObject {
CollisionObject(glm::vec2 position, glm::vec2 velocity, const std::string &filename,
std::shared_ptr<EffectShader> effect_shader);

void update(std::vector<Player> &players, const ofEasyCam &camera);
void update(std::vector<Player> &players, const std::vector<CollisionObject> &objects, const ofEasyCam &camera);
void draw() const;

std::pair<bool, glm::vec2> global_effect_triggered();
Expand All @@ -30,11 +30,13 @@ class CollisionObject {
float width() const;
float height() const;
glm::vec2 position() const;
glm::vec2 velocity() const;
std::shared_ptr<EffectShader> effect_shader() const;

protected:
void play_random_pluck();
std::pair<bool, glm::vec2> check_collision_with_bodies(std::vector<Player> &players, const ofEasyCam &camera);
std::pair<bool, glm::vec2> check_collision_with_objects(const std::vector<CollisionObject> &objects);

const float _min_speed = 5;
const float _max_speed = 20;
Expand Down
46 changes: 44 additions & 2 deletions tracking/src/collision_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

#include <ofAppRunner.h>
#include <ofFbo.h>
#include <ofMath.h>
#include <ofGraphics.h>
#include <ofMath.h>

CollisionObject::CollisionObject() : CollisionObject({0, 0}, {0, 0}, "", std::make_shared<EffectShader>()) {}

Expand Down Expand Up @@ -39,7 +39,8 @@ CollisionObject::CollisionObject(glm::vec2 position, glm::vec2 velocity, const s
}
}

void CollisionObject::update(std::vector<Player> &players, const ofEasyCam &camera) {
void CollisionObject::update(std::vector<Player> &players, const std::vector<CollisionObject> &objects,
const ofEasyCam &camera) {
if (_position.x <= 0 || _position.x + width() >= ofGetWidth()) {
play_random_pluck();
_velocity.x *= -1;
Expand All @@ -60,6 +61,10 @@ void CollisionObject::update(std::vector<Player> &players, const ofEasyCam &came
_can_collide = true;
}

if (auto [collided, dir] = check_collision_with_objects(objects); collided) {
_velocity = dir;
}

_velocity *= _friction;

auto speed = glm::length(_velocity);
Expand Down Expand Up @@ -143,10 +148,47 @@ std::pair<bool, glm::vec2> CollisionObject::check_collision_with_bodies(std::vec
return {false, {0, 0}};
}

std::pair<bool, glm::vec2> CollisionObject::check_collision_with_objects(const std::vector<CollisionObject> &objects) {
auto this_bounds = ofRectangle(position().x, position().y, width(), height());
for (const auto &object: objects) {
if (this == &object) {
continue;
}

auto other_bounds = ofRectangle(object.position().x, object.position().y, object.width(), object.height());
if (this_bounds.intersects(other_bounds)) {
glm::vec2 new_velocity = _velocity;

// Calculate overlap on X and Y axes
float overlap_left = this_bounds.getRight() - other_bounds.getLeft();
float overlap_right = other_bounds.getRight() - this_bounds.getLeft();
float overlap_top = this_bounds.getBottom() - other_bounds.getTop();
float overlap_bottom = other_bounds.getBottom() - this_bounds.getTop();

float x_overlap = std::min(overlap_left, overlap_right);
float y_overlap = std::min(overlap_top, overlap_bottom);

const auto intersection = this_bounds.getIntersection(other_bounds);

if (x_overlap < y_overlap) {
new_velocity.x = -(new_velocity.x); // bounce horizontally
} else {
new_velocity.y = -(new_velocity.y); // bounce vertically
}

return {true, new_velocity};
}
}

return {false, _velocity};
}

float CollisionObject::width() const { return _image.getWidth(); }

float CollisionObject::height() const { return _image.getHeight(); }

glm::vec2 CollisionObject::position() const { return _position; }

glm::vec2 CollisionObject::velocity() const { return _velocity; }

std::shared_ptr<EffectShader> CollisionObject::effect_shader() const { return _effect_shader; }
2 changes: 1 addition & 1 deletion tracking/src/tracking_scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void TrackingScene::update() {
}

for (auto &collision_object: _collision_objects) {
collision_object.update(_players, _camera);
collision_object.update(_players, _collision_objects, _camera);

if (ofGetSystemTimeMillis() - _global_effect_trigger_time > _global_effect_duration) {
auto [triggered, position] = collision_object.global_effect_triggered();
Expand Down