From 09fba466578c1074bf7471bbc4615a9047237d46 Mon Sep 17 00:00:00 2001 From: Tim Redfern Date: Thu, 19 Apr 2012 02:14:26 +0100 Subject: bird chasing players --- gaunt01/src/bird.cpp | 51 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 3 deletions(-) (limited to 'gaunt01/src/bird.cpp') diff --git a/gaunt01/src/bird.cpp b/gaunt01/src/bird.cpp index 69549e0..35d9020 100644 --- a/gaunt01/src/bird.cpp +++ b/gaunt01/src/bird.cpp @@ -23,13 +23,15 @@ bird::bird() texture.loadImage("TextureBird.jpg"); //starting pos - position=ofVec3f(ofGetWidth()/2,ofGetHeight(),-ofGetHeight()/4); + position=ofVec3f(ofGetWidth()/2,ofGetHeight(),-ofGetHeight()/10); heading=-90; direction=ofVec3f(0,-1,0); //director for a heading of 0, level velocity=ofGetWidth()/50; turnRate=20; diveRate=0; + + fieldofview=60; lastTime=ofGetElapsedTimef(); } @@ -39,13 +41,51 @@ bird::~bird() //dtor } -void bird::update(const map& players){ +void bird::update(map& players, float angle){ float time=ofGetElapsedTimef(); float timeSeg=time-lastTime; lastTime=time; - heading+=turnRate*timeSeg; + 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)) + + //absolute ray pointer + 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); + + ofRay relpointer=ofRay(position,-direction.rotated(heading,ofVec3f(0,0,-1))*1000.0f,false); + + playang.clear(); + playdist.clear(); + playpos.clear(); + playhead.clear(); + playdip.clear(); + + 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); + } + } + //fly towards nearest visible player + int nearest=-1; + float distance=9999; + for(int i=0;i-1) turnRate=playhead[nearest]; + } void bird::draw(){ glEnable(GL_DEPTH_TEST); @@ -74,4 +114,9 @@ void bird::drawShadow(){ model.drawAnimated(); ofPopMatrix(); } +void bird::drawDebug(){ + ofSetHexColor(0xff00ff); + ofLine(pointer.s,pointer.s+1000*pointer.t); +} + -- cgit v1.2.3