Skip to content

Instantly share code, notes, and snippets.

@prisonerjohn
Last active November 10, 2019 18:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prisonerjohn/eadb01103b995546dadb837e6f1599b9 to your computer and use it in GitHub Desktop.
Save prisonerjohn/eadb01103b995546dadb837e6f1599b9 to your computer and use it in GitHub Desktop.
Sensing Machines Classes
#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);
}
#pragma once
#include "ofMain.h"
class ezBall
{
public:
void setup(int x, int y);
void draw();
private:
glm::vec2 pos;
float mass;
ofColor color;
};
#include "ezBall.h"
#pragma once
class ezBall
{
public:
private:
};
#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);
}
#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;
};
#pragma once
#include "ezBall.hpp"
#ifndef ezBall_hpp
#define ezBall_hpp
#include <stdio.h>
#endif /* ezBall_hpp */
#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);
}
#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;
};
#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