PDA

View Full Version : Unable to get the cat to turn towards the rat



huimin
04-08-2008, 07:29 AM
Hi,

I can't get the cat to turn towards the rat. I had attached the code for reference. Can anyone enlighten me on this? Thanks.

#pragma warning(disable: 4068) //ignore warning given by MSVis about some opengl settings

#include <windows.h>
#include <math.h>
#include "gl.h"
#include "glu.h"
#include "glut.h"
#include "csc3406.h"

//used for glut init
//needed but don't really do anything!
int argc = 1;
char *argv = "";

FileReadWriter fileHandler;
WindowSetup settings;

//structure for vectors
typedef struct
{
GLfloat x,y;

void normalise()
{
float length = sqrt(pow(x,2) + pow(y,2));
x = x/length;
y = y/length;
}
} vector;


//structure for animated objects
typedef struct
{
GLfloat x, y; //position on screen
vector velocity; //current facing direction
GLfloat speed; //rate of movement
} animObject;

//create a cat, rat {x, y, velocity, speed}
animObject cat = {100.0,100.0,0.0,1.0,0.05};
animObject rat = {500.0,400.0,0.0,1.0,0};

void myInit(void)
{
glClearColor(0.0,0.0,0.0,0.0); // set black background colour
glColor3f(1.0,1.0,1.0); // set white foreground colour
glClear(GL_COLOR_BUFFER_BIT); // clear the screen

//setup world coordinates
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(settings.WORLD_LEFT, settings.WORLD_RIGHT,
settings.WORLD_BOTTOM, settings.WORLD_TOP);

//setup window coordinates
glViewport(settings.VIEW_LEFT, settings.VIEW_BOTTOM,
settings.VIEW_RIGHT - settings.VIEW_LEFT,
settings.VIEW_TOP - settings.VIEW_BOTTOM);

glClear(GL_COLOR_BUFFER_BIT);

}

double turnAngle(vector v1, vector v2)
{
double angle = acos(v1.x*v2.x + v1.y*v2.y) * 180/3.14;
if( (v1.x*v2.y - v1.y*v2.x) < 0)
return -angle;
else if ((v1.x*v2.y - v1.y*v2.x) > 0)
return angle;
return 0;
}

void myMouse(int button, int state, int x, int y)
{

}


void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT); // clear the screen

vector v;
v.x = rat.x - cat.x;
v.normalise();
cat.x += v.x * cat.speed;
cat.y += v.y * cat.speed;

//draw rat - little pink square
glColor3f(1.0,0.5,0.5); //pink
glPushMatrix();
glRecti(rat.x, rat.y, rat.x + 10, rat.y + 10);
glPopMatrix();

//draw cat - a few brown shapes
glColor3f(0.8,0.5,0.2); //brown

glPushMatrix();
glTranslated(cat.x,cat.y,0);
glRotated(turnAngle(cat.velocity,v),0,0,1);
glTranslated(-cat.x,-cat.y,0);
//head
glRecti(cat.x + 8, cat.y - 40, cat.x + 32, cat.y - 60);
//ears
glBegin(GL_LINE_STRIP);
glVertex2i(cat.x + 8, cat.y - 60);
glVertex2i(cat.x + 10, cat.y - 70);
glVertex2i(cat.x + 20, cat.y - 60);
glVertex2i(cat.x + 25, cat.y - 70);
glVertex2i(cat.x + 32, cat.y - 60);
glEnd();
//body
glRecti(cat.x, cat.y, cat.x + 40, cat.y - 40);
//tail
glBegin(GL_LINE_STRIP);
glVertex2i(cat.x + 20, cat.y);
glVertex2i(cat.x + 22, cat.y + 10);
glVertex2i(cat.x + 15, cat.y + 30);
glEnd();
glPopMatrix();

glutSwapBuffers();
}

void myKeyboardFunc(unsigned char key, int x, int y)
{
switch(key)
{
case '3':
{
fileHandler.captureScreen(settings.SCREENWIDTH, settings.SCREENHEIGHT,"capture.bmp");
break;
}
case 27:
{
exit(1);
}
}
}
void myReshape(GLsizei W, GLsizei H)
{
if(settings.ASPECT_RATIO > W/H) //use global window aspect ratio
glViewport(0, 0, W, W/settings.ASPECT_RATIO);
else
glViewport(0, 0, H*settings.ASPECT_RATIO, H);
}

void myIdle()
{
glutPostRedisplay();
}


int main()
{
glutInit(&argc, &argv); // initialize the toolkit
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); // set display mode

//load window setups from ini file
fileHandler.loadSetupFile("setup.ini", &settings);

glutInitWindowSize(settings.SCREENWIDTH, settings.SCREENHEIGHT); // set window size
glutInitWindowPosition(100, 150); // set window position on screen
glutCreateWindow("Cat and Mouse"); // open the screen window and set the name
glutDisplayFunc(myDisplay); // register redraw function
glutReshapeFunc(myReshape); // register reshape function
glutMouseFunc(myMouse); //register mouse function
myInit();
glutIdleFunc(myIdle); // do this when nothing else is happening
glutMainLoop(); // go into a perpetual loop

return 1;
}