Last active
November 10, 2019 18:19
-
-
Save prisonerjohn/eadb01103b995546dadb837e6f1599b9 to your computer and use it in GitHub Desktop.
Sensing Machines Classes
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 "ezBall.h" | |
void ezBall::setup(int x, int y) | |
{ | |
pos = glm::vec2(x, y); | |
mass = ofRandom(10, 30); | |
color = ofColor(ofRandom(127, 255), ofRandom(127, 255), ofRandom(127, 255)); | |
} | |
void ezBall::draw() | |
{ | |
ofSetColor(color); | |
ofDrawCircle(pos, mass); | |
} |
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" | |
class ezBall | |
{ | |
public: | |
void setup(int x, int y); | |
void draw(); | |
private: | |
glm::vec2 pos; | |
float mass; | |
ofColor color; | |
}; |
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 "ezBall.h" |
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 | |
class ezBall | |
{ | |
public: | |
private: | |
}; |
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 "ezBall.h" | |
void ezBall::setup(int x, int y) | |
{ | |
pos = glm::vec2(x, y); | |
mass = ofRandom(10, 30); | |
color = ofColor(ofRandom(127, 255), ofRandom(127, 255), ofRandom(127, 255)); | |
} | |
void ezBall::update(glm::vec2 force) | |
{ | |
// Add force. | |
acc += force / mass; | |
if (glm::length(vel) > 0) | |
{ | |
// Add friction. | |
glm::vec2 friction = glm::normalize(vel * -1) * 0.01f; | |
acc += friction; | |
} | |
// Apply acceleration, then reset it! | |
vel += acc; | |
acc = glm::vec2(0.0f); | |
// Move object. | |
pos += vel; | |
// Bounce off walls, taking radius into consideration. | |
if (pos.x - mass < 0 || pos.x + mass > ofGetWidth()) | |
{ | |
pos.x = ofClamp(pos.x, mass, ofGetWidth() - mass); | |
vel.x *= -1; | |
} | |
if (pos.y - mass < 0 || pos.y + mass > ofGetHeight()) | |
{ | |
pos.y = ofClamp(pos.y, mass, ofGetHeight() - mass); | |
vel.y *= -1; | |
} | |
} | |
void ezBall::draw() | |
{ | |
ofSetColor(color); | |
ofDrawCircle(pos, mass); | |
} |
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" | |
class ezBall | |
{ | |
public: | |
void setup(int x, int y); | |
void update(glm::vec2 force); | |
void draw(); | |
private: | |
glm::vec2 pos; | |
glm::vec2 vel; | |
glm::vec2 acc; | |
float mass; | |
ofColor color; | |
}; |
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 |
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 "ezBall.hpp" |
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
#ifndef ezBall_hpp | |
#define ezBall_hpp | |
#include <stdio.h> | |
#endif /* ezBall_hpp */ |
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() | |
{ | |
ofBackground(0); | |
} | |
void ofApp::update() | |
{ | |
} | |
void ofApp::draw() | |
{ | |
for (int i = 0; i < balls.size(); i++) | |
{ | |
balls[i].draw(); | |
} | |
// OR | |
//for (auto b : balls) | |
//{ | |
// b.draw(); | |
//} | |
} | |
void ofApp::addBall(int x, int y) | |
{ | |
// Add a new ezBall. | |
balls.push_back(ezBall()); | |
// Setup the last added ezBall. | |
balls.back().setup(x, y); | |
} | |
void ofApp::keyPressed(int key) | |
{ | |
if (key == ' ') | |
{ | |
balls.clear(); | |
} | |
} | |
void ofApp::mouseDragged(int x, int y, int button) | |
{ | |
addBall(x, y); | |
} | |
void ofApp::mousePressed(int x, int y, int button) | |
{ | |
addBall(x, y); | |
} |
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" | |
#include "ezBall.h" | |
class ofApp : public ofBaseApp | |
{ | |
public: | |
void setup(); | |
void update(); | |
void draw(); | |
void keyPressed(int key); | |
void mouseDragged(int x, int y, int button); | |
void mousePressed(int x, int y, int button); | |
void addBall(int x, int y); | |
std::vector<ezBall> balls; | |
}; |
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() | |
{ | |
ofBackground(0); | |
} | |
void ofApp::update() | |
{ | |
glm::vec2 gravity = glm::vec2(0, 9.8f); | |
for (int i = 0; i < balls.size(); i++) | |
{ | |
balls[i].update(gravity); | |
} | |
// OR | |
//for (auto b : balls) | |
//{ | |
// b.update(gravity); | |
//} | |
} | |
void ofApp::draw() | |
{ | |
for (int i = 0; i < balls.size(); i++) | |
{ | |
balls[i].draw(); | |
} | |
// OR | |
//for (auto b : balls) | |
//{ | |
// b.draw(); | |
//} | |
} | |
void ofApp::addBall(int x, int y) | |
{ | |
// Add a new ezBall. | |
balls.push_back(ezBall()); | |
// Setup the last added ezBall. | |
balls.back().setup(x, y); | |
} | |
void ofApp::keyPressed(int key) | |
{ | |
if (key == ' ') | |
{ | |
balls.clear(); | |
} | |
} | |
void ofApp::mouseDragged(int x, int y, int button) | |
{ | |
addBall(x, y); | |
} | |
void ofApp::mousePressed(int x, int y, int button) | |
{ | |
addBall(x, y); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment