Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.
Posted 09 February 2010 - 03:44 AM
Posted 09 February 2010 - 10:13 AM
Quote:The question of whether the vector needs to be normalized should be easy enough to answer for yourself. What is the length of new_up after it's constructed? What does the 'normalize' operation do? Mathematically, would the 'normalize' operation change the vector in any way in this case? From that, you should be able to determine whether the normalization is needed.
Vec3D new_up = new Vec3D(0.0, 1.0, 0.0);
new_up.normalize(); // do I need to normalize this vector?
Quote:For simply aligning an object with a direction vector (the velocity vector in this case), the position of the object doesn't matter.
Having a velocity vector and location vector how can I align my 3D object to face in the same direction as the velocity?
vector3 side = cross(up, forward);The above pseudocode performs the conversion by way of a matrix simply because 'quaternion from matrix' functions are fairly common, while 'quaternion from basis vectors' functions aren't so common (even though they're basically equivalent).
side.normalize();
up = cross(forward, side);
matrix m = matrix_from_basis_vectors(side, up, forward);
quaternion q = quaternion_from_matrix(m);
Posted 09 February 2010 - 10:38 PM
Posted 10 February 2010 - 04:12 AM
Quote:The example I gave is susceptible to the problem I mentioned. The problem is here:
Original post by nardove
Hi jyk, so you said using my method "It'll work fine so long as the direction vector isn't parallel or nearly parallel with the up vector"
1. is there a way to avoid that? I can see that my object flip somethimes back and forward, is that the reason? if so can it be prevented?
2. will your pseudo code rotating using a quaternion fix the problem in 1?
// If 'up' and 'forward' are nearly parallel, the magnitude (length) of side will be small.From a conceptual standpoint, it makes sense that the algorithm doesn't work when the direction and up vectors are aligned. The purpose of the up vector is to provide additional information about how to orient the object, and if it's parallel to the direction vector, it provides no such information.
vector3 side = cross(up, forward);
// Normalizing a vector with small magnitude can have unpredictable results. Mathematically, you
// should be able to normalize any vector with non-zero length, but due to the limits of floating-point
// precision, normalization may fail even for vectors with very small magnitude (that is, you'll get
// undefined results, e.g. NaN's).
side.normalize();
// If normalizing the 'side' vector fails, everything from here on out will be invalid.
up = cross(forward, side);
matrix m = matrix_from_basis_vectors(side, up, forward);
quaternion q = quaternion_from_matrix(m);
vector3 axis = cross(current_forward, desired_forward);That's off the top of my head, but I think I got it right.
float length = axis.length();
if (length > epsilon) {
axis /= length;
float angle = atan2(length, dot(current_forward, desired_forward));
quaternion q = quaternion_from_axis_angle(axis, angle);
orientation = q * orientation; // 'orientation' is a quaternion
orientation.normalize();
}
Posted 10 February 2010 - 05:04 AM
Posted 10 February 2010 - 07:20 AM
Quote:Unfortunately I don't think I can provide a working version of your method, because I don't quite understand what it's doing (or trying to do). Among other things, there's some context missing; it's not really clear from your example (to me at least) how the transforms are being handled, and what the 'push' and 'pop' calls are supposed to do.
from there you lost me, will it be possible provide a pseudocode using my logic to rotate instead of quaternions?
Posted 10 February 2010 - 07:40 PM
Posted 11 February 2010 - 06:26 AM
Quote:Would probably help.
Perhaps you could describe in a bit more detail what type of behavior you're after. Are you trying to get the object to rotate incrementally each update so as to align with the velocity vector? Or are you trying to build a transform 'from scratch' that will align the object with the velocity vector?
Posted 11 February 2010 - 11:15 AM
Posted 11 February 2010 - 11:40 AM
// Somewhere during initialization:I think that's about the best advice I can offer at this point. If you have questions about the above pseudocode though, feel free to ask.
quaternion q = whatever_you_want_the_initial_orientation_to_be();
// For each update:
matrix m = matrix_from_quaternion(q);
vector3 forward = m.get_third_basis_vector(); // Ask if you're not sure what this means
vector3 desired_forward = normalize(velocity);
vector3 axis = cross(forward, desired_forward);
float length = axis.length();
if (length > epsilon) {
axis /= length;
float angle = atan2(length, dot(forward, desired_forward));
quaternion rotation = quaternion_from_axis_angle(axis, angle);
q = rotation * q;
q.normalize();
}
Posted 11 February 2010 - 11:35 PM
Posted 12 February 2010 - 04:22 AM
Vec3D forward = new Vec3D((float)mat.matrix[0][2],Is probably right, but just be sure that your matrices are actually column-basis matrices (as opposed to having the basis vectors in the rows). Next, take a look at this line:
(float)mat.matrix[1][2],
(float)mat.matrix[2][2]);
Vec3D desired_forward = forward.normalize();And see if you can spot any difference between it and the corresponding line in the pseudocode I posted earlier.
rotate(rotationQuat.w, rotationQuat.x, rotationQuat.y, rotationQuat.z);Looks a little suspicious. I don't know what your rotate() function does, but keep in mind that although a quaternion encodes an axis-angle rotation, under normal circumstances its elements cannot directly be interpreted as an axis-angle pair. (Typically, what you would do here is convert the quaternion to a matrix, and then upload the matrix to the graphics API.)
Posted 12 February 2010 - 04:44 AM
Vec3D desired_forward = velocity.normalize();
Posted 12 February 2010 - 05:10 AM
Posted 12 February 2010 - 06:11 AM
Posted 12 February 2010 - 06:44 AM
Quote:With those axis-angle values, you shouldn't be getting an identity quaternion. What math library are you using? Can you post a link to the documentation?
length: 0.8714546
angle: 1.05816
axis: {x:1.6277998E-8, y:0.9906332, z:0.13654976}
rotationQuat: {axis: [0.0,0.0,0.0], w: 1.0}
length: 0.86895746
angle: 1.0530915
axis: {x:1.6521371E-8, y:0.99034965, z:0.13859132}
rotationQuat: {axis: [0.0,0.0,0.0], w: 1.0}
Posted 12 February 2010 - 07:40 AM
Quote:For URLs, use the HTML 'a' element along with the 'href' attribute to create clickable links.
Original post by nardove
Documentation can be found here http://toxiclibs.org/docs/core/
Whate are the [ tags ] for a url?
rS
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.
GameDev.net™, the GameDev.net logo, and GDNet™ are trademarks of GameDev.net, LLC.