From eecd5a7eb92f24a1e9d3a2e6853363f23ccfccb2 Mon Sep 17 00:00:00 2001 From: Tim Redfern Date: Wed, 13 Jun 2012 23:50:16 +0100 Subject: bird starting to dive --- gaunt01/src/bird.cpp | 116 ++++++++++++++++++++++++++++++------------------ gaunt01/src/bird.h | 4 +- gaunt01/src/testApp.cpp | 4 +- 3 files changed, 79 insertions(+), 45 deletions(-) (limited to 'gaunt01/src') diff --git a/gaunt01/src/bird.cpp b/gaunt01/src/bird.cpp index 09e8a60..da246d1 100644 --- a/gaunt01/src/bird.cpp +++ b/gaunt01/src/bird.cpp @@ -26,7 +26,7 @@ bird::bird() position=ofVec3f(ofGetWidth()/2,ofGetHeight(),-ofGetHeight()/10); heading=-90; direction=ofVec3f(0,-1,0); //director for a heading of 0, level - velocity=ofGetWidth()/50; + velocity=1.0f/30; turnRate=20; diveRate=0; @@ -51,16 +51,13 @@ bird::~bird() } void bird::update(map& players, float angle){ + //movement basics float time=ofGetElapsedTimef(); float timeSeg=time-lastTime; lastTime=time; - heading=heading+(turnRate*timeSeg); - while (heading>180) heading=heading-360; - while (heading <-180) heading=heading+360; - - 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; @@ -130,19 +127,14 @@ void bird::update(map& players, float angle){ while (edgeangle>180.0f) edgeangle -=360.0f; while (edgeangle<-180.0f) edgeangle +=360.0f; turnRate=(turnRate*.995)+(pow(0.002f*radius,2)*edgeangle*0.01); - - //need: a function for generating the angle between things - - - //strip it right back - //bird flying in circle //bird cruising while avoiding edges - //bird floowing people while avoiding edges + //bird floowing people while staying on screen //bird changing height //morph targets - - /* + + //make a list of players within view and decide which one is the best target + //1sr draw lines and visualise ofRay relpointer=ofRay(position,-direction.rotated(heading,ofVec3f(0,0,-1))*1000.0f,false); @@ -151,47 +143,82 @@ void bird::update(map& players, float angle){ playpos.clear(); playhead.clear(); playdip.clear(); + + float nearest=1000000.0f; + int nearnum=-1; + int in=0; map::iterator it; for (it=players.begin();it!=players.end();it++) { if (it->second.active) { - ofVec3f p=it->second.getWorldPosition()-position; - playang.push_back(p.angle(-direction.rotated(heading,ofVec3f(0,0,-1)))); - playdist.push_back(p.length()); - playpos.push_back(it->second.getWorldPosition()); float headif=(atan2(it->second.getWorldPosition().x-position.x,it->second.getWorldPosition().y-position.y)*RAD_TO_DEG)-heading; while (headif>180) headif=headif-360; while (headif <-180) headif=headif+360; - playhead.push_back(headif); + if (abs(headif) < 30) { //this could be based on height + playhead.push_back(headif); + ofVec3f p=it->second.getWorldPosition()-position; + playang.push_back(p.angle(-direction.rotated(heading,ofVec3f(0,0,-1)))); + playdist.push_back(p.length()); + playpos.push_back(it->second.getWorldPosition()); + //maybe all of this is unnecessary if we are only following 1 - just save it if nearer + if (p.length() < nearest) { + nearest=p.length(); + nearnum=in; + } + in++; + } } } - //check if flying out of play - ofVec3f futurepos=position-direction.rotated(heading,ofVec3f(0,0,-1))*velocity*2.0; - ofPoint futurepoint=ofPoint(futurepos.x,futurepos.y); - centrehead=atan2(position.x-centrePoint.x,position.y-centrePoint.y)*RAD_TO_DEG; - while (centrehead>180) centrehead=centrehead-360; - while (centrehead <-180) centrehead=centrehead+360; - - if (0) { //OutsidePolygon(border,futurepoint)) { - //if (ofVec - leaving=true; - turnRate=centrehead/5; + + //dive behaviour + if (nearnum>-1) { + //influence direction + turnRate+=playhead[nearnum]*.1; + //check if relatively high + if (position.z<-ofGetHeight()/40) { + if (diveRate<2.0f) { + //increase dive rate + diveRate+=0.2f; + } + } + else { + //low enough + if (diveRate>0.0f) { + diveRate*=0.9f; + } + } } else { - leaving=false; - //fly towards nearest visible player - int nearest=-1; - float distance=9999; - for(int i=0;i-ofGetHeight()/10) { + //below ceiling + if (diveRate>-0.5f) { + //increase climb rate + diveRate-=0.05f; } } - if (nearest>-1) turnRate=-playhead[nearest]; + else diveRate*=0.9f; } - */ + + if (sign(turnRate)) turnRate=min(2.0f,turnRate); + else turnRate=max(-2.0f,turnRate); + + //if high and target is in sight, swoop to gain speed + //if low and no narget is near, climb to gain a view + + //for now, when the bird hits the target they both blink and the board comes down? + //initially, get the bird to connect with players- + //integrate new keying + //interface - update info - save data + //do the documentation + //go back to the bird - watch out for dead players (from trap doors) - bird is going out of play - just retrieve it for now? + //flap faster while climbing + swoop + + heading=heading+(turnRate*timeSeg); + while (heading>180) heading=heading-360; + while (heading <-180) heading=heading+360; + position-=direction.rotated(heading,ofVec3f(0,0,-1))*(velocity*ofGetHeight()*(1.0f+diveRate))*timeSeg; //.rotate(heading,ofVec3f(0,1,0)) + position +=ofVec3f(0,0,1)*diveRate*timeSeg; } void bird::draw(){ glEnable(GL_DEPTH_TEST); @@ -221,14 +248,19 @@ void bird::drawShadow(){ ofPopMatrix(); } void bird::drawDebug(){ + /* if (leaving) ofSetHexColor(0xff0000); else ofSetHexColor(0xff00ff); ofLine(pointer.s,pointer.s+1000*pointer.t); ofSphere(edgepoint,2.0f); + */ + ofSetHexColor(0xff00ff); char numStr[64]; - sprintf(numStr, "close: %4.1f\nangle: %4.1f\nheading: %4.1f", edgelength,edgeangle,heading); + sprintf(numStr, "dive: %4.2f\nheight: %4.2f", diveRate,position.z); ofDrawBitmapString(numStr,10,10); + + } diff --git a/gaunt01/src/bird.h b/gaunt01/src/bird.h index 1bc99c5..f413911 100644 --- a/gaunt01/src/bird.h +++ b/gaunt01/src/bird.h @@ -92,9 +92,9 @@ class bird ofVec3f direction; - float velocity; //per second + float velocity; //per second relative to ofGetHeight() float turnRate; //per second - float diveRate; //per second + float diveRate; //per second relative to ofGetHeight() float lastTime; diff --git a/gaunt01/src/testApp.cpp b/gaunt01/src/testApp.cpp index 379fb17..749f6d3 100644 --- a/gaunt01/src/testApp.cpp +++ b/gaunt01/src/testApp.cpp @@ -723,9 +723,11 @@ void testApp::draw(){ ofDrawBitmapString(numStr, sc.x, sc.y); for(int i=0;i