Skip to content

Instantly share code, notes, and snippets.

@brannondorsey
Created June 3, 2015 15:00
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 brannondorsey/debc103625a87fb16e95 to your computer and use it in GitHub Desktop.
Save brannondorsey/debc103625a87fb16e95 to your computer and use it in GitHub Desktop.
//
// Butterfly.cpp
// ButterflyTest
//
// Created by bdorse on 4/16/15.
//
//
#include "Butterfly.h"
Butterfly::Butterfly():
_maxWingSpread(120),
_maxFlapSpeed(2),
_wingFlapDir(1),
_curWingTilt(-180),
_bHoming(false),
_bInAir(true),
_maxSpeed(20.0),
_maxForce(0.1),
_maxRotSpeed(1.0),
_velocity(0.0),
_acceleration(0.0),
_destination(ofVec3f::zero())
{
}
Butterfly::~Butterfly()
{
}
void Butterfly::setup(const ofColor& color, const ofColor& shadowColor, const ofVec2f& size)
{
_color = color;
_shadowColor = shadowColor;
_size = size;
_size.y *= 2.0;
_image.loadImage(ofToDataPath("wing_empty_space_2.png"));
// dont use this method
// _image.resize(_size.x, _size.y);
_wing1.setResolution(2, 2);
_wing2.setResolution(2, 2);
_wing1.resizeToTexture(_image.getTextureReference());
_wing2.resizeToTexture(_image.getTextureReference());
_wing1.setScale(0.5);
_wing2.setScale(0.5);
_wing1.truck(0.1);
// face wings towards _body's orientation
_wing1.rotate(-90, ofVec3f(0, 1, 0));
_wing2.rotate(-90, ofVec3f(0, 1, 0));
_wing1.setParent(_body);
_wing2.setParent(_body);
_wing1.tilt(_curWingTilt);
_wing2.tilt(_curWingTilt);
}
void Butterfly::update()
{
if (_bInAir)
{
if (_curWingTilt < -180 || _curWingTilt > -180 + _maxWingSpread)
{
_wingFlapDir = -_wingFlapDir;
}
float step = _maxFlapSpeed * _wingFlapDir;
_wing1.tilt(step);
_wing2.tilt(-step);
_curWingTilt = _wing1.getPitch();
}
if (_bHoming)
{
// POSITION
// apply homing force
_steer();
// apply acceleration to velocity
// and update position by velocity
_velocity += _acceleration;
_velocity.limit(_maxSpeed);
ofVec3f pos = _body.getPosition();
pos += _velocity;
_body.setPosition(pos);
_acceleration.set(ofVec3f::zero());
// ROTATION
// ofVec3f currentRot = _body.getOrientationEuler();
// ofVec3f delta = (_destination - _body.getPosition());
// ofVec3f dir = delta.normalized();
// ofVec3f step = dir * _maxRotSpeed;
//
// cout << delta << endl;
// _body.pan(step.y);
// _body.tilt(step.x);
}
}
void Butterfly::draw(const ofCamera& camera)
{
ofVec3f cameraPos = camera.getPosition();
ofColor color1 = _color;
ofColor color2 = _shadowColor;
if (cameraPos.distance(_wing1.getGlobalPosition()) < cameraPos.distance(_wing2.getGlobalPosition()))
{
color1 = _shadowColor;
color2 = _color;
}
_image.getTextureReference().bind();
ofSetColor(color1);
_wing1.draw();
ofSetColor(color2);
_wing2.draw();
}
void Butterfly::homeTo(const ofVec3f& position)
{
_bHoming = true;
_destination = position;
}
void Butterfly::setPosition(const ofVec3f& position)
{
_body.setPosition(position);
}
void Butterfly::applyForce(const ofVec3f& force)
{
// We could add mass here if we want A = F / M
_acceleration += force;
}
ofVec3f Butterfly::getPosition()
{
return _body.getPosition();
}
bool Butterfly::isHoming()
{
return _bHoming;
}
bool Butterfly::isInAir()
{
return _bInAir;
}
void Butterfly::_steer()
{
// A vector pointing from the location to the target
ofVec3f desired = _destination - _body.getPosition();
float d = desired.length();
// Scale with arbitrary damping within 100 pixels
if (d < 100)
{
float m = ofMap(d, 0, 100, 0, _maxSpeed);
desired.scale(m);
} else
{
desired.scale(_maxSpeed);
}
// Steering = Desired minus Velocity
ofVec3f steer = desired - _velocity;
steer.limit(_maxForce); // Limit to maximum steering force
if (d > 1)
{
applyForce(steer);
}
else
{
// destination reached
desired.set(ofVec3f::zero());
_bHoming = false;
_destination.set(ofVec3f::zero());
}
}
//
// Butterfly.h
// ButterflyTest
//
// Created by bdorse on 4/16/15.
//
//
#ifndef __ButterflyTest__Butterfly__
#define __ButterflyTest__Butterfly__
#include "ofMain.h"
class Butterfly
{
public:
Butterfly();
~Butterfly();
void setup(const ofColor& color, const ofColor& shadowColor, const ofVec2f& size);
void update();
void draw(const ofCamera& camera);
void homeTo(const ofVec3f& position);
void setPosition(const ofVec3f& position);
void applyForce(const ofVec3f& force);
bool isHoming();
bool isInAir();
ofVec3f getPosition();
protected:
void _steer();
bool _bHoming;
bool _bInAir;
float _maxWingSpread;
float _maxFlapSpeed;
float _wingFlapDir;
float _curWingTilt;
float _maxSpeed;
float _maxForce;
float _maxRotSpeed;
ofVec2f _size;
ofVec3f _destination;
ofVec3f _velocity;
ofVec3f _acceleration;
ofColor _color;
ofColor _shadowColor;
ofImage _image;
ofPlanePrimitive _wing1;
ofPlanePrimitive _wing2;
ofNode _body;
};
#endif /* defined(__ButterflyTest__Butterfly__) */
#include "ofMain.h"
#include "ofApp.h"
//========================================================================
int main( ){
ofSetupOpenGL(1024,768,OF_WINDOW); // <-------- setup the GL context
// this kicks off the running of my app
// can be OF_WINDOW or OF_FULLSCREEN
// pass in width and height too:
ofRunApp(new ofApp());
}
#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup(){
ofBackground(255);
ofSetFrameRate(60);
ofEnableDepthTest();
ofEnableAlphaBlending();
ofEnableAntiAliasing();
butterfly.setup(ofColor(255, 0, 0), ofColor(200, 0, 0), ofVec2f(40, 25));
camera.lookAt(butterfly.getPosition());
}
//--------------------------------------------------------------
void ofApp::update(){
butterfly.update();
}
//--------------------------------------------------------------
void ofApp::draw(){
camera.begin();
// don't render alpha pixels to depth buffer
glAlphaFunc(GL_GREATER, 0.1f);
glEnable(GL_ALPHA_TEST);
butterfly.draw(camera);
camera.end();
}
//--------------------------------------------------------------
void ofApp::keyPressed(int key){
}
//--------------------------------------------------------------
void ofApp::keyReleased(int key){
if (key == OF_KEY_LEFT)
{
butterfly.homeTo(camera.screenToWorld(ofVec3f(-50, 0, 0)));
}
else if (key == OF_KEY_RIGHT)
{
butterfly.homeTo(camera.screenToWorld(ofVec3f(50, 0, 0)));
}
else if (key == OF_KEY_UP)
{
butterfly.homeTo(camera.screenToWorld(ofVec3f(0, 50, 0)));
}
else if (key == OF_KEY_DOWN)
{
butterfly.homeTo(camera.screenToWorld(ofVec3f(0, -50, 0)));
}
else if (key == OF_KEY_RETURN)
{
butterfly.homeTo(camera.screenToWorld(ofVec3f::zero()));
}
}
//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y ){
}
//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button){
if (button == 2)
{
ofLogNotice("ofApp::mousePressed") << "Homing butterfly";
butterfly.homeTo(camera.screenToWorld(ofVec3f(ofGetMouseX(), ofGetMouseY(), 50)));
}
}
//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::windowResized(int w, int h){
}
//--------------------------------------------------------------
void ofApp::gotMessage(ofMessage msg){
}
//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo){
}
#pragma once
#include "ofMain.h"
#include "Butterfly.h"
class ofApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
void keyPressed(int key);
void keyReleased(int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void windowResized(int w, int h);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
ofEasyCam camera;
Butterfly butterfly;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment