Jump to content
  • Advertisement


  • Content Count

  • Joined

  • Last visited

Community Reputation

122 Neutral

About neiluk

  • Rank
  1. Hi, maybe a stupid question, but when opengl doesnt draw polygons outside the view area, does it still take as much work to process these non drawn triangles. Just wondering if its worth me checking in the code myself if sections of my level are outside the draw area and not drawing them before hand. All the polygon data is already held in arrays and isnt changed so i wouldnt be saving anytime on calculations, just calls to glVertex, glColor and glNormal inside a big for loop
  2. neiluk

    3D Rotation Problem

    Hi thanks, but that wasnt the problem, think id left that in from when I was trying out differnt combinations lol have changed it back now. Have uploaded the source code and executable so maybe if anyone fancies a look they can see the problem in describing. Its in a zip file here: http://geocities.com/neilsdemos/MoutainFighter.zip To see the problem, first use the up and down arrows to tilt the plane up/down, this works fine, next uses left/right arrows, it again rotates correctly. Now use up and down again and the camera drifts sideways as well as up :S (also x to accelerate, z to brake but arnt needed to see the problem) Thanks to anyon who takes a look. PS. the rotation code is in the file Object.h
  3. neiluk

    3D Rotation Problem

    any ideas anyone...?
  4. Im getting a werid problem with my code to rotate objects in 3D. 1. rotations about the Y axis always rotate the object about its local Y axis even if they follow several rotations about the z axis. 2. rotations about the Z axis rotate the object about its local z-axis if there have been no prior y axis rotations but they seem to veer off course if there have been prior y-axis rotations. Im doing it like this void RotateY(float angle) { rotation.y += angle; // Convert angle to radians angle = angle/RAD2DEG; float x = faceing.x; faceing.x = faceing.z*sin(angle) + x*cos(angle); faceing.z = faceing.z*cos(angle) - x*sin(angle); x = up.x; up.x = up.z*sin(angle) + x*cos(angle); up.z = up.z*cos(angle) - x*sin(angle); } void RotateZ(float angle) { // Increment object z rotation rotation.z += angle; // Convert angle to radians angle = angle/RAD2DEG; // Update object orientation vectors float x = faceing.x; faceing.x = (faceing.y*sin(angle) + x*cos(angle)); faceing.y = faceing.y*cos(angle) - x*sin(angle); x = up.x; up.x = (up.y*sin(angle) + x*cos(angle)); up.y = up.y*cos(angle) - x*sin(angle); } up and faceing are unit vectors pointing along the objects local axis. The object model is rotated using glRotatef() calls which work fine, the problem is that the camera is aligned behind the object using the faceing vector, which is incorrect. Im guessin i need to change some of the signs of the cos and sin components in the RotateZ function but i cant figure out which (have tried with trial and error)
  5. still cant get this working, tried reversing the order of the matrix multiplications but that didnt help. for the rednering im doing: glPushMatrix(); glTranslatef(position.x, position.y, position.z); //Draw Triangles in list glPopMatrix(); any ideas welcome...
  6. Ok well ive tried implementing rotations usig my own matrix class but im having some problems. All is fine when the object is positioned at the world origion but when its translated it all goes a bit pear shaped. I think it might be that the 3 vectors up, faceing and perp arnt being rotated correctly after the rotation but im not sure, anyone got any suggestions? Up, Faceing and perp are unit vectors along the object local axis, think the other variables are self explainatory but if not ill post the rest of my code. // Rotate object on its local x axis by angle (degrees) void Rotate(float angle, int axis) { // Unit vector in direction of axis of rotation Vector u; // Set rotation axis switch (axis) { case X: { u = perp; break; } case Y: { u = up; break; } case Z: { u = faceing; break; } default: { // Default to X axis rotation u = perp; } } // Translation matrix to translate to origin float Tarray[] = {1.0, 0.0, 0.0, -position.x, 0.0, 1.0, 0.0, -position.y, 0.0, 0.0, 1.0, -position.z, 0.0, 0.0, 0.0, 1.0}; Matrix T = Matrix(Tarray); // Compute rotation matrix to rotate to xz plane float d = sqrt((u.y*u.y)+(u.z*u.z)); // Mag of |u'| = |(0, u.y, u.z)| float c_d = u.z / d; // c divided by d float b_d = u.y / d; // b divided by d // Rotation matrix to rotate to xz plane float RXarray[] = {1.0, 0.0, 0.0, 0.0, 0.0, c_d, -b_d, 0.0, 0.0, b_d, c_d, 0.0, 0.0, 0.0, 0.0, 1.0}; Matrix Rx = Matrix(RXarray); // Compute rotation matrix to rotate to z axis float RYarray[] = {d, 0.0, -u.x, 0.0, 0.0, 1.0, 0.0, 0.0, u.x, 0.0, -d, 0.0, 0.0, 0.0, 0.0, 1.0}; Matrix Ry = Matrix(RYarray); // Rotation matrix to rotate about z axis float radian = angle/RAD2DEG; float cos_x = cos(radian); float sin_x = sin(radian); float RZarray[] = {cos_x, -sin_x, 0.0, 0.0, sin_x, cos_x, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0}; Matrix Rz = Matrix(RZarray); // Inverse Matricies float T_Iarray[] ={1.0, 0.0, 0.0, position.x, 0.0, 1.0, 0.0, position.y, 0.0, 0.0, 1.0, position.z, 0.0, 0.0, 0.0, 1.0}; Matrix T_I = Matrix(T_Iarray); Matrix Rx_I = Rx.Transpose(); Matrix Ry_I = Ry.Transpose(); // Calculate combined transformation matrix Matrix rotation = ((((((T_I*Rx_I)*Ry_I)*Rz)*Ry)*Rx)*T); // Rotate Triangles in list list<Triangle>::iterator iter; Vector temp; for(iter=triangles.begin(); iter!=triangles.end(); ++iter) { // Update V1 temp = iter->getV1(); temp = rotation*temp; iter->setV1(temp); // Update V2 temp = iter->getV2(); temp = rotation*temp; iter->setV2(temp); // Update V3 temp = iter->getV3(); temp = rotation*temp; iter->setV3(temp); // Update Normal iter->CalculateNormal(); } // Get points at end of unit vectors in the directions of the objects axis Vector f1 = position + faceing; Vector u1 = position + up; Vector p1 = position + perp; // Rotate these points by new transformation f1 = rotation*f1; u1 = rotation*u1; p1 = rotation*p1; // Calculate new axis vectors faceing = f1 - position; up = u1 - position; perp = p1 - position; // Normalize Unit axis vectors faceing.Normalize(); up.Normalize(); perp.Normalize(); }
  7. I was just wondering, what are peoples opinions on the best way to handle rotations of objects about their own axis in 3D when using openGL, is it better to use combinations of glTranslate and glRotate or to create your own transformation matricies and a class to handle there manipulations. Also ive been reading about quaternions from the green book and was wondering if there actually that much faster than using the standard transformation matrix approach. It says some of the speed increase is due to efficient implementations of vector calculations in hardware, so will this increase not be apparent if im using my own vector class to perform the operations?
  8. Nope still just gettig a flat square damn it. Point *PerlinNoise(float x, float y, float roughness, int seed) { int n = int(x*100) + int(y*100)*57 + seed * 131; n = (n<<13)^n; n = (1.0f - ((n * (n * n * 15731 + 789221) + 1376312589) &0x7fffffff)* 0.000000000931322574615478515625f); Point *displacement = new Point(0.0, n*roughness, 0.0); return displacement; } using int(time(NULL)) as the seed, i multiplied x and y by 100 before making them ints as their floats in a fairly close range so thought i best spread them out a bit so i dont get all the same value. It just seems to return n=0.0 for all values of x and y, any ideas?
  9. Thanks Basiror, I tried using that function but i just seem to get a flat terrain. Do the x y coordinates have to be integer values as im using floating points? What kind of form does the seed take on, is it just a roughness factor which is the same over all the terrain? What kind of values have you been using for the seed to get good results? Thanks Neil
  10. Its cause ive structured the grid into "Squares" rather than just a 2D array of points. I didnt really do much research before i started lol was just going on the half a page of details in the openGL green book. I cant be bothered rewritting it all tho, im pretty sure there must be a way to figure out if a mid point is shared that wont slow it down too much but i cant think straight, been trying to figure it out for too long.
  11. Well ive been trying to make a fractal terrain generator using the midpoint displacement algorithm and ive come up with a bit of a problem in my implementation. Basiclly I have a list of Squares made up of 4 Points. On every iteration I do this: void Terrain ::Generate(int iterations) { for(int i=0; i&lt;iterations; i++) { // Get current number of squares making up the terrain int size = squares.size(); // Iterate over the current squares for(int j=0; j&lt;size; j++) { // Get next square from the list and save it to temp Square *temp = squares.front(); // Remove the square from the list squares.pop_front(); // Subdivide into 4 smaller squares Square *subSquares[4]; temp-&gt;SubDivide(subSquares, roughness, xMax, xMin, yMax, yMin); // Add the new squares to the end of the list for(int k=0; k&lt;4; k++) { squares.push_back(subSquares[k]); } } } } where SubDivide() looks like this: // Divides this square into 4 other squares using random midpoint displacement void SubDivide(Square *subSquares[], float roughness, float xMax, float xMin, float yMax,float yMin) { // Find the midPoints of the squares edges Point *ab = a-&gt;MidPoint(b); Point *bc = b-&gt;MidPoint(c); Point *cd = c-&gt;MidPoint(d); Point *da = d-&gt;MidPoint(a); // Find the Centre of the square Point *m = ab-&gt;MidPoint(cd); // Add random displacements to the mid points if not on edges if(ab-&gt;x != xMax && ab-&gt;x != xMin && ab-&gt;y != yMax && ab-&gt;y != yMin) ab-&gt;add(RandomDisplacement(roughness)); if(bc-&gt;x != xMax && bc-&gt;x != xMin && bc-&gt;y != yMax && bc-&gt;y != yMin) bc-&gt;add(RandomDisplacement(roughness)); if(cd-&gt;x != xMax && cd-&gt;x != xMin && cd-&gt;y != yMax && cd-&gt;y != yMin) cd-&gt;add(RandomDisplacement(roughness)); if(da-&gt;x != xMax && da-&gt;x != xMin && da-&gt;y != yMax && da-&gt;y != yMin) da-&gt;add(RandomDisplacement(roughness)); m-&gt;add(RandomDisplacement(roughness)); // Create and store the sub squares subSquares[0] = new Square(a, ab, m, da); subSquares[1] = new Square(ab, b, bc, m); subSquares[2] = new Square(da, m, cd, d); subSquares[3] = new Square(m, bc, c, cd); } Basiclly the problem is when 2 squares share a midpoint, the midpoint is calculated seperatly twice, giving two differnt heights and therefore making gaps in my terrain when i render. Any ideas on how i can fix this problem??
  12. Im wanting to tell if two float values have the same sign or not. I was just wondering if there was a quick way of doing this, as at the minute im subtracting them both ways and having to use a load of if statements, seems like a lot of work for such a little thing, i figure there must be a well known optimum way to do this, Thanks
  13. neiluk

    Heading vector from a rotation

    Quote:Original post by jyk Quote:Is that a suggestion to check if its wrong or are you saying its deffinatly wrong?? Im pretty sure thats the way i want it for a vector from the enemy too the player, did try changing it but didnt help anyI'm pretty sure it's wrong; you want the vector from the player to the enemy, not the other way around. If changing it didn't fix the problem, it's probably due to other errors. yeah i did want it the other way round, they were turning correctly using TheCOOL1s solution but had there backs faceing the player not there fronts, thanks for you help also
  14. neiluk

    Heading vector from a rotation

    Quote:Original post by TheC00L1 Ummm.... I don't think that's how you're suppose to rotate towards an object, correct me if i'm wrong: //both must be normalized, if necessary t=target-position; t.normalize(); d=direction_veoctor; //dot product of the perpendicular if (d.y*t.x-d.x*t.y<0) angle-=1; else angle+=1; No, you appear to be right, thanks :) rating++ for you kind sir
  15. neiluk

    Heading vector from a rotation

    Quote:Original post by jyk A few observations: - You may want 'position - player->position' rather than 'player->position - position' Is that a suggestion to check if its wrong or are you saying its deffinatly wrong?? Im pretty sure thats the way i want it for a vector from the enemy too the player, did try changing it but didnt help any Quote: - Depending on the value of RAD2DEG, it may be that you're supposed to multiply by it rather than divide is def divide ive used it already elsewhere in my program Quote: - You don't need to normalize the facing vector after you create it but it wudnt cause any errors surely?? Quote: - I take it angle is an int; I can't remember what happens when you divide an int by a float in c++, but you might cast 'angle' just to be safe have changed this just incase, dont know if its made any difference Quote: - acos(dot(a,b)) gives you the unsigned angle, which isn't of much use here; you want angle = atan2(perp_dot(a,b),dot(a,b)) guessing this is where my problem is then, was looking in another thread and got the idea of what this means, but how can i calculate perp(a), wud be gratefull if you could explain :) [Edited by - neiluk on June 6, 2006 4:04:37 AM]
  • Advertisement

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!