summaryrefslogtreecommitdiff
path: root/gaunt01/src/bird.cpp
diff options
context:
space:
mode:
authorTim Redfern <tim@eclectronics.org>2012-06-13 14:52:22 +0100
committerTim Redfern <tim@eclectronics.org>2012-06-13 14:52:22 +0100
commite05bc2828bc213f1e78256f976284bad80722e1a (patch)
treee682759db2c253a4649fd6fac96c46be0d386300 /gaunt01/src/bird.cpp
parent8bc09d4264575d2752374413a180bf9dc1b3b035 (diff)
avoiding edges version
Diffstat (limited to 'gaunt01/src/bird.cpp')
-rw-r--r--gaunt01/src/bird.cpp81
1 files changed, 80 insertions, 1 deletions
diff --git a/gaunt01/src/bird.cpp b/gaunt01/src/bird.cpp
index 49567bc..18f8227 100644
--- a/gaunt01/src/bird.cpp
+++ b/gaunt01/src/bird.cpp
@@ -37,13 +37,18 @@ bird::bird()
centrePoint=ofVec2f(ofGetWidth()/2,600); //quick and dirty
}
+void bird::setBounds(ofPlane* _bounds) {
+ bounds=_bounds;
+}
bird::~bird()
{
//dtor
}
-void bird::update(map<int,player>& players, float angle,vector<ofPoint> border){
+void bird::update(map<int,player>& players, float angle){
+
+ //movement basics
float time=ofGetElapsedTimef();
float timeSeg=time-lastTime;
lastTime=time;
@@ -53,9 +58,77 @@ void bird::update(map<int,player>& players, float angle,vector<ofPoint> border){
position-=direction.rotated(heading,ofVec3f(0,0,-1))*velocity*timeSeg; //.rotate(heading,ofVec3f(0,1,0))
+ //tending to straighten unless avoiding an edge
+ //turnRate *= 0.995;
+
+ //create, -->draw a line representing the birds heading
+
//absolute ray pointer
+ //put on ground
+ //do intersection with outline?
+ //ray: can intersect with another ray
+ //so: make a ray for each segment of polygon and intersect, find the nearest?
+ //ray:: intersect returns a ray- shortest line between lines
+ //or: project bird & heading onto ground
+ //get intersection with each line of poly bounds
+ //or maybe just use screen edges- quicker
+ //find the shortest
+ //depending on the angle it makes, decide whether to turn left or right to avoid boundary or get back within it
+
+ //similar for people - use similar 2d algorithm
+ //deal with elevation seperately
+
+ //bounds is already an array of 4 planes representing edges of screen- does this work?
pointer=ofRay(position.rotated(angle,ofVec3f(1,0,0)),-direction.rotated(heading,ofVec3f(0,0,-1)).rotated(angle,ofVec3f(1,0,0))*1000.0f,false);
+ //intersect with bounds and find shortest distance to edge of world
+ float shortest=1000000.0f;
+ vector <ofVec3f> pts;
+ int shnum=-1;
+ int bdnum=0;
+ for (int i=0;i<4;i++) {
+ ofVec3f p;
+ if (bounds[i].intersect(pointer,p)) {
+ pts.push_back(p);
+ if (position.rotated(angle,ofVec3f(1,0,0)).distance(p)<shortest) {
+ shortest=position.rotated(angle,ofVec3f(1,0,0)).distance(p);
+ shnum=pts.size()-1;
+ bdnum=i;
+ }
+ }
+ }
+ if (shnum>-1) {
+ edgepoint=pts[shnum];
+ edgelength=shortest;
+ ofVec3f bv=bounds[bdnum].getNormal().rotated(angle,ofVec3f(-1,0,0));
+ edgeangle=((atan2(bv.y,bv.x)*RAD_TO_DEG)+90)+heading;
+ while (edgeangle>180) edgeangle -=360;
+ while (edgeangle<-180) edgeangle +=360;
+ }
+ else {
+ printf("error: no bird bounds intersection\n");
+ }
+
+ //decide whether we are running out of space and if so, which way to turn#
+ //turning tendency is more acute when we are more perpendicular, and closer, to the edge
+ //mayeb work out how many frames left before we crash
+
+ //turn formula max(0,90-abs(turnangle))*sign(turnangle)
+ //avoiding edges is a bit of a nightmare
+ //what about a force attracting to the middle of the screen?
+
+ turnRate=(turnRate*.99)+(0.05f*max(0.0f,1.0f-(pow(edgelength*.0100f,2.0f)))*max(0.0f,90.0f-abs(edgeangle))*sign(edgeangle));
+
+ //strip it right back
+ //bird flying in circle
+
+ //bird cruising while avoiding edges
+ //bird floowing people while avoiding edges
+ //bird changing height
+ //morph targets
+
+ /*
+
ofRay relpointer=ofRay(position,-direction.rotated(heading,ofVec3f(0,0,-1))*1000.0f,false);
playang.clear();
@@ -102,6 +175,7 @@ void bird::update(map<int,player>& players, float angle,vector<ofPoint> border){
}
if (nearest>-1) turnRate=-playhead[nearest];
}
+ */
}
void bird::draw(){
@@ -135,6 +209,11 @@ void bird::drawDebug(){
if (leaving) ofSetHexColor(0xff0000);
else ofSetHexColor(0xff00ff);
ofLine(pointer.s,pointer.s+1000*pointer.t);
+ ofSphere(edgepoint,2.0f);
+
+ char numStr[64];
+ sprintf(numStr, "close: %4.1f\nangle: %4.1f\nheading: %4.1f", edgelength,edgeangle,heading);
+ ofDrawBitmapString(numStr,10,10);
}