# eskimo456

Member

32

100 Neutral

• Rank
Member
1. ## ray to box intersection from viewpoint

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. ## ray to box collision detection problems

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. ## point to plane equation possible rearranging required?

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. ## 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. ## 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.

7. ## using structs as input arguments

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. ## 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. ## 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. ## 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. ## 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. ## returning structs across classes

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. ## Program structure questions

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. ## 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