Advertisement

Moving objects in 3D

Started by June 08, 2015 01:02 PM
5 comments, last by tonemgub 9 years, 6 months ago

I'm using SharpDX (virtually identical to SlimDX). I'm using the coordinate system where the positive sides of the axes point right, up and forward.

Anyway, I'm just wondering if there's a function such that I can input three vectors (center point, forward direction and up direction - right would be implied), which will all represent the destination of an object, but it will just output a matrix, so that I can multiply the matrix by any vector (generally all points of an object in a loop), and it will then move and rotate each vector that amount. For example, if I start with a vector at the origin, it will end up moving exactly to the center point (about which rotation occurs), but if I started with a point slightly to the right of the origin, it would end up that same distance to the "right" of the center point (by "right" I mean in the direction of the implied right vector), or if it's a distance forward (along z) from the origin, it will end up that far "forward" of the center point.

So I was trying functions that seemed similar, such as LookAtLH, but that's really for positioning a camera, so if anything, it seems to have the opposite effect from what I want (if I move the camera forward, I'm actually moving points backward, so they're not as far ahead when they're drawn).

So is there a different function I should use instead?

I've looked into it a bit more, and I might be onto something, but I'm not sure, and I'd like some advice please. I found a function called Transformation, which takes as inputs:

- a translation vector

- a vector for the rotation point, and a quaternion to rotate

- a vector for the point to scale around, a vector for the amount of scaling, and a quaternion for rotating the axes along which to scale (so it doesn't necessarily scale on the default axes)

This seems to do sort of what I want, except there are a couple problems with it:

- It isn't really in the ideal format, so I'd have to translate my 3 vectors into other things (I'm not worried about scaling for the time being and if/when I am, I'll figure that out). I could use what I referred to as my "center point" as the translation vector, and make another one for the center of rotation, or else rotate on that same point (I think I just didn't take that into account when I listed them before, but it's easy to add), but the quaternion might cause problems. For one thing, how do I translate my forward and up vectors into a quaternion for rotation?

- Also, does the quaternion have enough data to rotate to absolutely any orientation, or does it just rotate an angular amount around a specified axes? I'm a little rusty in quaternion math. I need to be able to rotate so that the forward, up and right vectors can go in any direction so long as they're perpendicular.

In addition to that, it would be REALLY great if I could specify two sets of data: a starting location and orientation vectors, and an ending location and orientation vectors, and when I input all that into a function, it will make a matrix to move any point(s) I want in that specific way. If I could also get scaling into it, even better.

Could someone please advise me?

Advertisement


I've looked into it a bit more, and I might be onto something, but I'm not sure, and I'd like some advice please. I found a function called Transformation, which takes as inputs:

- a translation vector
- a vector for the rotation point, and a quaternion to rotate
- a vector for the point to scale around, a vector for the amount of scaling, and a quaternion for rotating the axes along which to scale (so it doesn't necessarily scale on the default axes)

This sounds like the transform function somewhat known from X3D. There are also the functions named AffineTransform with less parameters. Both can be used. It is probably so that for your use case

* the scalingCenter is (0,0,0)

* the scalingRotation is (1,0,0,0)

* the scaling is (1,1,1)

* the rotationCenter is (0,0,0)

* the rotation is (your-rotation)

* the translation is (your-translation)

However: It would be easier to just set the elements of the transform matrix from the parameters you already have. Assuming that SlimDX uses row vectors, the 1st row stores the sideways vector, the 2nd row stores the upward vector, the 3rd row stores the forward vector, and the 4th row stores the position vector. (Notice please that the rows stores homogeneous coordinate vectors, so that each of the direction vectors has a 0 as its last element, while the position vector has a 1.)

So, if you have pairwise orthogonal vectors right, up, and forward then you can use the setters Matrix.Mxx for that purpose. If they are not pairwise orthogonal then you need to apply the cross product before using the vectors.


However: It would be easier to just set the elements of the transform matrix from the parameters you already have. Assuming that SlimDX uses row vectors, the 1st row stores the sideways vector, the 2nd row stores the upward vector, the 3rd row stores the forward vector, and the 4th row stores the position vector. (Notice please that the rows stores homogeneous coordinate vectors, so that each of the direction vectors has a 0 as its last element, while the position vector has a 1.)

Really? How can that make the proper matrix to do all the translation and rotation just from cramming the vectors into the rows of a matrix? Could you supply a math proof or a link to one please?

If you multiply the vector (1,0,0,0) by your matrix, you'll get the first row. So the first row must be what haegarr described. Same thing for (0,1,0,0), (0,0,1,0) and (0,0,0,1).

If you multiply the vector (1,0,0,0) by your matrix, you'll get the first row. So the first row must be what haegarr described. Same thing for (0,1,0,0), (0,0,1,0) and (0,0,0,1).

Jesus! I just thought about that and it blew my mind! Why hadn't I ever noticed that before? For that matter, why hasn't it ever been pointed out before?! I've read vector/matrix geometry/trig books, took college courses on the subject, and read all about matrix transformations online and in programming books that always way over-complicated it with crazy garbage, but nothing ever put it in terms so simple!

Can this or something similar also be applied to the problem of taking two different locations and sets of orientation vectors, and transforming from one to the other?

P.S.: Honestly though, I really do need to rotate around an arbitrary point, and it would also be nice to scale around an arbitrary point along arbitrary axes as well. Do you have calculations for these or could I use that Transformation function I mentioned earlier? Of course that would require getting my data into a format that it would understand it, and I don't know for certain that it can rotate to the exact orientation I want.

Advertisement

All I ever needed to know about matrix transformations is this: https://msdn.microsoft.com/en-us/library/windows/desktop/bb206269%28v=vs.85%29.aspx

This topic is closed to new replies.

Advertisement