-
Notifications
You must be signed in to change notification settings - Fork 293
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
is there a way of rotating a model around a point instead of around itself? #1783
Comments
Hi @hamza-hajji , This is an interesting problem that can be solved with a following approach: A 3D transformation matrix that represents the case you described can be created as follows:
There are many libraries that abstract such operations, and xeokit SDK exposes such a library as well, as the function eulerRotationAroundPoint(eulerRotation, center) {
// Matrix that translates `center` to origin.
const t1 = math.translationMat4v(math.subVec3([0,0,0], center, math.vec3()), math.identityMat4());
// Quaternion and matrix representing the rotation
const q2 = math.eulerToQuaternion(eulerRotation, "XYZ", math.vec4());
const r2 = math.quaternionToMat4(q2, math.identityMat4());
// Matrix that translates `center` back to its place.
const t3 = math.translationMat4v(center, math.identityMat4());
// Combine the transformations (order is important)
const m = math.mat4();
math.mulMat4(t3, math.mulMat4(r2, t1, m), m);
return m;
} and then used as: const newRotation = [0, 90, 0];
// global center
const center = [0, 0, 0];
model.matrix = eulerRotationAroundPoint(newRotation, center); Hope this helps! |
@MichalDybizbanskiCreoox Thanks for your response, but for some reason after applying that algo, the model moves to position
I call it then in the console |
Hi @hamza-hajji , object.matrix = math.mulMat4(
eulerRotationAroundPoint(rotation, center),
math.translationMat4v(position, math.identityMat4()),
math.mat4()); That represents a sequence of transformations:
BTW if you wanted to rotate a model around a pivot while preserving existing transformation, you could achieve it by object.matrix = math.mulMat4(
eulerRotationAroundPoint(rotation, center),
object.matrix,
math.mat4()); |
Is your feature request related to a problem? Please describe.
I want a way to rotate an object based on a different origin, not the default one. If I load a model , I can specify its origin in the load params
If I rotate it like this, it's correctly rotated according to that local origin, but I wish there was a way that while rotating using
model.rotation = [x, y, z]
we also provide an global origin for that rotationContext
I want to rotate a group of individual models around the center point which is the average of all their positions, so I need to set each of their global origins as that center
Describe the solution you'd like
This could be a function called like this
This would rotate a model around the point (0, 0, 0) in the world, not the origin (0, 0, 0) we provide when loading the model
Alternative solutions I tried
I tried to group the models under one Node, like with Meshes but that apparenly only works with Meshes, I wonder if it's possible?
Thanks for consideration
The text was updated successfully, but these errors were encountered: