I have these:
a translation:
[x1, y1, z1]
a quaternion:
[x, y, z, w]
how can I use the translation and quaternion to make rotation and translate with a three.js mesh.
also, can I combine these into one 4X4 matrix, which is used for mesh.applyMatrix()?
thanks.
var obj = new THREE.Object3D()
obj.position.set(x1,y1,z1)
obj.quaternion.set(x,y,z,w)
obj.updateMatrix();
yourMesh.applyMatrix(obj.matrix)
Related
I know that there is set_origin_pose to shift a pose in X/Y/Z.
But I was not able to to rotate a pose along its own X Y or Z axis. I cant simply add an angle to the pose's values, because they refer to the camera's coordinates.
How can a pose be rotated?
Solved by converting the pose to a mat3d, rotating the mat with hom_mat3d_rotate_local and then converting back to a pose:
*shift base pose
set_origin_pose (CalculationPose, X1 ,0, Y1, CalculationPose)
disp_3d_coord_system(3600, CameraParam, CalculationPose, 0.1)
*rotate base pose
pose_to_hom_mat3d(CalculationPose, CalculationMat)
hom_mat3d_rotate_local(CalculationMat, -AngleRad , 'y',CalculationMatRotated)
hom_mat3d_to_pose(CalculationMatRotated, CalculationPose)
I have a PlaneBufferGeometry and its vertex origin is the center of the object, but I'd like to move it to the top left vertex. Is there a function or do I have to create it?
You can translate your geometry using the following pattern:
geometry.translate( x, y, z );
three.js r.84
Is there a way to rotate a generated linestring geometry around one of its points? I've built a length string that is pointing north (only adding length to the one co-oordinate) but I now need to rotate it to a given compass heading.
Geometry objects don't seem to have the ability to be rotated around a point (OL2 did?)
What can I do to rotate this geometry?
I eventually went with generating the geometry dynamically and solving pythagoras.
Given the length of the current linestring geometry segment and the angle in radians, I worked out how to offset the coordinates when extending the LineGeometry to correctly angle the segments.
calculateCoordinateOffset = function(length, angle) {
var _a = angle,
_l = length,
_x,
_y;
_x = _l * Math.sin(_a);
_y = _l * Math.cos(_a);
return [_x, _y];
};
The I add X and Y to the geometry coordinates of the last segment and add those coordinates onto the linestring geometry (addCoordinates()).
Any feedback would be good. My maths is traditionally VERY bad.
Unity3D offers the following method:
Rotate(eulerAngles: Vector3, relativeTo: Space = Space.Self);
For example, this will rotate the object around it's local X axis:
transform.Rotate(Vector3(50,0,0) * Time.deltaTime, Space.Local);
If I first rotate 90 degrees around it's local y axis (which is up in unity) and then rotate it around the X axis relative to World, it will basically rotate around the local Z axis, ie:
//setup
transform.Rotate(Vector3(0, 90, 0));
//on update
transform.Rotate(Vector3(50,0,0) * Time.deltaTime, Space.World);
In my own implementation, using quaternions, I have the local rotation implemented, which was easy.
//rotate around local axis
currentRotation *= rotateQuat;
How would I go about implementing the relative to world behaviour using quaternions?
There's probably a way to do it in Unity without explicit calculations, but...
When using quaternions for rotation, it makes more sense to think in terms of a rotation (angle a) around a specific axis (unit vector u) rather than using Euler angles; the quaternion itself (actually a unit quaternion, or "versor") can be represented as a 4-vector (w, x, y, z), where w = cos(0.5*a) and (x, y, z) = (u_x, u_y, u_z) * sin(0.5*a) (meaning a = 2 * arccos(w) and u = (x, y, z) / sin(0.5*a) = (x, y, z) / sin(arccos(w)) ). Following from this, the "identity" quaternion (i.e. no rotation) is (1, 0, 0, 0), as cos(0) = 1 and sin(0) = 0, and very usefully, the inverse/conjugate of a quaternion, (w, -x, -y, -z), represents the opposite rotation (x, y, and z are technically imaginary components; you can also represent a quaternion as w+x*i+y*j+z*k). To apply a rotation to a point p in 3D space using a quaternion, you use p' = (w, x, y, z)*(0, p_x, p_y, p_z)*(w, -x, -y, -z), where the multiplication is performed as shown here: http://en.wikipedia.org/wiki/Quaternion#Ordered_list_form
Thus, performing a world-relative rotation with a quaternion given an already-existing rotation represented by a quaternion is relatively simple if you know what the angle and axis of rotation (in world space) are for the new rotation, as the main addition to the computation is to apply the conjugate of the existing quaternion to the axis of rotation for the new quaternion in order to represent that axis in local/object space; then you just create the quaternion for the new rotation and apply it to the existing one (I'm not sure how Unity orders quaternion composition, but normally if you apply q1 followed by q2 the composed quaternion would be q2*q1 [quaternion multiplication is non-commutative], so it should be something like resultQuat = newQuat * prevQuat;).
I must be misunderstanding something about GLKit's handling of quaternions and rotation matrices. In the following snippet, I would expect matrices a and b to end up with identical contents (subject to floating point errors)...
GLKQuaternion q = GLKQuaternionMakeWithAngleAndAxis(M_PI / 2, 1, 1, 1);
GLKMatrix3 a = GLKMatrix3MakeWithQuaternion(q);
GLKMatrix3 b = GLKMatrix3MakeRotation(M_PI / 2, 1, 1, 1);
However, they don't agree. Not even close. In column major order, the arrays contain...
a.m[0]=0.000000 b.m[0]=0.333333
a.m[1]=1.000000 b.m[1]=0.910684
a.m[2]=0.000000 b.m[2]=-0.244017
a.m[3]=0.000000 b.m[3]=-0.244017
a.m[4]=0.000000 b.m[4]=0.333333
a.m[5]=1.000000 b.m[5]=0.910684
a.m[6]=1.000000 b.m[6]=0.910684
a.m[7]=0.000000 b.m[7]=-0.244017
a.m[8]=0.000000 b.m[8]=0.333333
I thought that both GLKQuaternionMakeWithAngleAndAxis and GLKMatrix3MakeRotation each took radians, x, y, z in order to represent a rotation of the specified radians around the specified axis. And I thought that GLKMatrix3MakeWithQuaternion was intended to convert from the quaternion representation to the matrix representation.
So, why don't those agree? Do I need to normalize the axis before the quaternion creation? This does, in fact, seem to fix the problem but I don't believe it is documented that way.
From GLKQuaternion.h
/*
Assumes the axis is already normalized.
*/
static inline GLKQuaternion GLKQuaternionMakeWithAngleAndAxis(float radians, float x, float y, float z);
So yes, you do need to normalize the axis before creating the quaternion.