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 +++++++++++- gaunt01/src/bird.h | 32 +++++++- gaunt01/src/player.cpp | 1 + gaunt01/src/player.h | 1 + gaunt01/src/testApp.cpp | 204 +++++++++++++++++++++++++++-------------------- gaunt01/src/testApp.h | 4 + gaunt01/src/trapdoor.cpp | 27 +++---- 7 files changed, 210 insertions(+), 110 deletions(-) (limited to 'gaunt01') 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); +} + diff --git a/gaunt01/src/bird.h b/gaunt01/src/bird.h index abcd66f..026da72 100644 --- a/gaunt01/src/bird.h +++ b/gaunt01/src/bird.h @@ -26,6 +26,15 @@ MAKING THE BIRD CHASE THE PLAYERS + + find distance to edge of play - alter turn behaviour depending on how bird needs to head + detect player in FOV + turn & swoop + pick player and remove + + planes frustrum belongs to camera + bird checks ray dist to planes + avoids nearest +ve */ @@ -34,23 +43,37 @@ #include "normBindTexture.h" #include "player.h" +#include "ofxRay.h" class bird { public: bird(); virtual ~bird(); - void update(const map& players); + void update(map& players,float angle); void draw(); void drawShadow(); + void drawDebug(); string currentseq; morphmesh model; + + ofRay pointer; + ofVec3f position; + + vector playang; + vector playhead; + vector playdip; + vector playdist; + vector playpos; + + float heading; + protected: private: - - ofVec3f position; - float heading; + + float fieldofview; + ofVec3f direction; float velocity; //per second @@ -61,6 +84,7 @@ class bird ofImage texture; + }; diff --git a/gaunt01/src/player.cpp b/gaunt01/src/player.cpp index 0ffdcda..cd51097 100644 --- a/gaunt01/src/player.cpp +++ b/gaunt01/src/player.cpp @@ -3,6 +3,7 @@ player::player() { isCaught=false; + active=true; } player::~player() diff --git a/gaunt01/src/player.h b/gaunt01/src/player.h index 9167237..5aadec6 100644 --- a/gaunt01/src/player.h +++ b/gaunt01/src/player.h @@ -18,6 +18,7 @@ class player : public ofTessellator void draw(); void caught(); bool isCaught; + bool active; //still on screen void carried(ofVec3f _posOffset); void setCatchTransform(ofVec3f _catchPos,ofVec3f _catchScale); protected: diff --git a/gaunt01/src/testApp.cpp b/gaunt01/src/testApp.cpp index 9e2a574..4047f69 100644 --- a/gaunt01/src/testApp.cpp +++ b/gaunt01/src/testApp.cpp @@ -81,6 +81,8 @@ void testApp::setup(){ cam.cacheMatrices(); //stop error messages testpts=new ofVec3f[4]; + + bounds=new ofPlane[4]; trapDoor=trapdoor(screen2plane(ofVec2f(ofGetWidth(),0)),screen2plane(ofVec2f(ofGetWidth(),ofGetHeight())),ofVec2f(30,30)); updatePlane(); @@ -127,7 +129,35 @@ bool testApp::rectsCross(ofRectangle rect1,ofRectangle rect2) { void testApp::updatePlane(){ + //setup screen for new incline angle plane.setNormal(ofVec3f(0,sin(cam_angle*DEG_TO_RAD),-cos(cam_angle*DEG_TO_RAD))); + + ofVec2f l=ofVec2f(ofGetWidth()/20,ofGetHeight()/2); + ofRay r=projector.castPixel(l.x,l.y); + ofVec3f p; + plane.intersect(r,p); + ofVec3f pn=(p-projector.getGlobalPosition()).getPerpendicular(ofVec3f(0,1,0)); + bounds[0]=ofPlane(p,pn,pn,ofVec2f(1000,1000)); + + l=ofVec2f(ofGetWidth()/2,ofGetHeight()/20); + r=projector.castPixel(l.x,l.y); + plane.intersect(r,p); + pn=(p-projector.getGlobalPosition()).getPerpendicular(ofVec3f(1,0,0)); + bounds[1]=ofPlane(p,pn,-pn,ofVec2f(1000,1000)); + + l=ofVec2f(19*ofGetWidth()/20,ofGetHeight()/2); + r=projector.castPixel(l.x,l.y); + plane.intersect(r,p); + pn=(p-projector.getGlobalPosition()).getPerpendicular(ofVec3f(0,1,0)); + bounds[2]=ofPlane(p,pn,-pn,ofVec2f(1000,1000)); + + l=ofVec2f(ofGetWidth()/2,19*ofGetHeight()/20); + r=projector.castPixel(l.x,l.y); + plane.intersect(r,p); + pn=(p-projector.getGlobalPosition()).getPerpendicular(ofVec3f(1,0,0)); + bounds[1]=ofPlane(p,pn,-pn,ofVec2f(1000,1000)); + + //create ground mesh with hole for trapdoor ground=ofMesh(); ground.addVertex(ofVec3f(0,0,0)); @@ -249,8 +279,10 @@ void testApp::update(){ //check if a key exists in a map - map::count //do we purge them or just stop drawing them? is it a problem inheriting tesselator re memory? //if we keep them we can have a 'loserboard' + set ids; for(int i=0;i::iterator it; + for (it=players.begin();it!=players.end();it++) if(ids.find(it->first)==ids.end()) it->second.active=false; } if (trapDoor.checkUpdate(players)) updatePlane(); - Bird.update(players); + Bird.update(players,cam_angle); + } //-------------------------------------------------------------- void testApp::draw(){ - ofBackground(0,0,0); + + ofBackground(0,0,0); cam.begin(); + + glDisable(GL_DEPTH_TEST); - switch(mode) { - case PLAY: - glDisable(GL_DEPTH_TEST); - - ofSetHexColor(0xffffff); - ofPushMatrix(); - ofRotate(cam_angle,1,0,0); - trapDoor.draw(); - ofPopMatrix(); - - ofSetHexColor(0xffffff); - bindTexture(bgImg); - ground.draw(); - unbindTexture(bgImg); - - ofPushMatrix(); - ofRotate(cam_angle,1,0,0); - Bird.drawShadow(); - ofPopMatrix(); - - glDisable(GL_DEPTH_TEST); - ofSetHexColor(0xffffff); - ofPushMatrix(); - ofRotate(cam_angle,1,0,0); - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - trapDoor.drawSplash(cam_angle); - //trapDoor.splashFrames[0].draw(ofGetWidth()/2,ofGetHeight()/2); - glDisable(GL_BLEND); - ofPopMatrix(); - - glDisable(GL_DEPTH_TEST); - ofSetHexColor(0xffffff); - colorImg.getTextureReference().bind(); - for(int i=0;i + #include "ofMain.h" #include "ofxOpenCv.h" @@ -93,6 +95,8 @@ class testApp : public ofBaseApp{ float bgnum; ofLight light; + + ofPlane* bounds; }; diff --git a/gaunt01/src/trapdoor.cpp b/gaunt01/src/trapdoor.cpp index 46151c1..5fc639f 100644 --- a/gaunt01/src/trapdoor.cpp +++ b/gaunt01/src/trapdoor.cpp @@ -100,15 +100,17 @@ float trapdoor::getFalldist(){ bool trapdoor::checkUpdate(map& players) { map::iterator it; for (it=players.begin();it!=players.end();it++) { - ofVec3f p=it->second.getWorldPosition(); - ofRectangle r=getInnerRect(); - if (getInnerRect().inside(p.x,p.y)&&!it->second.isCaught) { - trigger(); - it->second.caught(); - printf("caught!\n"); - caught.push_back(&(it->second)); - caughtTime.push_back(ofGetElapsedTimef()); - sounds[3].play(); //falling down hole + if (it->second.active) { + ofVec3f p=it->second.getWorldPosition(); + ofRectangle r=getInnerRect(); + if (getInnerRect().inside(p.x,p.y)&&!it->second.isCaught) { + trigger(); + it->second.caught(); + printf("caught!\n"); + caught.push_back(&(it->second)); + caughtTime.push_back(ofGetElapsedTimef()); + sounds[3].play(); //falling down hole + } } } for (int i=0;igetCaughtPos().x,caught[i]->getCaughtPos().y,0); - glVertex3f(caught[i]->getScreenPosition().x,caught[i]->getScreenPosition().y,0); - } - glEnd(); } void trapdoor::draw() { -- cgit v1.2.3