Skip to content

Instantly share code, notes, and snippets.

@vanderlin
Created November 25, 2013 18:42
Show Gist options
  • Save vanderlin/7646395 to your computer and use it in GitHub Desktop.
Save vanderlin/7646395 to your computer and use it in GitHub Desktop.
#include "testApp.h"
ofEasyCam camera;
ofVec3f posA, posB;
ofVec3f velA, velB;
bool bTouchingA, bTouchingB;
bool bPressedA, bPressedB;
static float minMouseDis = 15.0;
//--------------------------------------------------------------
void testApp::setup(){
ofEnableDepthTest();
bTouchingA = bTouchingB = false;
bPressedA = bPressedB = false;
camera.setDistance(50);
posA.set(0, 0, 0);
posB.set(20, 0, 0);
}
//--------------------------------------------------------------
void testApp::update(){
float restLength = 15.0;
float springiness = 0.03;
ofVec3f vec = posB - posA;
float dis = vec.length();
float k = -1;
float spring = (springiness * (restLength - dis)) * k;
vec.normalize();
ofVec3f frc = vec * spring;
if(!bPressedA) {
velA += frc;
velA *= 0.96;
posA += velA;
}
if(!bPressedB) {
velB -= frc;
velB *= 0.96;
posB += velB;
}
}
//--------------------------------------------------------------
void testApp::draw(){
camera.begin();
ofNoFill();
ofSetColor(90);
ofDrawBox(0, 0, 0, 100, 100, 100);
ofFill();
(bTouchingA || bPressedA) ? ofSetColor(0, 255, 0) : ofSetColor(255, 0, 0);
ofDrawSphere(posA, 1);
(bTouchingB || bPressedB) ? ofSetColor(0, 255, 0) : ofSetColor(255, 255, 0);
ofDrawSphere(posB, 1);
ofSetColor(255);
ofLine(posA, posB);
camera.end();
}
//--------------------------------------------------------------
void testApp::keyPressed(int key){
}
//--------------------------------------------------------------
void testApp::keyReleased(int key){
}
//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ) {
ofVec2f mouse(ofGetMouseX(), ofGetMouseY());
bTouchingA = ofVec2f(camera.worldToScreen(posA)).distance(mouse) < minMouseDis;
bTouchingB = ofVec2f(camera.worldToScreen(posB)).distance(mouse) < minMouseDis;
}
//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){
ofVec3f cameraMouse = camera.screenToWorld(ofVec2f(ofGetMouseX(), ofGetMouseY()));
if(bPressedA) {
posA.set(cameraMouse);
}
if(bPressedB) {
posB.set(cameraMouse);
}
}
//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
ofVec2f mouse(ofGetMouseX(), ofGetMouseY());
bPressedA = ofVec2f(camera.worldToScreen(posA)).distance(mouse) < minMouseDis;
bPressedB = ofVec2f(camera.worldToScreen(posB)).distance(mouse) < minMouseDis;
if(bPressedA || bPressedB) {
camera.disableMouseInput();
}
}
//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button){
camera.enableMouseInput();
bPressedA = bPressedB = false;
}
//--------------------------------------------------------------
void testApp::windowResized(int w, int h){
}
//--------------------------------------------------------------
void testApp::gotMessage(ofMessage msg){
}
//--------------------------------------------------------------
void testApp::dragEvent(ofDragInfo dragInfo){
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment