summaryrefslogtreecommitdiff
path: root/gaunt01/src
diff options
context:
space:
mode:
authorTim Redfern <tim@eclectronics.org>2012-04-19 02:14:26 +0100
committerTim Redfern <tim@eclectronics.org>2012-04-19 02:14:26 +0100
commit09fba466578c1074bf7471bbc4615a9047237d46 (patch)
tree5741542ed77047527414218d4d192bd5b590d41b /gaunt01/src
parentc4fc08cc7238dfa310ed56e65a24df22c6bbb625 (diff)
bird chasing players
Diffstat (limited to 'gaunt01/src')
-rw-r--r--gaunt01/src/bird.cpp51
-rw-r--r--gaunt01/src/bird.h32
-rw-r--r--gaunt01/src/player.cpp1
-rw-r--r--gaunt01/src/player.h1
-rw-r--r--gaunt01/src/testApp.cpp204
-rw-r--r--gaunt01/src/testApp.h4
-rw-r--r--gaunt01/src/trapdoor.cpp27
7 files changed, 210 insertions, 110 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() {