diff options
| author | Tim Redfern <tim@eclectronics.org> | 2012-04-19 02:14:26 +0100 |
|---|---|---|
| committer | Tim Redfern <tim@eclectronics.org> | 2012-04-19 02:14:26 +0100 |
| commit | 09fba466578c1074bf7471bbc4615a9047237d46 (patch) | |
| tree | 5741542ed77047527414218d4d192bd5b590d41b | |
| parent | c4fc08cc7238dfa310ed56e65a24df22c6bbb625 (diff) | |
bird chasing players
| -rw-r--r-- | gaunt01/src/bird.cpp | 51 | ||||
| -rw-r--r-- | gaunt01/src/bird.h | 32 | ||||
| -rw-r--r-- | gaunt01/src/player.cpp | 1 | ||||
| -rw-r--r-- | gaunt01/src/player.h | 1 | ||||
| -rw-r--r-- | gaunt01/src/testApp.cpp | 204 | ||||
| -rw-r--r-- | gaunt01/src/testApp.h | 4 | ||||
| -rw-r--r-- | gaunt01/src/trapdoor.cpp | 27 | ||||
| -rw-r--r-- | morpher/morpher.layout | 8 | ||||
| -rw-r--r-- | rayhit/rayhit.layout | 2 | ||||
| -rw-r--r-- | rayhit/src/testApp.cpp | 2 |
10 files changed, 216 insertions, 116 deletions
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<int,player>& players){ +void bird::update(map<int,player>& 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<int,player>::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<playhead.size();i++) { + if ((abs(playhead[i])<(fieldofview/2))&&playdist[i]<distance) { + nearest=i; + distance=playdist[i]; + } + } + if (nearest>-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<int,player>& players); + void update(map<int,player>& players,float angle); void draw(); void drawShadow(); + void drawDebug(); string currentseq; morphmesh model; + + ofRay pointer; + ofVec3f position; + + vector<float> playang; + vector<float> playhead; + vector<float> playdip; + vector<float> playdist; + vector<ofVec3f> 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<int> ids; for(int i=0;i<blobsManager.blobs.size();i++){ ofxCvBlob blob = blobsManager.blobs.at(i); + ids.insert(blobsManager.blobs.at(i).id); ofRectangle r=blob.boundingRect; ofVec2f blobBase=ofVec2f(r.x+(r.width/2),r.y+r.height-(r.width/2)); ofVec3f pp,rp; @@ -261,105 +293,75 @@ void testApp::update(){ players[blobsManager.blobs.at(i).id].setWorldPosition(ofVec3f(rp.x,rp.y,0)); players[blobsManager.blobs.at(i).id].update(blob); //create model } + map<int,player>::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<blobsManager.blobs.size();i++){ - players[blobsManager.blobs.at(i).id].draw(); - } - colorImg.getTextureReference().unbind(); - //ofxCvBlob blob=contourFinder.blobs[i]; - //if (rectsCross(blob.boundingRect,trapDoor.getBoundingRect())){ //ALWAYS draw players? - //create outline mesh - /* - playeroutline=ofPolyline(blob.pts); - tesselator.tessellateToMesh(playeroutline,OF_POLY_WINDING_NONZERO,player,true); - for (int i=0;i<player.getNumVertices();i++) { - ofVec3f v=player.getVertex(i); - player.addTexCoord(ofVec2f(v.x,v.y)); - } - - - //figure out if player has stood on trap door - //get screen basepoint - ofRectangle r=blob.boundingRect; - ofVec2f blobBase=ofVec2f(r.x+(r.width/2),r.y+r.height-(r.width/2)); - if (trapDoor.getInnerRect().inside(blobBase.x,blobBase.y)) { - trapDoor.trigger(); - - } - else { - */ + 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<blobsManager.blobs.size();i++){ + players[blobsManager.blobs.at(i).id].draw(); + } + colorImg.getTextureReference().unbind(); - //draw players is in front of trap door or shadow - - - - + glEnable(GL_LIGHTING); - glEnable(GL_LIGHTING); + ofPushMatrix(); + ofRotate(cam_angle,1,0,0); + Bird.draw(); + ofPopMatrix(); + + glDisable(GL_LIGHTING); - ofPushMatrix(); - ofRotate(cam_angle,1,0,0); - Bird.draw(); - ofPopMatrix(); + switch(mode) { + case PLAY: - glDisable(GL_LIGHTING); break; case CALIBRATE: - ofFill(); - ofSetHexColor(0x333333); - ofSetHexColor(0xa0a0a0); - // we could draw the whole contour finder - //contourFinder.draw(360,540); - //keep running background average- how do you use a custom background - ofSetHexColor(0xffffff); - //colorImg.draw(0,0,ofGetWidth(),ofGetHeight()); - bgImg.draw(0,0,ofGetWidth(),ofGetHeight()); + ofSetHexColor(0xffffff); ofPushMatrix(); ofRotate(cam_angle,1,0,0); for (float i=0;i<=ofGetWidth();i+=ofGetWidth()/10) { @@ -374,15 +376,43 @@ void testApp::draw(){ } ofVec2f pp=screen2plane(pos); - ofSphere(pp.x,pp.y,0,5); + //ofSphere(pp.x,pp.y,0,5); ofPopMatrix(); ofPushMatrix(); ofRotate(cam_angle,1,0,0); - trapDoor.draw(); trapDoor.drawDebug(); ofPopMatrix(); - + + Bird.drawDebug(); + + ofSetHexColor(0x77ff77); + + + ofVec3f bp; + for (int i=0;i<4;i++) { + bounds[i].draw(); + if ( + bounds[i].intersect(Bird.pointer,bp)) { + char numStr[16]; + sprintf(numStr, "%4.1f", (bp-Bird.position.rotated(cam_angle,ofVec3f(1,0,0))).length()); + ofVec3f sc=cam.worldToScreen(bp); + ofDrawBitmapString(numStr, sc.x, sc.y); + } + } + + for(int i=0;i<Bird.playpos.size();i++) { + ofPushMatrix(); + ofRotate(cam_angle,1,0,0); + ofSetHexColor(0x77ffff); + ofLine(Bird.position,Bird.playpos[i]); + + ofPopMatrix(); + char numStr[10]; + sprintf(numStr, "%4.1f", Bird.playhead[i]); + ofVec3f sc=cam.worldToScreen(ofVec3f((Bird.position.x+Bird.playpos[i].x)/2,(Bird.position.y+Bird.playpos[i].y)/2,(Bird.position.z+Bird.playpos[i].z)/2).rotated(cam_angle,ofVec3f(1,0,0))); + ofDrawBitmapString(numStr, sc.x, sc.y); + } for(int i=0;i<blobsManager.blobs.size();i++){ @@ -402,8 +432,8 @@ void testApp::draw(){ // } - - + + break; diff --git a/gaunt01/src/testApp.h b/gaunt01/src/testApp.h index f2a35c0..ff53510 100644 --- a/gaunt01/src/testApp.h +++ b/gaunt01/src/testApp.h @@ -1,5 +1,7 @@ #pragma once +#include <set> + #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<int,player>& players) { map<int,player>::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;i<caught.size();i++) { @@ -148,13 +150,6 @@ void trapdoor::drawDebug() { glVertex3f(getInnerRect().x,getInnerRect().y,0); glEnd(); - ofSetHexColor(0x0000ff); - glBegin(GL_LINES); - for (int i=0;i<caught.size();i++) { - glVertex3f(caught[i]->getCaughtPos().x,caught[i]->getCaughtPos().y,0); - glVertex3f(caught[i]->getScreenPosition().x,caught[i]->getScreenPosition().y,0); - } - glEnd(); } void trapdoor::draw() { diff --git a/morpher/morpher.layout b/morpher/morpher.layout index 19ecc35..82acfc0 100644 --- a/morpher/morpher.layout +++ b/morpher/morpher.layout @@ -7,8 +7,8 @@ <File name="config.make" open="0" top="0" tabpos="9"> <Cursor position="478" topLine="0" /> </File> - <File name="src/morphmesh.cpp" open="1" top="1" tabpos="6"> - <Cursor position="3075" topLine="87" /> + <File name="src/morphmesh.cpp" open="1" top="0" tabpos="6"> + <Cursor position="3075" topLine="258" /> </File> <File name="src/morphmesh.h" open="1" top="0" tabpos="5"> <Cursor position="2727" topLine="67" /> @@ -19,8 +19,8 @@ <File name="src/normBindTexture.h" open="1" top="0" tabpos="1"> <Cursor position="0" topLine="0" /> </File> - <File name="src/testApp.cpp" open="1" top="0" tabpos="4"> - <Cursor position="524" topLine="0" /> + <File name="src/testApp.cpp" open="1" top="1" tabpos="4"> + <Cursor position="524" topLine="14" /> </File> <File name="src/testApp.h" open="1" top="0" tabpos="3"> <Cursor position="597" topLine="0" /> diff --git a/rayhit/rayhit.layout b/rayhit/rayhit.layout index 63b7198..369bfe7 100644 --- a/rayhit/rayhit.layout +++ b/rayhit/rayhit.layout @@ -11,7 +11,7 @@ <Cursor position="0" topLine="0" /> </File> <File name="src/testApp.cpp" open="1" top="1" tabpos="4"> - <Cursor position="1148" topLine="0" /> + <Cursor position="1771" topLine="18" /> </File> <File name="src/testApp.h" open="0" top="0" tabpos="0"> <Cursor position="641" topLine="0" /> diff --git a/rayhit/src/testApp.cpp b/rayhit/src/testApp.cpp index 5e1bd14..ff34033 100644 --- a/rayhit/src/testApp.cpp +++ b/rayhit/src/testApp.cpp @@ -5,7 +5,7 @@ void testApp::setup(){ ofVec3f centre=ofVec3f(ofGetWidth()/2,0,0); //ofGetHeight(),0); ofVec3f normal=ofVec3f(0,0,-1); ray=ofRay(); - plane=ofPlane(centre,normal); + plane=ofPlane(centre,normal,normal,ofVec2f(ofGetWidth(),ofGetWidth())); plane.color=ofColor(255,255,255); //setup that works for a new camera. but can we match the existing camera, must be possible! |
