Jump to content
  • Advertisement

eskimo456

Member
  • Content Count

    32
  • Joined

  • Last visited

Community Reputation

100 Neutral

About eskimo456

  • Rank
    Member
  1. Hi there I am attempting to shoot a ray from the middle of the camera in openGL using the gluLookAt function. I from what I understand that when you move the " camera " You actually rotate the whole world? If the box has the co-ordinate lowX = -50, high X = 40, lowY = -10,highY = 10,highZ = -200,lowZ = -250 I am attempting to fire a ray from the middle of the camera and test to see if it hits a box or not. It seems to give false positives however. If the camera position is in the middle of the box and I am looking completely to the right for instance "99", z = "-10" then both the X and Z values of my ray are true even though the box is no where on screen. I am defining a box as highX,lowX,highY,lowY,highZ,lowZ; I then check if the direction vector is 0, if it is then because the bounding box is axis aligned it has to parallel so check if the start point is between the box. if not calculate the intersection difference by doing (highX - rayorigionX)/rayDirectionX. (lowX-rayorigionX/rayDirectionX) Then swap values if necessary. Check that nearIntersection < farIntersection and farIntersection >0. repeat for Y and Z. Do I need to check it against normalized planes instead of the box? Or is the false positives purely to do with having to find the middle of the viewing point in openGl? Many thanks
  2. Hi I am attempting to use a ray to box intersection within my program. It initially seems fine and seems to work ok for the object I am moving around my scene. It can calculate how far away from the object I am etc. I am also attempting to use it within a camera. The idea being to be able to fire a ray in the direction the camera is facing. If this hits the box return true else return false. I am assuming it is something I am doing with the camera that is causing me errors? Maybe to do with the way I am looking down the axis? I seem to either be able to get false positive most of the time or no positive. The code for my ray to box intersection is bool Collision::rayToBoxCollision(float boxX1,float boxX2, float boxY1, float boxY2, float boxZ1, float boxZ2, float rayStartX, float rayStartY, float rayStartZ,float rayDirectionX,float rayDirectionY, float rayDirectionZ, float angleOfRotation) { float tNear = -500; float tFar = 500; float T1Final; float T2Final; float T1; float T2; j = 0; // keep track of box edges float boundingBox[] = {boxX1, boxX2,boxY1,boxY2,boxZ1,boxZ2}; // set up bounding box points float raystartPoint[] = {rayStartX,rayStartY,rayStartZ}; // ray starting vector float rayDirectionVector[] = {rayDirectionX,rayDirectionY,rayDirectionZ}; // ray direction vector bool collision[] = {0,0,0}; for(int i = 0; i<3; i++) // loop around all arrays { tNear = -500; tFar = 500; if(raystartPoint == 0) // if ray is parrallel to box { if(raystartPoint < boundingBox[j] || raystartPoint > boundingBox[j+1]) // if its not within the box boundry collision = false; else collision = true; } if(rayDirectionVector == 0) rayDirectionVector += 1; T1 = boundingBox[j] - raystartPoint; T1Final = T1/rayDirectionVector; // calculate intersection Distance between bottom T2 = boundingBox[j+1] - raystartPoint; T2Final = T2/rayDirectionVector; //calculate intersection distance top if(T1Final>T2Final) // if top and bottom are the wrong way round swap { float temp = T2Final; T2Final = T1Final; T1Final = temp; } if(T1Final>tNear) //clamp near collision to t1 { tNear = T1Final; } if(T2Final < tFar) // clamp far collision to t2 { tFar = T2Final; } rayTfar = tFar; rayTNear = tNear; rayArray = tNear; rayArray[i+1] = tFar; j += 2; if(tNear > tFar) collision = false; else if(tFar<0) collision = false; else collision = true; } if(collision[0] == true && collision[1] == true && collision[2] == true) // check x,y and z axis are all true return true; else return false; } Is there something wrong here that I have missed? or is it the way I am shooting the ray from the camera? Any help greatly appreciated
  3. Hi I am using a bounding box as an initial quick test once this has collided, I am them attempting to use a point to plane test with a barycentric traingle to get precise collision with a wall. I have a formula that seems to work on paper but when I use it in the computer it looks as though it is the wrong way around. My formula is.. s = (p-a).N; c.x = p.x-s*N.x c.y = p.y,s*N.y c.z = p.z,s*N.z p is point of interest, a is point on plane, and n is normalised normal. I get an answer of 0 initially but if I move my character into a wall I then get an answer of 50. The point to plane test gives a distance of 50. I need the boat to be of distance 50, and as I move to the wall for it to decrease to 0. Then do an if(distance<0) statement for collision. Am I accidnetly calculating the character position? Should I rearrange it to something like p.x-s*n.x-c.x? to give me the distance away from the wall? Many thanks
  4. eskimo456

    using structs as input arguments

    Quote:Original post by SiCrane You have a circular dependency problem. See problem two in this article. Awsome I have had a look at the article and it suggests removing my #include and just having my class Enviroment; I have done this in both classes having my class Enviroment and class Collision in the .h files. I have then included he #include "Enviroment.h" and #include "Collision.h" in my .cpp file as it says in the document. I still get the errors I did earlier??? As my struct is in a class do I need to do something like class Enviroment::NormalArray;
  5. eskimo456

    using structs as input arguments

    yep sorry that is the full code the problem code is in my Collision header file #pragma once #include <windows.h> // Header File For Windows #include <gl\gl.h> // Header File For The OpenGL32 Library #include <gl\glu.h> // Header File For The GLu32 Library #include "console.h" #include "Boat_Pos.h" #include "Calculation.h" #include "Enviroment.h" class Boat_Pos; class Enviroment; //class Enviroment::NormalArray; static class Collision { public: Collision(){} ~Collision(){} static float * CalculateNormal(Enviroment::NormalArray normalArray); private: }; the errors I get is "undefined type Enviroment" error code C2027 in visual studio 2010. I'm guessing it cant find Enviroment::NormalArray to allow it to be a return type? The other error is related saying "syntax error: identifier NormalArray". I am using it in this my Enviroment CPP file void Enviroment::InitiliseEnviroment(float wallPosition[]) { Collision::CalculateNormal(wallNormal); } The function is defined like this float * Collision::CalculateNormal(Enviroment::NormalArray normalArray) { float ux = normalArray.X1 - normalArray.X1; float uy = normalArray.Y2 - normalArray.Y1; float uz = normalArray.Z1 - normalArray.Z1; float vx = normalArray.X2 - normalArray.X1; float vy = normalArray.Y2 - normalArray.Y1; float vz = normalArray.Z2 - normalArray.Z1; float tempNormal[3]; tempNormal [0] = ((vy*uz) - (vz*uy))*-1; tempNormal [1] = ((vz*ux) - (vx*uz)); tempNormal [2] = ((vx*uy) - (vy*ux)); std::cout<<"The normal1 is "<<tempNormal [0]<<std::endl; std::cout<<"The normal2 is "<<tempNormal [1]<<std::endl; std::cout<<"The normal3 is "<<tempNormal [2]<<std::endl; return tempNormal; } When I do normalArray. it brings up a list of all my possible options etc so I am assuming that this is working fine. I am only gettin 2 errors both I mentioned earlier in the header file.
  6. eskimo456

    using structs as input arguments

    Sorry I'm always worried about posting loads of code. As far as I can see I have included the headers required My full Enviroment header is #pragma once #include <windows.h> // Header File For Windows #include <gl\gl.h> // Header File For The OpenGL32 Library #include <gl\glu.h> // Header File For The GLu32 Library #include "tgaload.h" #include "console.h" #include "Collision.h" #include "Boat_Pos.h" class Boat_Pos; class Enviroment { public: struct Wall { float X1; float Z1; float X2; float Z2; }; //struct that holds walls normals struct NormalArray { float X1; float Y1; float Z1; float X2; float Y2; float Z2; }; struct NormalizedWall { float X; float Y; float Z; }; //function prototype static Wall WallPos(float wallPosition[]); static NormalArray GetNormal(float xNormal1,float yNormal1,float zNormal1,float xNormal2,float yNormal2,float zNormal2); void drawWater(int StartXwater, int StartYwater, int StartZwater, int EndXwater, int EndYWater, int EndZWater, int waterPosition); static void drawWall(float wallPosition[],Boat_Pos playerBoat); static float * initArray(float wallArray[]); static void InitiliseEnviroment(float wallPosition[]); //static Enviroment::NormalizedWall InitiliseEnviroment(float wallPosition[]); GLuint water; GLuint wall; private: }; My full header for my Collsion is #pragma once #include <windows.h> // Header File For Windows #include <gl\gl.h> // Header File For The OpenGL32 Library #include <gl\glu.h> // Header File For The GLu32 Library #include "console.h" #include "Boat_Pos.h" #include "Calculation.h" #include "Enviroment.h" class Boat_Pos; class Enviroment; static class Collision { public: Collision(){} ~Collision(){} void DrawSphere(int x1,int z2, int radius); static void DrawBox(int posX1, int PosX2, int posY1, int posY2, int posZ1, int posZ2);//draw bounding boxes static bool BoundingBox(float wall[],Boat_Pos playerBoat);//check bounding collision int boxPos(int posX1, int posX2,int posZ1,int posZ2);//calculate box position static double DotProduct(int Value1X, int Value2X,int Value1Y, int Value2Y, int Value1Z, int Value2Z); double CrossProduct(double U1, double V1, double U2, double V2, double U3, double V3); //were problem is static float * CalculateNormal(Enviroment::NormalArray wallToCalculate); static void NormaliseNormal(float normalArray[],float corner[]); static float PointToPlaneTest(Boat_Pos player,float pointOnPlane[],float normalValues[]); static float PlaneEquation(float point1[],float point2[], float point3[],float point[]); static bool BarryCentricEquation(float triA[],float triB[], float triC[],float point[]); double posX1; double posX2; double posY1; double posY2; double posZ1; double posZ2; bool collision; bool bccCollision; float dist; //static float normal[3]; private: }; This is where my error is occouring I am actucally running it in my Enviroment class cpp file #include "Enviroment.h" Enviroment::Wall Enviroment::WallPos(float wallPosition[]) { Wall wall; wall.X1 = wallPosition[0]; wall.Z1 = wallPosition[1]; wall.X2 = wallPosition[2]; wall.Z2 = wallPosition[3]; std::cout<<"this is my struct "<<wall.X1<<std::endl; std::cout<<"this is my struct "<<wall.Z1<<std::endl; std::cout<<"this is my struct "<<wall.X2<<std::endl; std::cout<<"this is my struct "<<wall.Z2<<std::endl; return wall; }; Enviroment::NormalArray Enviroment::GetNormal(float xNormal1,float yNormal1,float zNormal1,float xNormal2,float yNormal2,float zNormal2) { NormalArray wallNormal; wallNormal.X1 = xNormal1; wallNormal.Y1 = yNormal1; wallNormal.Z1 = zNormal1; wallNormal.X2 = xNormal2; wallNormal.Y2 = yNormal2; wallNormal.Z2 = zNormal2; return wallNormal; } void Enviroment::drawWater(int StartXwater, int StartYwater, int StartZwater, int EndXwater, int EndYWater, int EndZWater, int waterPosition) { if(waterPosition == 0) { water = tgaLoadAndBind("waterr.tga", TGA_ALPHA); glEnable(GL_TEXTURE_2D); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_REPEAT); glBindTexture(GL_TEXTURE_2D, water); glPushMatrix(); glBegin(GL_POLYGON); glTexCoord2f(0,0); glVertex3f(StartXwater, 0, StartZwater); glTexCoord2f(50,0);glVertex3f(EndXwater, 0, StartZwater); glTexCoord2f(50,100);glVertex3f(EndXwater, 0, EndZWater); glTexCoord2f(0,100);glVertex3f(StartXwater, 0, EndZWater); glEnd(); glDisable(GL_BLEND); glDisable(GL_TEXTURE_2D); glPopMatrix(); } if(waterPosition == 1) { water = tgaLoadAndBind("waterr.tga", TGA_ALPHA); glEnable(GL_TEXTURE_2D); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_REPEAT); glBindTexture(GL_TEXTURE_2D, water); glPushMatrix(); glBegin(GL_POLYGON); glTexCoord2f(0,0); glVertex3f(StartXwater, 0, StartZwater); glTexCoord2f(0,300);glVertex3f(EndXwater, 0, StartZwater); glTexCoord2f(100,300);glVertex3f(EndXwater, 0, EndZWater); glTexCoord2f(100,0);glVertex3f(StartXwater, 0, EndZWater); glEnd(); glDisable(GL_BLEND); glDisable(GL_TEXTURE_2D); glPopMatrix(); } } void Enviroment::drawWall(float wallPosition[], Boat_Pos playerBoat) { glPushMatrix(); glBegin(GL_POLYGON); glColor3f(0.7,0.7,0.7); glTexCoord2f(0,0); glVertex3f(wallPosition[0], 0, wallPosition[1]); glTexCoord2f(1,0); glVertex3f(wallPosition[0], 3, wallPosition[1]); glTexCoord2f(1,1); glVertex3f(wallPosition[2], 3, wallPosition[3]); glTexCoord2f(0,1); glVertex3f(wallPosition[2], 0, wallPosition[3]); glEnd(); } void Enviroment::InitiliseEnviroment(float wallPosition[]) { Enviroment::Wall wallToNormalise = Enviroment::WallPos(wallPosition); std::cout<<"Wall Position 1 is "<<wallToNormalise.X1<<std::endl; std::cout<<"Wall Position 2 is "<<wallToNormalise.Z1<<std::endl; std::cout<<"Wall Position 3 is "<<wallToNormalise.X2<<std::endl; std::cout<<"Wall Position 4 is "<<wallToNormalise.Z2<<std::endl; Enviroment::NormalArray wallNormal; /*wallNormal.X1 = wallToNormalise.X1; wallNormal.Z1 = wallToNormalise.Z1; wallNormal.X2 = wallToNormalise.X2; wallNormal.Z2 = wallToNormalise.Z2;*/ //this is where I am using my collision Collision::CalculateNormal(wallNormal); } If you would like any further code please let me know. I am only using the code in this section though. This is being called in the init() function in the main.
  7. Hi Sorry more struct questions. I have now managed to create some structs that get the information and calculate the information I would like initially. My problem is now I would like to take in a struct into a function in another cpp. I get an error C2027,error C2061, error C2660 It says undefined type, and syntax error identifier. I am going from my enviroment class header class Enviroment { public: struct Wall { float X1; float Z1; float X2; float Z2; }; //struct that holds walls normals struct NormalArray { float X1; float Y1; float Z1; float X2; float Y2; float Z2; }; struct NormalizedWall { float X; float Y; float Z; }; //function prototype static Wall WallPos(float wallPosition[]); static NormalArray GetNormal(float xNormal1,float yNormal1,float zNormal1,float xNormal2,float yNormal2,float zNormal2); void drawWater(int StartXwater, int StartYwater, int StartZwater, int EndXwater, int EndYWater, int EndZWater, int waterPosition); static void drawWall(float wallPosition[],Boat_Pos playerBoat); static float * initArray(float wallArray[]); static void InitiliseEnviroment(float wallPosition[]); //static Enviroment::NormalizedWall InitiliseEnviroment(float wallPosition[]); GLuint water; GLuint wall; private: }; and trying to use it as an input to my collision class static class Collision { public: Collision(){} ~Collision(){} void DrawSphere(int x1,int z2, int radius); static void DrawBox(int posX1, int PosX2, int posY1, int posY2, int posZ1, int posZ2); static bool BoundingBox(float wall[],Boat_Pos playerBoat); int boxPos(int posX1, int posX2,int posZ1,int posZ2); static double DotProduct(int Value1X, int Value2X,int Value1Y, int Value2Y, int Value1Z, int Value2Z); double CrossProduct(double U1, double V1, double U2, double V2, double U3, double V3); //this is where my error is static float * CalculateNormal(Enviroment::NormalArray normalArray); //this is where my error is static void NormaliseNormal(float normalArray[],float corner[]); static float PointToPlaneTest(Boat_Pos player,float pointOnPlane[],float normalValues[]); static float PlaneEquation(float point1[],float point2[], float point3[],float point[]); static bool BarryCentricEquation(float triA[],float triB[], float triC[],float point[]); double posX1; double posX2; double posY1; double posY2; double posZ1; double posZ2; bool collision; bool bccCollision; float dist; //static float normal[3]; private: }; Do I need to import my struct seperate to my class? [Edited by - eskimo456 on December 11, 2010 5:21:56 PM]
  8. eskimo456

    returning structs across classes

    I could cry with my idiocy. I got rid of everything except the test::GetNormal(); and it works. That makes complete sense and is fine. Thanks a lot for all your help Just a few last questions with regards to structs. I understand they pass by value? so is it possible to have something like struct student. student.grade = a; for(int i = 0; i<30 i++) { student. student.grade = a; a = 70; } Would this generate 30 different structs all with the same name/attributes? Or would you have to name them all seperatly? If you wanted to change a variable in one of the structs would you have to pass by memory? Many thanks
  9. eskimo456

    returning structs across classes

    Sorry my stupidiyt I have several things open looking at code from one to the other its a whole mess. I have started agian from scratch with everything closed except for this :) I have a console,mainopenGL, and my test. my test.h is #pragma once #include <windows.h> // Header File For Windows #include <gl\gl.h> // Header File For The OpenGL32 Library #include <gl\glu.h> // Header File For The GLu32 Library #include "console.h" static class test{ public: struct NormalArray { float x; float y; float z; }; //function prototype static NormalArray GetNormal(); private: }; my cpp is just #include "test.h" test::NormalArray test::GetNormal() { NormalArray test; test.x = 5; test.y = 12; test.z = 20; std::cout<<"sample struct is "<<test.x<<std::endl; return test; } my main is #include <windows.h> // Header File For Windows #include <gl\gl.h> // Header File For The OpenGL32 Library #include <gl\glu.h> // Header File For The GLu32 Library #include "console.h" #include "test.h" ConsoleWindow console; #include <iostream> using namespace std; int screenWidth=640, screenHeight=480; bool keys[256]; double spin=0; double mouse_x, mouse_y; bool LeftPressed = false; //OPENGL FUNCTION PROTOTYPES void display(); //called in winmain to draw everything to the screen void reshape(); //called when the window is resized void init(); //called in winmain when the program starts. void processKeys(); //called in winmain to process keyboard input void update(); //called in winmain to update variables void drawCube(); //draws a cube of unit size to the screen, with centre at the origin. /************* START OF OPENGL FUNCTIONS ****************/ void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0f,(GLfloat)screenWidth/(GLfloat)screenHeight,0.1f,1000.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0,0,-10); glPushMatrix(); glRotated(spin,0,1,0); glColor3f(0,0,1); drawCube(); glPopMatrix(); test::NormalArray test::GetNormal() { NormalArray test; test.x = 5; test.y = 12; test.z = 20; std::cout<<"test struct is "<<test.x<<std::endl; return test; } glFlush(); } There is more mian but its just the windows code stuff. This works fine as it does draw a blue cube as it should just to make sure it works. My test.h and test.cpp seem fine When I try to load it in the main though I get the error test::GetNormal() Local function definitions are illegal? It is static so I shouldn't need any objects do I need to do something to allow my main draw function to access it? Sorry about before decided to start fresh
  10. eskimo456

    returning structs across classes

    header file #pragma once #include <windows.h> // Header File For Windows #include <gl\gl.h> // Header File For The OpenGL32 Library #include <gl\glu.h> // Header File For The GLu32 Library #include "console.h" class test{ public: struct NormalArray { float x; float y; float z; }; //function prototype static NormalArray GetNormal(); private: }; cpp file #include "test.h" Enviroment::NormalArray Enviroment::GetNormal() { NormalArray test; test = 5; sample_struct.normY = 12; sample_struct.normZ = 20; std::cout<<"sample struct is "<<sample_struct.normX<<std::endl; return sample_struct; } I get the error 140 IntelliSense: member function "Enviroment::GetNormal" may not be redeclared outside its class c:\users\phil\desktop\game draft 3\win32opengl2template\enviroment.cpp 132 38 win32template I now get errors to do with accessing test but I am assuming thats because of the other error. Just read my error message it may still be linking to my other stuff I'll start again and see what happens
  11. eskimo456

    returning structs across classes

    I have renamed the struct and have took out the typeDef I have know tried to use the accessors as you suggested Test::NormalArray Test::GetNormal() //I get an error telling that function cannot be redeclared outside of the class? { } If I do Test::NormalArray:: it finds my X,Y,Z values but I cant get it to allow me to do the actual function?
  12. I am trying to pass a few variables from one class to another using structs as opposed to arrays as suggested. When I attempt to create the function in my cpp file it tells me that a ; is required? My header file has a struct with the function prototype class test { public: typedef struct NormalArray { float normX; float normY; float normZ; } SAMPLE_STRUCT; //function prototype SAMPLE_STRUCT GetNormal(); private: }; I am then trying to use this in my cpp file. The cpp file can find the fields and items associated with the struct and seems to be OK except it wont compile. #include "test.h" SAMPLE_STRUCT GetNormal() { //need ; here? SAMPLE_STRUCT sample_struct; sample_struct.normX = 5; sample_struct.normY = 12; sample_struct.normZ = 20; std::cout<<"sample struct is "<<sample_struct.normX<<std::endl; return sample_struct; } I am just trying to use a really simple example just to put the values in print the mout then return the struct. I want to them print this out again to ensure that the values are passed correctly etc. Do I need to pass by reference using pointers? Am I missing something obvious? Many thanks
  13. Hello again I was wandering about how larger projects are actually structured in the real world. I know of the use of UML diagrams but am curious as to how the design of large projects are done. I know this is a really ambigious question. My project which is really quite small is already having further structuring problems although I am trying to sort them ASAP. I have a class called Collisions - this takes care of normalising,point to plane,bounding boxes and barycentric co-ordinates testing. This is linked directly to my enviroment class - this draws things to screen and checks for collisions between the player and the enviroment. This is finally loaded in my main that passes the player co-ordinates and wall co-ordinates to it. My main then also interacts with my controller class and camera class. Does this structure sound reasonable? It seems to work pretty well so far I understand that in big projects things will tend to get buried such as Linked list or VectorMath classes etc. Are there any good tips on designing how the code should run. Should you start with the main and figure out what needs to interact together? Or do you do as much work as possible only adding it into the main at the end? Really interested in how the real world works and software engineering design approaches. Many thanks
  14. eskimo456

    array not passing the values it should

    After much rewriting going over and sorting stuff out I have finally got the information to pass from the main to my enviroment class using the struct as opposed to any arrays. I am now just going to pass the struct to my collision class and calculate everything. I would like to say a big thank you to the hlep provided and everyone who looked. I will end this topic now and hopefully be able to sort everything out. If I have any more problems I will start a new topic hopefully not to soon though :D Many thanks again
  15. eskimo456

    array not passing the values it should

    Thanks for the help its so frustrating that when I put all the code in the main it worked yet trying to split it up so can be reused and easier to read etc it broke it now is a complete mess I will post the code in seperate boxes to show you the classes I am trying to get this to run on. My main class is this... The idea is to be able to declare wall here in a line and then be able to pass values to other classes. #include <windows.h> // Header File For Windows #include <gl\gl.h> // Header File For The OpenGL32 Library #include <gl\glu.h> // Header File For The GLu32 Library #include <vector> #include "console.h" #include "Boat_Pos.h" #include "360_Controller.h" #include "Camera.h" #include "Calculation.h" #include "Enviroment.h" #include <math.h> #include "Collision.h" #include "Controller.h" ConsoleWindow console; #include <iostream> using namespace std; int screenWidth=640, screenHeight=480; bool keys[256]; double spin=0; double mouse_x, mouse_y; bool LeftPressed = false; //double speed = 0; //CXBOXController* Player1; //OPENGL FUNCTION PROTOTYPES void display(); //called in winmain to draw everything to the screen void reshape(); //called when the window is resized void init(); //called in winmain when the program starts. void processKeys(); //called in winmain to process keyboard input void update(); //called in winmain to update variables void drawTriangle(); float wall1[] = {-50,-150,-50,50}; Enviroment::Wall(test); Boat_Pos Player(0,0,0); Boat_Pos Player2boat(0,0,0); Camera userCamera(1); Camera userCamera2(1); Enviroment track; Collision wallNormal; Collision normalTest; int currentTime=0,lastTime=0; float elapsedTime=0; static float translatePlayerSpeed = 0; static float translateRotation = 0; CXBOXController* Player1Controller; CXBOXController* Player2Controller; Controller Player1; Controller Player2; /************* START OF OPENGL FUNCTIONS ****************/ void display() { currentTime = GetTickCount(); elapsedTime = ((float)currentTime - (float)lastTime)/1000.0f; lastTime = currentTime; //Players 1 screen glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glViewport(0,0,screenWidth/2,screenHeight); glMatrixMode(GL_PROJECTION); // Select The Projection Matrix glLoadIdentity(); // Reset The Projection Matrix //set up a perspective view (fov, aspect ratio, near, far) gluPerspective(45.0f,(GLfloat)screenWidth/(GLfloat)screenHeight,0.1f,1000.0f); glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix glLoadIdentity(); // Reset The Modelview Matrix glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0f,(GLfloat)(screenWidth/2)/(GLfloat)screenHeight,0.1f,1000.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); userCamera.checkCam(Player1Controller); userCamera.readCam(Player,Player1Controller); glPushMatrix(); //all water int bottomWater = 0; int MiddleWater = 1; float Playerbbpoints[] = {Player.boatDirectionX-4,Player.boatDirectionZ-4,Player.boatDirectionX+4,Player.boatDirectionZ+4}; float playerPPTest[] = {Player.boatDirectionX,Player.boatDirectionZ,Player.boatDirectionX,Player.boatDirectionZ}; glColor3f(0.0,0.0,1.0); track.drawWater(-50,0,-150,50,0,50,0); track.drawWater(-600,0,-500,50,0,-150,1); track.drawWater(-600,0,-750,-500,0,-500,0); Enviroment::InitiliseEnviroment(wall1); int playerSpeedAccelerate = Player1.RightTrigger(Player1Controller); int playerSpeedDeccelerate = Player1.LeftTrigger(Player1Controller); float boatRotation = Player.BoatRotation(Player1Controller); Player.BoatSpeed(playerSpeedAccelerate,playerSpeedDeccelerate,elapsedTime); Player.BoatDirectionX(Player1Controller); Player.BoatDirectionZ(Player1Controller); glPushMatrix(); glTranslatef(Player.boatDirectionX,0,Player.boatDirectionZ); glRotatef(boatRotation,0,1,0); glRotatef((Player.speed*5)*-1,1,0,0); //std::cout<<"lean"<<(Player.speed*10)*-1<<std::endl; Player.DrawBoat(1,0,0); glPopMatrix(); I am then trying to draw the walls an intilise variables etc here void Enviroment::drawWall(float wallArray[],float boatPoint[]) { glPushMatrix(); glBegin(GL_POLYGON); glColor3f(0.7,0.7,0.7); glTexCoord2f(0,0); glVertex3f(wallArray[0], 0, wallArray[1]); glTexCoord2f(1,0); glVertex3f(wallArray[0], 3, wallArray[1]); glTexCoord2f(1,1); glVertex3f(wallArray[2], 3, wallArray[3]); glTexCoord2f(0,1); glVertex3f(wallArray[2], 0, wallArray[3]); glEnd(); glPopMatrix(); } static Enviroment::Wall InitiliseEnviroment(float testArray[]) { std::cout<<"array 1 is "<<testArray[0]<<std::endl; std::cout<<"array 2 is "<<testArray[1]<<std::endl; std::cout<<"array 3 is "<<testArray[2]<<std::endl; std::cout<<"array 4 is "<<testArray[3]<<std::endl; //there should be the normal calculation in here } last but not least the normal calculation and collision detection is done here. I haven't changed this to work with my structs yet and they are still set up from when I was attempting to pass arrays across. float * Collision::CalculateNormal(float normalArray[3][3],float normal[3]) { //calcualte cross product float ux = normalArray[1][0] - normalArray[0][0]; float uy = normalArray[1][1] - normalArray[0][1]; float uz = normalArray[1][2] - normalArray[0][2]; float vx = normalArray[2][0] - normalArray[0][0]; float vy = normalArray[2][1] - normalArray[0][1]; float vz = normalArray[2][2] - normalArray[0][2]; //float normal[3]; normal [0] = ((vy*uz) - (vz*uy))*-1; normal [1] = ((vz*ux) - (vx*uz)); normal [2] = ((vx*uy) - (vy*ux)); std::cout<<"The normal1 is "<<normal [0]<<std::endl; std::cout<<"The normal2 is "<<normal [1]<<std::endl; std::cout<<"The normal3 is "<<normal [2]<<std::endl; return normal; } bool Collision::BarryCentricEquation(float triA[],float triB[], float triC[],float point[]) { Collision triangle; float v0[] = {triC[0] - triA[0],triC[1] - triA[1],triC[2] - triA[2]}; float v1[] = {triB[0] - triA[0],triB[1] - triA[1], triB[2] - triA[2]}; float v2[] = {point[0] - triA[0],point[1] - triA[1],point[2] - triA[2]}; float dot00 = Calculation::dotProduct(v0,v0); float dot01 = Calculation::dotProduct(v0,v1); float dot02 = Calculation::dotProduct(v0,v2); float dot11 = Calculation::dotProduct(v1,v1); float dot12 = Calculation::dotProduct(v1,v2); float bcc = 1/(dot00 * dot11 - dot01 * dot01); float u = (dot11 * dot02 - dot01 * dot12) * bcc; float v = (dot00 * dot12 - dot01 * dot02) *bcc; if((u>0) && (v>0) && (u+v<1)) { triangle.bccCollision = true; } else { triangle.bccCollision = false; } return triangle.bccCollision; } float Collision::NormaliseNormal(float normalArray[3],float testPoint[4],float corner[3]) { float lengthX = sqrt((normalArray[0]*normalArray[0]) + (normalArray[1]*normalArray[1]) + (normalArray[2]*normalArray[2])); float normalX = normalArray[0] /= lengthX; float normalY = normalArray[1] /= lengthX; float normalZ = normalArray[2] /= lengthX; float testX[] = {testPoint[0] - corner[0]}; float testY[] = {testPoint[1] - corner[1]}; float testZ[] = {testPoint[2] - corner[2]}; float sX = (testX[0] - corner[0]); float sY = (testY[0] - corner[1]); float sZ = (testZ[0] - corner[2]); float distanceX = Collision::DotProduct(sX,normalX,0,0,0,0); float distanceY = Collision::DotProduct(0,0,sY,normalY,0,0); float distanceZ = Collision::DotProduct(0,0,0,0,sZ,normalZ); std::cout<<"DistanceX "<<distanceX<<std::endl; std::cout<<"DistanceY "<<distanceY<<std::endl; std::cout<<"DistanceZ "<<distanceZ<<std::endl; float cx = testPoint[0] - distanceX * normalX; float cy = testPoint[1] - distanceY * normalY; float cz = testPoint[2] - distanceZ * normalZ; std::cout<<"cx is "<<cx<<std::endl; std::cout<<"cy is "<<cy<<std::endl; std::cout<<"cz is "<<cz<<std::endl; //float collisionPoints[] = {cx,cy,cz}; return cx; } Sorry as it is a lot of code. I am no longer using the collision stuff anywhere to try and make it easier to solve. I have tested the functions individually and the results work and are fine. When I try to link it all together I cant quite get my head around how it works. I have not included the header files but that is just full of prototypes, except for my enviroment header File. I have also included the struct Wall in there so I can use it elsewhere. Would it be easier to get rid of the classes I have now that don't work and start from scratch trying to design them to include the struct format? If so how do I get the values from the struct from one class to another? Many thanks again you have been very helpful and patient and I am sorry to keep bugging you this. P.S I have now changed the code slightly with a for loop in the main to add all the elements of wall 1 into the wall struct for(int i = 0; i<4; i++) { test.wall = wall1; std::cout<<"will this work "<<test.wall<<std::endl; } It prints all the values out right however it doesn't feel right inputting values like this is that just me being paranoid? would I be able to do in theory test2.wall = wall2, and just do that for every wall?
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!