Skip to main content

Trouble getting absolute vector from a Transform3D

3 replies [Last post]
blackhatbrigade
Offline
Joined: 2013-04-21
Points: 0

I am having difficulty getting a true translation vector from a transform.

	BranchGroup bg1 = new BranchGroup();
BranchGroup bg11 = new BranchGroup();
TransformGroup tg1 = new TransformGroup();
TransformGroup tg11 = new TransformGroup();
Transform3D trans1 = new Transform3D();
Transform3D trans11 = new Transform3D();

Vector3f v1 = new Vector3f(0.0f, 1.0f, 0.0f);
trans1.setTranslation(v1);
tg1.setTransform(trans1);
bg1.addChild(tg1);

v1.set(0.0f, -1.0f, 0.0f);
trans11.setTranslation(v1);
tg11.setTransform(trans11);
bg11.addChild(tg11);

So far so good. I omit any geometry because I don't believe it really matters. Here is where I get an issue...

	BranchGroup bg2 = new BranchGroup();
TransformGroup tg2 = new TransformGroup();
Transform3D trans2 = new Transform3D();

tg2.addChild(bg1);  // attaching previous BranchGroup to this TransformGroup
tg2.addChild(bg11);

trans2.rotX(Math.PI);
tg2.setTransform(trans2);
bg2.addChild(tg2);

// Now let's try to get that translation out

Transform3D t3d = new Transform3D();
t3d.mul(trans1, trans2);
Vector3f v = new Vector3f();

t3d.get(v);
System.out.println(v.toString());

That will output: (0.0, 1.0, 0.0)

Strictly speaking, that isn't what I'm looking for. The object that I attach to tg1 will have swapped places with the object attached to tg11. Of course, it will rotated 180degrees from it's starting axis.

What I want is to be able to get the new vector on it's location (in this case, I was looking for an output of: (0.0, -1.0, 0.0)

Is that possible? Thanks in advance for any help.

(I apologize if any of the code is off, this is not the actual code I'm working on. The actual code is a little too obfuscated to easily show what I'm looking for. This is merely an example of what I'm trying for.)

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
interactivemesh
Offline
Joined: 2006-06-07
Points: 0

Transform3D represents a transformation as a 4x4 matrix.

t3d.mul(trans1, trans2) is aquivalent to t3d = trans1 * trans2.

The rotation trans2 doesn't effect the translation trans1 according to the rules of 4x4 matrix multiplication. t3d is a concatenation of both and first rotates and secondly translates a point.

If trans1 and trans2 are reversed then rotation trans2 effects the translation trans1 ( => (0.0, -1.0, 0.0) ) and again t3d first applies its rotation and secondly its translation to a point.

In this case these two approaches lead to different results. Matrix multiplication is not in general commutative.

Apply the transformation t3d to the origin to determine how t3d 'moves' a point:

Point3d origin = new Point3d(0, 0, 0);
t3d.transform(origin);

t3d = trans1 * trans2 => origion = (0.0, 1.0, 0.0)
t3d = trans2 * trans1 => origion = (0.0, -1.0, 0.0)

August

blackhatbrigade
Offline
Joined: 2013-04-21
Points: 0

It occurred to me last night that trans2 was just a rotation and that my problem was that the object I was rotating was a combined mass of the two objects. Does Java3D figure out a "center of mass" as a origin? (ie. if I had chosen locations 0, -1, 0 and 0, 1, 2 for my objects, would 0, 0, 1 have been the origin of rotation for trans2? )

Thanks a TON for the solution. This should work for what I need (the added question was purely for future reference).

interactivemesh
Offline
Joined: 2006-06-07
Points: 0

Rotation always takes place around the origin (0, 0, 0). To rotate around an arbitrary point (px, py, pz) apply first a center translation (-px, -py, -pz), then the rotation, and finally the reverse center translation (px, py, pz).

August