Jump to content

Rotating a Mesh around a Pivot


ChrisR
 Share

Recommended Posts

yes but the pivot happens before the rotation which means that you can set a translation in the pivot, do your rotation and then reset your position

 

No way to achieve this without the pivot. As you may know matrices multiplication are not associative: AxB != BxA

 

the complete matrices chain in bjs is: PivotMatrix x Scaling x Rotation x Translation

Link to comment
Share on other sites

  • 1 year later...

Recently I needed to rotate a mesh around a pivot and started out using a parent. In the project the camera was always fixed on the mesh and the pivot needed to move from frame to frame and I had problems keeping the mesh in its correct position. A search for "rotating" and "pivot" brought me back to this topic which I had forgotten. So I decided if I found a solution I would post it here.

@ChrisR sorry to have taken so long (over a year) and hope what follows is a solution to what you wanted, even if you have solved it another way I hope others might find it useful.

What I wanted was for a mesh to rotate around an line that did not pass through its local origin. Two things are needed to fix a line, a point that the line passes through and a vector representing the direction of the line. You can think of these as a pivot point and an axis.

Given a pivot point (pivotAt) and an axis (axis) and an angle (theta) for the rotation the following will rotate a mesh around the line.

var m = BABYLON.Matrix.Identity().setTranslation(mesh.position.subtract(pivotAt));
var invm = m.clone().invert();
var r = BABYLON.Matrix.RotationAxis(axis,theta);
var ar = m.multiply(r).multiply(invm);
mesh.setPivotMatrix(ar);

http://www.babylonjs-playground.com/#29BGXO#3

Link to comment
Share on other sites

  • 4 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...