guix Posted January 15, 2013 Share Posted January 15, 2013 Hello! I would like to know how can I detect if vehicle move forward, or backward? I think using getElementMatrix but I have not much idea what to do with those values! I tried with getElementVelocity with Y velocity between 1 and -1, but, those velocities are related to the world, not the vehicle direction Sorry if this has been asked alrady but I searched for "vehicle forward" but no luck. Little help please? Also can you give me a description of the values in the 4*4 matrix ? Edit I did this, it seem to work: if (( fVelocityY < 0 and fRotationZ >= 90 and fRotationZ < 270 ) or ( fVelocityY > 0 and (fRotationZ < 90 or fRotationZ >= 270 )))then -- vehicle go forward end I think there is an easier way. Link to comment
DiSaMe Posted January 15, 2013 Share Posted January 15, 2013 Matrix which getElementMatrix returns contains 4 vectors. They are 4D vectors because they use homogenous coordinates. For simplicity you can ignore the 4th coordinate of vectors in this case. If we store the matrix into variable m, then m[4] is the position of element. m[1] is the direction from the element to the right (X axis of the element). m[2] points to the front (Y) and m[3] points up (Z). Because only element rotation and not position determines how absolute and relative coordinates of velocity are converted between each other, we only need m[1], m[2] and m[3]. If we have velocity coordinates rx, ry, rz which are relative to the element, then to find absolute coordinates ax, ay, az we need to do these calculations: ax = rx*m[1][1] + ry*m[2][1] + rz*m[3][1] ay = rx*m[1][2] + ry*m[2][2] + rz*m[3][2] az = rx*m[1][3] + ry*m[2][3] + rz*m[3][3] This shouldn't be hard to understand. The more X vector of element extends to the global X direction (m[1][1] is bigger), the more rx influences ax. Similarly, the more X vector points to the sky (m[1][3] is bigger), the higher is the point which is in the right direction from the vehicle (rx causes az to increase). In your case, we need to do the opposite: use ax, ay, az which we got from getElementVelocity to get rx, ry, rz. That would require to get the inverse of the matrix. But if length of the vectors is 1 and they're perpendicular to each other (something what rotation matrix usually looks like), then inverse will be the same as transpose of a matrix (the matrix in which columns become rows and vice-versa). So the code will look something like this: local ax, ay, az = getElementVelocity(element) local rx = ax*m[1][1] + ay*m[1][2] + az*m[1][3] local ry = ax*m[2][1] + ay*m[2][2] + az*m[2][3] local rz = ax*m[3][1] + ay*m[3][2] + az*m[3][3] rx is the element velocity to the right direction, ry is to the front and rz is up. I hope that helps Link to comment
guix Posted January 15, 2013 Author Share Posted January 15, 2013 Thank you very much for detailed post! This is working fVelocityRY = (fVelocityX * fMatrix[2][1]) + (fVelocityY * fMatrix[2][2]) + (fVelocityZ * fMatrix[2][3]) if fVelocityRY > 0 then -- vehicle move forward end I understand matrix a lot better now Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now