• Advertisement

Jonathan2006

Member
  • Content count

    11
  • Joined

  • Last visited

Community Reputation

122 Neutral

About Jonathan2006

  • Rank
    Member

Personal Information

  1. Transforming Angular Velocity

    The red wire sphere code is the “mat4 rotateMatrix” that I posted further above. Thank you JoeJ for your time. I just don’t know enough to figure this out. Below are some things I have learned. After skimming through some game developer physics books, euclideanspace.com, and your posts here’s what I think I have learned so far... 1) Axis Angle can be used to represent the length and direction of angular velocity/rotations in quaternions and matrices. 2) Multiplying two quaternions is just like multiplying two matrices. 3) Angular velocity can be represented in a 3x3 skew matrix. What went wrong 1) I could never get any of my axis angle functions to work with more than one integrator. 2) Not sure about multiplying quaternions since I could never get the correct output after multiplying. 3) I tried multiplying several skew matrices together and the final matrix had all zero values. I used the GLM library for most of my quaternion and matrix code but I was never really able to debug the axis angle quaternions. I tried axis angle matrices from euclideanspace.com and I did find a bug in the GLM code that I fixed in my code. The output of angular velocity in the matrices never came out correctly after I added more than one integrator. Since nothing worked I will have to use my old unreliable code below: vec3 angleRadians = GEEulerAngleFromMat4(matrix); vec3 angularVelocity = (angleRadians - oldAngleRadians) / globalTimeStep; vec3 oldAngleRadians = angleRadians; Hope this helps anyone else with this problem. Thanks again, Jonathan
  2. Transforming Angular Velocity

    Thank you again for all the help! I am still trying to wrap my head around quaternions. I just thought of something. Even if I use something like quatCur - quatOld should the angular oscillating stop if I normalize the quaternion like you did in your code? I think I understand your last post on quaternions but I am getting weird acceleration. I also added the time step back into the last integrator (hope that's the name). The reason I use the last angleEulerRadians and angularVelocityVector (integrator?) is to only test the final angular velocity. I created a gif of the rotations below. Also Here's the code I am using: void integrateAngularVelocity(const quat curOrn, const vec3 angvel, const float timeStep, quat *predictedOrn) { vec3 axis; float fAngle = GEMagnitudeVector(angvel); if (fAngle < 0.00001) { *predictedOrn = curOrn; return; } else { axis = GEMultiplyVectorAndScalar(angvel, sinf(fAngle * timeStep * 0.5) / fAngle); } quat dorn = GESetQuaternion(axis.x, axis.y, axis.z, cosf(fAngle * timeStep * 0.5)); *predictedOrn = GEMultiplyQuaternions(dorn, curOrn); *predictedOrn = GENormalizeQuaternion(*predictedOrn); } void Shape(void) { vec3 angularVelocityVector1 = GERadiansFromDegreesVector(GESetVector(20.0, 0.0, 0.0)); vec3 angularVelocityVector2 = GERadiansFromDegreesVector(GESetVector(0.0, 0.0, 20.0)); vec3 angularVelocityVector3 = GERadiansFromDegreesVector(GESetVector(20.0, 0.0, 0.0)); static quat quaternion; integrateAngularVelocity(quaternion, angularVelocityVector1, globalTimeStep, &quaternion); integrateAngularVelocity(quaternion, angularVelocityVector2, globalTimeStep, &quaternion); integrateAngularVelocity(quaternion, angularVelocityVector3, globalTimeStep, &quaternion); vec3 angularVelocityVector = GEMultiplyVectorAndScalar(GEAxisFromQuaternion(quaternion), GEAngleFromQuaternion(quaternion)); angularVelocityVector = GEAddVectors(angularVelocityVector, GEMultiplyVectorAndScalar(angularAccelerationVector, globalTimeStep)); angleEulerRadians = GEAddVectors(angleEulerRadians, GEMultiplyVectorAndScalar(angularVelocityVector, globalTimeStep)); glColor3f(0.0, 1.0, 1.0); glPushMatrix(); glTranslatef( 2.0, 0.0, 0.0); // This spins really fast glMultMatrixf(GEMat4FromEulerAngle(angleEulerRadians).array); glutWireSphere(2.0, 7.0, 7.0); glPopMatrix(); } It's not the best quality but the red sphere is the correct rotation and the cyan is the quaternion code above.
  3. Transforming Angular Velocity

    Thanks for the posts and ideas. I already know how to get the final values for position, linear velocity, linear acceleration, and Euler angle. I have been researching quaternions some more. if it’s even possible I would like to know the instant angular velocity and angular acceleration which I tried to find in the code below. If it’s not possible to find the instant value I am confused as to what should be a vector and what should be a quaternion in your post above. If it matters I am trying to use instant quaternions. I had problems with oscillating after a perfect collision using just the original and resulting vectors (I have not tried it with quaternions though). The C code below is as close as I can get to the real motion engine: void rotateShapeX(mat4 *m, quat *q, vec3 *angleRadians, vec3 *angularVelocity, vec3 *angularAcceleration) { *angularVelocity = GEAddVectors(*angularVelocity, GEMultiplyVectorAndScalar(*angularAcceleration, globalTimeStep)); *angleRadians = GEAddVectors(*angleRadians, GEMultiplyVectorAndScalar(*angularVelocity, globalTimeStep)); *q = GEQuaternionFromAxisAndAngle(*angularVelocity, angleRadians->x); *m = GEMultiplyMat4(GEMat4FromRotate(angleRadians->x, 1.0, 0.0, 0.0), *m); } void rotateShapeZ(mat4 *m, quat *q, vec3 *angleRadians, vec3 *angularVelocity, vec3 *angularAcceleration) { *angularVelocity = GEAddVectors(*angularVelocity, GEMultiplyVectorAndScalar(*angularAcceleration, globalTimeStep)); *angleRadians = GEAddVectors(*angleRadians, GEMultiplyVectorAndScalar(*angularVelocity, globalTimeStep)); *q = GEQuaternionFromAxisAndAngle(*angularVelocity, angleRadians->z); *m = GEMultiplyMat4(GEMat4FromRotate(angleRadians->z, 0.0, 0.0, 1.0), *m); } void shape(void) { mat4 rotateMatrix, rotateMatrix1, rotateMatrix2, rotateMatrix3; rotateMatrix = rotateMatrix1 = rotateMatrix2 = rotateMatrix3 = GEIdentityMat4(); // These will be static once it works vec3 angularVelocityVector1 = GERadiansFromDegreesVector(GESetVector(20.0, 0.0, 0.0)); vec3 angularVelocityVector2 = GERadiansFromDegreesVector(GESetVector(0.0, 0.0, 20.0)); vec3 angularVelocityVector3 = GERadiansFromDegreesVector(GESetVector(20.0, 0.0, 0.0)); // This is here so I can test only the angular velocity vec3 angularAccelerationVector = GESetVector(0.0, 0.0, 0.0); static vec3 angleEulerRadians, angleEulerRadians1, angleEulerRadians2, angleEulerRadians3; static quat quaternion, quaternion1, quaternion2, quaternion3; static char initialze = 0; if (initialze == 0) { angleEulerRadians = angleEulerRadians1 = angleEulerRadians2 = angleEulerRadians3 = GESetVector(0.0, 0.0, 0.0); quaternion = GESetQuaternion(0.0, 0.0, 0.0, 1.0); initialze = 1; } // Rotate rotateShapeX(&rotateMatrix1, &quaternion1, &angleEulerRadians1, &angularVelocityVector1, &angularAccelerationVector); rotateShapeZ(&rotateMatrix2, &quaternion2, &angleEulerRadians2, &angularVelocityVector2, &angularAccelerationVector); rotateShapeX(&rotateMatrix3, &quaternion3, &angleEulerRadians3, &angularVelocityVector3, &angularAccelerationVector); // Multiply matrices (works) rotateMatrix = GEMultiplyMat4(rotateMatrix1, rotateMatrix); rotateMatrix = GEMultiplyMat4(rotateMatrix2, rotateMatrix); rotateMatrix = GEMultiplyMat4(rotateMatrix3, rotateMatrix); // Multiply Quaternions? (does not work) quaternion = GETransformQuaternions(quaternion, quaternion1); quaternion = GETransformQuaternions(quaternion, quaternion2); quaternion = GETransformQuaternions(quaternion, quaternion3); vec3 angularVelocityVector = GEAxisFromQuaternion(quaternion); // Not sure if this is correct but I took out multipling globalTimeStep since it is already used in rotateShape*(); angularVelocityVector = GEAddVectors(angularVelocityVector, angularAccelerationVector); angleEulerRadians = GEAddVectors(angleEulerRadians, angularVelocityVector); glColor3f(0.0, 1.0, 1.0); glPushMatrix(); glTranslatef( 2.0, 0.0, 0.0); // This spins really fast glMultMatrixf(GEMat4FromEulerAngle(angleEulerRadians).array); glutWireSphere(2.0, 7.0, 7.0); glPopMatrix(); glColor3f(1.0, 0.0, 0.0); glPushMatrix(); glTranslatef(-2.0, 0.0, 0.0); glMultMatrixf(rotateMatrix.array); // (Works) glutWireSphere(2.0, 7.0, 7.0); glPopMatrix(); }
  4. Transforming Angular Velocity

    That was one I didn't think of. Thanks for the averaging idea. I still cannot get it working. I am trying to mimic OpenGL's glRotate(); except I need to know the final angular velocity and acceleration vectors. The physics engine I am working on only accepts one angular velocity and angular acceleration vector. Below I am posting more of the code: // The Angle Radians Test (code used both by the matrix and quaternions) angularVelocity1 = GEAddVectors(angularVelocity1, GEMultiplyVectorAndScalar(angularAcceleration1, timeStep)); angleRadiansVector1 = GEAddVectors(angleRadiansVector1, GEMultiplyVectorAndScalar(angularVelocity1, timeStep)); angularVelocity2 = GEAddVectors(angularVelocity2, GEMultiplyVectorAndScalar(angularAcceleration2, timeStep)); angleRadiansVector2 = GEAddVectors(angleRadiansVector2, GEMultiplyVectorAndScalar(angularVelocity2, timeStep)); angularVelocity3 = GEAddVectors(angularVelocity3, GEMultiplyVectorAndScalar(angularAcceleration3, timeStep)); angleRadiansVector3 = GEAddVectors(angleRadiansVector3, GEMultiplyVectorAndScalar(angularVelocity3, timeStep)); // Matrix (This works and is used to test the accuracy of the quaternions) mat4 rotateMatrix = GEMultiplyMat4(GEMat4FromRotate(angleRadiansVector1.x, 1.0, 0.0, 0.0), GEIdentityMat4()); rotateMatrix = GEMultiplyMat4(GEMat4FromRotate(angleRadiansVector2.z, 0.0, 0.0, 1.0), rotateMatrix); rotateMatrix = GEMultiplyMat4(GEMat4FromRotate(angleRadiansVector3.x, 1.0, 0.0, 0.0), rotateMatrix); glMultMatrixf(rotateMatrix.array); glutWireSphere(2.0, 7.0, 7.0); The first few lines are extra code that I was hoping I could leave out in the first post. The rotateMatrix works just like OpenGL's glRotate(); and is the desired output. I am trying to transform the angular vectors the same way OpenGL does when multipling two matrices.
  5. Transforming Angular Velocity

    My question: is it possible to transform multiple angular velocities so that they can be reinserted as one? My research is below: // This works quat quaternion1 = GEQuaternionFromAngleRadians(angleRadiansVector1); quat quaternion2 = GEMultiplyQuaternions(quaternion1, GEQuaternionFromAngleRadians(angleRadiansVector2)); quat quaternion3 = GEMultiplyQuaternions(quaternion2, GEQuaternionFromAngleRadians(angleRadiansVector3)); glMultMatrixf(GEMat4FromQuaternion(quaternion3).array); // The first two work fine but not the third. Why? quat quaternion1 = GEQuaternionFromAngleRadians(angleRadiansVector1); vec3 vector1 = GETransformQuaternionAndVector(quaternion1, angularVelocity1); quat quaternion2 = GEQuaternionFromAngleRadians(angleRadiansVector2); vec3 vector2 = GETransformQuaternionAndVector(quaternion2, angularVelocity2); // This doesn't work //quat quaternion3 = GEQuaternionFromAngleRadians(angleRadiansVector3); //vec3 vector3 = GETransformQuaternionAndVector(quaternion3, angularVelocity3); vec3 angleVelocity = GEAddVectors(vector1, vector2); // Does not work: vec3 angleVelocity = GEAddVectors(vector1, GEAddVectors(vector2, vector3)); static vec3 angleRadiansVector; vec3 angularAcceleration = GESetVector(0.0, 0.0, 0.0); // Sending it through one angular velocity later in my motion engine angleVelocity = GEAddVectors(angleVelocity, GEMultiplyVectorAndScalar(angularAcceleration, timeStep)); angleRadiansVector = GEAddVectors(angleRadiansVector, GEMultiplyVectorAndScalar(angleVelocity, timeStep)); glMultMatrixf(GEMat4FromEulerAngle(angleRadiansVector).array); Also how do I combine multiple angularAcceleration variables? Is there an easier way to transform the angular values?
  6. OpenGL Color Anaglyphs

    I'm having trouble getting anaglyphs working for a game that uses OpenGL ES 1.1 . Right now I'm trying to use code that I found on Paul Bourke's website. I've tried changing the color of the Cube() with lighting but if the cube is anything other than white the red and blue masks disappear. My question is how do I get colored objects other than white to look 3D with my red and blue glasses? I'm using the Toe-in Method which is explained here: http://local.wasp.uwa.edu.au/~pbourke/miscellaneous/stereographics/stereorender/ Thanks, Jonathan [Edited by - Jonathan2006 on June 27, 2010 4:05:40 PM]
  7. Smooth moving on terrain

    Sorry about that. I fixed it now.
  8. Smooth moving on terrain

    Well I'm confused now. Can someone please explain some ways to improve the code I already have so I don't get weird results. I'm writing in a different code language but below it's written in kind of pseudocode/C which should be easier to read. I also just use one square in the code but if you want to see the weird results add an array of squares. Here's the code I'm using: #define kGravity 3.0 char keyboardCommand; BOOL keyUp, collision; float xSpeed, ySpeed, zSpeed; float xPoint, yPoint, zPoint; float xOldPoint, yOldPoint, zOldPoint; float lastDistance, normalForce; float vectorDistance(float x1Vert, float y1Vert, float z1Vert, float x2Vert, float y2Vert, float z2Vert, float x3Vert, float y3Vert, float z3Vert, float xP, float yP, float zP); void keyboardDown(void); void keyPressed(char command) { keyUp = NO; // Sometimes this doesn't fire when the key is held down for a long time. // So I update it in the timer keyboardCommand = command; } void keyboardUp(char command) { keyUp = YES; xSpeed = 0.0; ySpeed = 0.0; zSpeed = 0.0; switch(command) { case 'w': case 's': zSpeed = 0.0; break; } } // this is where everything is updated void timer(void) { float x1Vert =-1.0; float y1Vert = 1.0; float z1Vert = 0.0; float x2Vert = 1.0; float y2Vert = 3.0; float z2Vert = 0.0; float x3Vert = 1.0; float y3Vert = 3.0; float z3Vert =-1.0; float x4Vert =-1.0; float y4Vert = 1.0; float z4Vert =-1.0; float A = y1Vert * (z2Vert - z3Vert) + y2Vert * (z3Vert - z1Vert) + y3Vert * (z1Vert - z2Vert); float B = z1Vert * (x2Vert - x3Vert) + z2Vert * (x3Vert - x1Vert) + z3Vert * (x1Vert - x2Vert); float C = x1Vert * (y2Vert - y3Vert) + x2Vert * (y3Vert - y1Vert) + x3Vert * (y1Vert - y2Vert); float D = x1Vert * (y2Vert * z3Vert - y3Vert * z2Vert) + x2Vert * (y3Vert * z1Vert - y1Vert * z3Vert) + x3Vert * (y1Vert * z2Vert - y2Vert * z1Vert); float vectorMagnitude = sqrt(A * A + B * B + C * C); float s; // This has more code but I don't want to write it out // All it does is count the time between when the timer(); was last called float theTimeSegment; xPoint += (xSpeed * theTimeSegment); yPoint += ((ySpeed - kGravity + normalForce) * theTimeSegment); zPoint += (zSpeed * theTimeSegment); // this needs to be after the speed keyboardDown(); if (s > 0.1) { xOldPoint = xPoint; yOldPoint = yPoint; zOldPoint = zPoint; } s = A * xPoint + B * yPoint + C * zPoint - D; if (lastDistance >=-0.1 && s <= 0.1) { // Normalize and finding the unit length of the normal float xPlanePoint = xPoint - ((s / vectorMagnitude) * (A / vectorMagnitude)); float yPlanePoint = yPoint - ((s / vectorMagnitude) * (B / vectorMagnitude)); float zPlanePoint = zPoint - ((s / vectorMagnitude) * (C / vectorMagnitude)); // This is for a square but it can be done for any polygon float distance1 = vectorDistance(x1Vert, y1Vert, z1Vert, x2Vert, y2Vert, z2Vert, (x1Vert - A), (y1Vert - B), (z1Vert - C), xPlanePoint, yPlanePoint, zPlanePoint); float distance2 = vectorDistance(x2Vert, y2Vert, z2Vert, x3Vert, y3Vert, z3Vert, (x2Vert - A), (y2Vert - B), (z2Vert - C), xPlanePoint, yPlanePoint, zPlanePoint); float distance3 = vectorDistance(x3Vert, y3Vert, z3Vert, x4Vert, y4Vert, z4Vert, (x3Vert - A), (y3Vert - B), (z3Vert - C), xPlanePoint, yPlanePoint, zPlanePoint); float distance4 = vectorDistance(x4Vert, y4Vert, z4Vert, x1Vert, y1Vert, z1Vert, (x4Vert - A), (y4Vert - B), (z4Vert - C), xPlanePoint, yPlanePoint, zPlanePoint); // Since the planes are pointing inward we check for a positive distance if (distance1 >= 0.0 && distance2 >= 0.0 && distance3 >= 0.0 && distance4 >= 0.0) { // Normalize float xNorm = A / vectorMagnitude; float yNorm = B / vectorMagnitude; float zNorm = C / vectorMagnitude; float dotProduct = (xSpeed * xNorm + ySpeed * yNorm + zSpeed * zNorm); xSpeed -= xNorm * dotProduct; ySpeed -= yNorm * dotProduct; zSpeed -= zNorm * dotProduct; xPoint = xOldPoint; yPoint = yOldPoint; zPoint = zOldPoint; normalForce = kGravity; collision = YES; } } lastDistance = s; } float vectorDistance(float x1Vert, float y1Vert, float z1Vert, float x2Vert, float y2Vert, float z2Vert, float x3Vert, float y3Vert, float z3Vert, float xP, float yP, float zP) { float A = y1Vert * (z2Vert - z3Vert) + y2Vert * (z3Vert - z1Vert) + y3Vert * (z1Vert - z2Vert); float B = z1Vert * (x2Vert - x3Vert) + z2Vert * (x3Vert - x1Vert) + z3Vert * (x1Vert - x2Vert); float C = x1Vert * (y2Vert - y3Vert) + x2Vert * (y3Vert - y1Vert) + x3Vert * (y1Vert - y2Vert); float D = x1Vert * (y2Vert * z3Vert - y3Vert * z2Vert) + x2Vert * (y3Vert * z1Vert - y1Vert * z3Vert) + x3Vert * (y1Vert * z2Vert - y2Vert * z1Vert); return ((A * xP) + (B * yP) + (C * zP) - D); } void keyboardDown(void) { if (keyUp == NO) { switch(keyboardCommand) { case 'w': if (collision) { xSpeed = 0.0; ySpeed = 0.0; zSpeed = 0.0; if (normalForce == kGravity) normalForce = 0.0; zSpeed =-2.0; collision = NO; } break; case 's': if (collision) { xSpeed = 0.0; ySpeed = 0.0; zSpeed = 0.0; if (normalForce == kGravity) normalForce = 0.0; zSpeed = 2.0; collision = NO; } break; } } } [Edited by - Jonathan2006 on May 23, 2006 9:11:35 PM]
  9. Smooth moving on terrain

    Right now I'm checking my collision detection between a point and a polygon/terrain. The way I test to see if the point needs to be moved is if it flys through the polygon and then I send it back to the point it was before the collision. This collision detection is pretty rough and I sometimes get my point to warp at the edges of each polygon and fly off in weird directions. Is there any way to get the point to follow the polygons smoother than flying through stopping and then changing the direction of the speed vector? Is there some kind of terrain avoidance or something that I could use?
  10. Changing a vector so it slopes with terrain

    Thanks so much for the replies. It worked perfect.
  11. I have a vector u = -1.0, 0.0, 0.0 and it hits a plane with a normal vector of v = 2.0, 2.0, 0.0 . When the vector collides with the plane what formulas should I use to make the u vector change to follow the slope of the plane?
  • Advertisement