Skip to content

Instantly share code, notes, and snippets.

@vanderlin
Created January 21, 2021 17:49
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 vanderlin/cb905bafc6e9aa5b84eca050e65d06b7 to your computer and use it in GitHub Desktop.
Save vanderlin/cb905bafc6e9aa5b84eca050e65d06b7 to your computer and use it in GitHub Desktop.
vector of ofxBox2d Circles
#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup() {
// first init box2d
box2d.init();
// set the gravity
box2d.setGravity(0, 10);
// create bounds x,y,w,h
box2d.createBounds();
// enable mouse grabbing
box2d.registerGrabbing();
}
//--------------------------------------------------------------
void ofApp::update() {
if (ofGetFrameNum() % 30) {
// make a shared circle
auto circle = make_shared<ofxBox2dCircle>();
// set the physics of the circle
circle->setPhysics(1, 0.7, 0.7);
// set the position and size of circle then add to the world
circle->setup(box2d.getWorld(), ofGetWidth()/2 + ofRandom(-10, 10), ofGetHeight()/2 + ofRandom(-10, 10), 10);
// add to the vector
circles.push_back(circle);
}
// update every frame
box2d.update();
}
//--------------------------------------------------------------
void ofApp::draw() {
for(auto &circle : circles) {
circle->draw();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment