Skip to content

Commit

Permalink
fishB and fishA orientation
Browse files Browse the repository at this point in the history
  • Loading branch information
albf committed May 5, 2014
1 parent e3e9c67 commit 85c95fa
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 16 deletions.
26 changes: 23 additions & 3 deletions fishA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ fishA::fishA() {
tailAngleInc = 1.0f;

// Fish Speed
xInc = 0.1;
yInc = 0;
zInc = 0.05;
xInc = 0.0;
yInc = -0.1;
zInc = 0.0;
}

void fishA::draw(void) {
Expand All @@ -38,6 +38,9 @@ void fishA::draw(void) {

glPushMatrix();
glTranslatef(x,y,z);
getCrossProduct(xInc,yInc,zInc,1,0,0); // get cross product
std::cout << "getangle: " << getAngle(xInc,yInc,zInc,0,0,1) << std::endl; // debug
glRotatef(getAngle(1,0,0,xInc,yInc,zInc), xcp, ycp, zcp); // rotate to look to where it is moving

// set up the material properties (only front needs to be set)
glMaterialfv(GL_FRONT, GL_AMBIENT, material);
Expand Down Expand Up @@ -116,6 +119,23 @@ void fishA::printXYZ(void) {
std::cout << std::endl;
}

float fishA::vectorDistance(float x, float y, float z) {
return (float)sqrt((x*x)+(y*y)+(z*z));
}

float fishA::getAngle(float x, float y, float z, float a, float b, float c) {
float dot_product = (x*a) + (y*b) + (z*c);
float div = vectorDistance(x,y,z)*vectorDistance(a,b,c);
float arc = (float) acos(dot_product/div);
return arc* 180.0 / PI;
}

void fishA::getCrossProduct(float x, float y, float z, float a, float b, float c) {
xcp = (y*c)-(z*b);
ycp = (z*a)-(x*c);
zcp = (x*b)-(y*a);
}

GLfloat fishA::vertex[] ={
// 2 | | |
0.0f, 0.0f, 0.0f, 0.5f, -0.5f, 0.0f, 0.5f, 0.0f, 0.45f,
Expand Down
6 changes: 6 additions & 0 deletions fishA.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <iostream>
#include <stdio.h>
#define FISH_TEXTURE 2
#define PI 3.14159

class fishA : public Renderable {
public:
Expand All @@ -16,6 +17,8 @@ class fishA : public Renderable {
GLfloat rx,ry, rz; // rotation angle of object
GLfloat sx,sy,sz; // scale of object
GLfloat xInc, yInc, zInc; // speed of object
float xcp, ycp, zcp; // Variables for cross-product


static unsigned int textures[2]; /// texture id array
static GLUquadricObj *quadric; /// quadric object for all renderables
Expand All @@ -32,6 +35,9 @@ class fishA : public Renderable {
static GLfloat shininess;

void drawSide();
float vectorDistance(float x, float y, float z);
float getAngle(float x, float y, float z, float a, float b, float c);
void getCrossProduct(float x, float y, float z, float a, float b, float c);

};

Expand Down
38 changes: 28 additions & 10 deletions fishB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@
#include <GLUT/glut.h>
#endif

#define FOV_RADIUS 3 // Radius of field of view
#define FOV_ANGLE 110 // Radius of field of view angle

#include "fishB.h"
#include "math.h"
#include <iostream>

fishB::fishB() {
x = 1;
y = 1;
z = 1;

xInc = 0;
yInc = -0.1;
zInc = 0;
xInc = 0.1;
yInc = 0.1;
zInc = 0.0;

angle = 40;
Autorun = 0;
Expand All @@ -39,8 +38,10 @@ void fishB::draw(void) {
z += zInc;
glPushMatrix();

glTranslatef(x,y,z);
glRotatef(90, 1,0,0); // correction
glTranslatef(x,y,z); // move to where it is right now
getCrossProduct(xInc,yInc,zInc,0,0,-1); // get cross product
// std::cout << "getangle: " << getAngle(xInc,yInc,zInc,0,0,1) << std::endl; // debug
glRotatef(getAngle(xInc,yInc,zInc,0,0,-1), xcp, ycp, zcp); // rotate to look to where it is moving

glEnable(GL_LIGHTING);

Expand Down Expand Up @@ -81,13 +82,13 @@ void fishB::draw(void) {
glDisable(GL_LIGHTING);

// Radius of influence
if ( true ) {
if ( false ) {
glColor3f( 0.2, 0.2, 0.2 );
glutWireSphere(FOV_RADIUS, 10, 3 );
}

// Draw Axis
if ( true ) {
if ( false ) {
glColor3f( 1, 0, 0 );
glBegin( GL_LINES );
glVertex3f(0,0,0);
Expand All @@ -108,7 +109,7 @@ void fishB::draw(void) {
}

// Draw Blind Cone
if ( true ) {
if ( false ) {
glPushMatrix();
glTranslatef( 0, 0, -FOV_RADIUS );
glutWireCone( FOV_RADIUS * tan( ( 180 - FOV_ANGLE )* PI / 180.0 ), FOV_RADIUS, 4, 1 );
Expand All @@ -124,3 +125,20 @@ void fishB::printXYZ(void) {
std::cout << "z " << z << std::endl;
std::cout << std::endl;
}

float fishB::vectorDistance(float x, float y, float z) {
return (float)sqrt((x*x)+(y*y)+(z*z));
}

float fishB::getAngle(float x, float y, float z, float a, float b, float c) {
float dot_product = (x*a) + (y*b) + (z*c);
float div = vectorDistance(x,y,z)*vectorDistance(a,b,c);
float arc = (float) acos(dot_product/div);
return arc* 180.0 / PI;
}

void fishB::getCrossProduct(float x, float y, float z, float a, float b, float c) {
xcp = (y*c)-(z*b);
ycp = (z*a)-(x*c);
zcp = (x*b)-(y*a);
}
12 changes: 9 additions & 3 deletions fishB.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,27 @@
#include <iostream>
#include <stdio.h>
#define PI 3.14159
#define FOV_RADIUS 3 // Radius of field of view
#define FOV_ANGLE 110 // Radius of field of view angle

class fishB : public Renderable {
public:
fishB();
virtual void draw();
void printXYZ(void);

GLfloat x,y,z; // For Movement
GLfloat xInc, yInc, zInc; // speed of object
float x,y,z; // For Movement
float xInc, yInc, zInc; // speed of object
float colour[4];
float xcp, ycp, zcp; // Variables for cross-product

private:
float angle; // For Function animation
float Autorun; // For Movement Autorun


float vectorDistance(float x, float y, float z);
float getAngle(float x, float y, float z, float a, float b, float c);
void getCrossProduct(float x, float y, float z, float a, float b, float c);
};

#endif
13 changes: 13 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,21 @@ int main(int argc, char** argv) {
fish2.y=5;
fish2.z=0;

fishB fish3;
fishB fish4;

fish3.x=0;
fish3.y=0;
fish3.z=0;

fish4.x=0;
fish4.y=-1;
fish4.z=0;

viewer.addRenderable(&fish);
viewer.addRenderable(&fish2);
viewer.addRenderable(&fish3);
viewer.addRenderable(&fish4);

//fish.printXYZ();

Expand Down

0 comments on commit 85c95fa

Please sign in to comment.