Created
October 27, 2022 22:00
-
-
Save stungeye/c48d44f7e774de862e99731fc1c3fe7a to your computer and use it in GitHub Desktop.
Collision Detection Using openFrameworks and ofRectangle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "ofApp.h" | |
//-------------------------------------------------------------- | |
void ofApp::setup() { | |
} | |
//-------------------------------------------------------------- | |
void ofApp::update() { | |
a2.update({static_cast<float>(ofGetMouseX()), static_cast<float>(ofGetMouseY())}); | |
} | |
//-------------------------------------------------------------- | |
void ofApp::draw() { | |
a2.draw(); | |
a1.draw(a2); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "ofMain.h" | |
struct Coordinate { | |
float x, y; | |
}; | |
class Actor { | |
ofColor color; | |
public: | |
ofRectangle collision; | |
Actor(Coordinate coordinate, float width, float height, ofColor color): | |
color{color}, collision{coordinate.x, coordinate.y, width, height} { | |
} | |
void draw() { | |
ofSetColor(color); | |
ofDrawRectangle(collision); | |
} | |
void draw(Actor other) { | |
draw(); | |
if (collision.intersects(other.collision)) { | |
ofSetColor(ofColor::white); | |
ofDrawRectangle(collision.getIntersection(other.collision)); | |
} | |
} | |
void update(Coordinate newCoordinate) { | |
collision.setX(newCoordinate.x); | |
collision.setY(newCoordinate.y); | |
} | |
}; | |
class ofApp : public ofBaseApp { | |
public: | |
void setup() override; | |
void update() override; | |
void draw() override; | |
private: | |
Actor a1{{200, 200}, 200, 100, ofColor::blue}; | |
Actor a2{{400, 400}, 50, 50, ofColor::red}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment