0

Video Link for the issue.

https://www.youtube.com/watch?v=iqX1RPo4NDE&feature=youtu.be

This is what i want to attain.

https://www.youtube.com/watch?v=bWwYV0VhXqs

Here after scaling the object i can move pivot independently , position of the pivot does not affect the position of the object.

These are my matrices. when i move the pivot point to one unit in x and if the scaling is set to 1 than everthing works fine.

the pivot point has moved to one unit and the cube has stayed in its position.

But when i first scale the object to 0.5 and than move the pivot point than the cube follows the pivot point , which should not be he case as i am only moving pivot point.

Please help me with this , how can i keep the cube in position.

Though i am moving the axis not the cube so the cube should stay in original position.

glm::mat4x4 Container::GetPositionMatrix()
{
    // posx  is the x position of the object.
    // posy  is the y position of the object.
    // posz  is the y position of the object.
    glm::mat4 TransformationPosition = glm::translate(glm::mat4x4(1.0),
    glm::vec3(posx, posy, posz ));
    return TransformationPosition;
}
glm::mat4x4 Container::GetRotationMatrix()
{
    // posx  is the x positon of the object
    // pivotx is the x position on the pivot point
    // rotx is the x rotation of the object
    glm::vec3 pivotVector(posx - pivotx, posy - pivoty, posz - pivotz);
    glm::mat4 TransPivot = glm::translate(glm::mat4x4(1.0f), pivotVector);
    glm::mat4 TransPivotInverse = glm::translate(glm::mat4x4(1.0f), 
    glm::vec3( -pivotVector.x , -pivotVector.y , -pivotVector.z));
    glm::mat4 TransformationRotation(1.0);
    TransformationRotation = glm::rotate(TransformationRotation, 
        glm::radians(rotx), glm::vec3(1.0, 0.0, 0.0));
    TransformationRotation = glm::rotate(TransformationRotation, 
        glm::radians(roty), glm::vec3(0.0, 1.0, 0.0));
    TransformationRotation = glm::rotate(TransformationRotation, 
        glm::radians(rotz ), glm::vec3(0.0, 0.0, 1.0));
    return  TransPivotInverse * TransformationRotation * TransPivot;
}
glm::mat4x4 Container::GetScalingMatrix()
{
    // posx  is the x positon of the object
    // pivotx is the x  position on the pivot point
    // scax is the x scaling of the object
    glm::vec3 pivotVector(posx - pivotx, posy - pivoty, posz - pivotz);
    glm::mat4 TransPivot = glm::translate(glm::mat4x4(1.0f), pivotVector);
    glm::mat4 TransPivotInverse = glm::translate(glm::mat4x4(1.0f),
        glm::vec3(-pivotVector.x, -pivotVector.y, -pivotVector.z));
    glm::mat4 TransformationScale = glm::scale(glm::mat4x4(1.0 ), 
        glm::vec3(scax, scay, scaz));
    return  TransPivotInverse * TransformationScale * TransPivot;
}

final matrix for the Object position.

TransformationPosition * TransformationRotation * TransformationScaling

This is my final matrix for the pivot point

 PivotPointPosition = MatrixContainerPosition * MatrixPivotPointPosition * 
 MatrixRotationContainer * MatrixScaleContainer
  • Could you please clarify how your pivot is supposed to behave and what we see in the images (why are there two cubes in the second picture, where is the coordinate system?) I suppose the pivot is the object center? Usually, you specify rotation, scaling, and translation with respect to the object center. I.e., your position is the position of the object center. Is this not what you want? Why does the position appear in the pivot vector? – Nico Schertler Jun 30 '19 at 18:52
  • @Nico Schertler i have made edits to my question , please have a look. –  Jul 01 '19 at 03:41
  • @Nico Schertler the pivot can be freely moved around the object and all the rotation and scaling should happen with respect to pivot position. –  Jul 01 '19 at 03:44
  • Everthing works fine if the scaling is set to 1 , but if i change the scaling of the object than object starts to move along with pivot point. –  Jul 01 '19 at 03:45
  • @Nico Schertler please have a look at this link https://www.youtube.com/watch?v=7uS9jTgj6i4&feature=youtu.be –  Jul 01 '19 at 06:35
  • This is a result of your (what I think to be) inconsistent definition. As I said, I would expect the "position" to reference the position of the object's center in world space. Let's go through it - what do you expect to happen. Your position is set to `(0, 0, 0)` and your pivot is at `(1, 0, 0)`. If you apply a scaling of `(0.5, 0, 0)` with respect to that pivot, then of course the object moves closer to the pivot. The fact that you set the parameters in a different order does not change that. – Nico Schertler Jul 01 '19 at 15:59
  • @Nico Schertler yes it should move closer to pivot if i apply scaling. –  Jul 01 '19 at 16:02
  • but when i move my pivot only than the object should stay in position. –  Jul 01 '19 at 16:03
  • I should be able to position pivot independently irrespective of the scaling set on the object. –  Jul 01 '19 at 16:05
  • @Nico Schertler this is what i want to attain. https://www.youtube.com/watch?v=6nqG8oBB9IM&feature=youtu.be –  Jul 01 '19 at 16:10
  • In my case when i scale the object and move the pivot than object also moves along with the pivot, but here i am able to move pivot independently without moving the object. –  Jul 01 '19 at 16:21
  • 2
    In this case, the pivot does only seem to affect rotation and not the scaling. – Nico Schertler Jul 01 '19 at 17:12
  • 1
    I agree with @NicoSchertler. `Container::GetScalingMatrix()` should `return TransformationScale;` – Rabbid76 Jul 01 '19 at 18:43

1 Answers1

1

The object should be orientated and located as follows:

The reference point (pivotx, pivoty, pivotz) is a point in object space.
The object has to be scaled (scax, scay, scaz) and rotated (rotx, roty, rotz) relative to the reference point (pivotx, pivoty, pivotz).
The point (posx, posy, posz) defines the position of the object in the scene, where the reference point has finally to be placed.

Do the following steps:

  1. Scale the object to the desired size, with respect to the reference point:
glm::mat4 GetScalingMatrix()
{
    glm::vec3 refVector(pivotx, pivoty, pivotz);

    glm::mat4 TransRefToOrigin   = glm::translate(glm::mat4(1.0f), -refVector);
    glm::mat4 TransRefFromOrigin = glm::translate(glm::mat4(1.0f),  refVector);

    glm::vec3 scale = glm::vec3(scax, scay, scaz);
    glm::mat4 TransformationScale = glm::scale(glm::mat4(1.0), scale);
    return TransRefFromOrigin * TransformationScale * TransRefToOrigin;
}
  1. Rotate the scaled object around the pivot, like in the answer to one of your previous questions (How to use Pivot Point in Transformations):
glm::mat4 GetRotationMatrix()
{
    glm::vec3 pivotVector(pivotx, pivoty, pivotz);

    glm::mat4 TransPivotToOrigin   = glm::translate(glm::mat4(1.0f), -pivotVector);
    glm::mat4 TransPivotFromOrigin = glm::translate(glm::mat4(1.0f),  pivotVector);

    glm::mat4 TransformationRotation(1.0);
    TransformationRotation = glm::rotate(TransformationRotation, 
        glm::radians(rotx), glm::vec3(1.0, 0.0, 0.0));
    TransformationRotation = glm::rotate(TransformationRotation, 
        glm::radians(roty), glm::vec3(0.0, 1.0, 0.0));
    TransformationRotation = glm::rotate(TransformationRotation, 
        glm::radians(rotz), glm::vec3(0.0, 0.0, 1.0));

    return  TransPivotFromOrigin * TransformationRotation * TransPivotToOrigin;
}
  1. Move the scaled and rotated object to its final position (posx, posy, posz).
glm::mat4 GetPositionMatrix()
{
    glm::vec3 trans = glm::vec3(posx-pivotx, posy-pivoty, posz-pivotz);
    glm::mat4 TransformationPosition = glm::translate(glm::mat4(1.0), trans);
    return TransformationPosition;
}

The order matters:

glm::mat4 model = GetPositionMatrix() * GetRotationMatrix() * GetScalingMatrix(); 

All this can be simplified:

// translate "pivot" to origin 
glm::mat4 ref2originM = glm::translate(glm::mat4(1.0f), -glm::vec3(pivotx, pivoty, pivotz));        

// scale
glm::mat4 scaleM = glm::scale(glm::mat4(1.0), glm::vec3(scax, scay, scaz));

// rotate
glm::mat4 rotationM(1.0);
rotationM = glm::rotate(rotationM, glm::radians(rotx), glm::vec3(1.0, 0.0, 0.0));
rotationM = glm::rotate(rotationM, glm::radians(roty), glm::vec3(0.0, 1.0, 0.0));
rotationM = glm::rotate(rotationM, glm::radians(rotz), glm::vec3(0.0, 0.0, 1.0));

// translate to "pos"
glm::mat4 origin2posM = glm::translate(glm::mat4(1.0), glm::vec3(posx, posy, posz));

// concatenate matrices
glm::mat4 model = origin2posM * rotationM * scaleM * ref2originM;
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Thanks for the answer , i tried but still no not working. –  Jul 02 '19 at 06:19
  • 2
    @shomit *"no not working"* - is not a statement with describes an error. It works, but probably it doesn't work as you expect it to work. I tried to understand what you want to achieve, but it seems I misunderstood. So please explain, what is the difference in the behavior, between the solution and the expected behavior. – Rabbid76 Jul 02 '19 at 06:25
  • the pivot is still moving the object. –  Jul 02 '19 at 06:40
  • this is what i want to happen. https://www.youtube.com/watch?v=bWwYV0VhXqs&feature=youtu.be –  Jul 02 '19 at 06:43
  • I would be raising bounty on this question today. –  Jul 02 '19 at 06:43
  • This is my final matrix for the CenterPoint. MatrixContainerPosition * MatrixCenterPointPosition * MatrixRotationContainer * MatrixScaleContainer ; –  Jul 02 '19 at 06:53
  • This is my final matrix for the Object TransformationRotation * TransformationPosition * TransformationScaling ; –  Jul 02 '19 at 06:53
  • 1
    @shomit Note, the rotation matrix (`GetRotationMatrix`) doesn't depend on on the position. It depends on the pivot (`pivotx, pivoty, pivotz`) only. – Rabbid76 Jul 02 '19 at 19:25
  • -> i Tried the changes , Issue-> the object does not scale with respect to pivot position , Issue-> if i move object position than it does not rotate around the pivot point. –  Jul 03 '19 at 07:05
  • same like autocad or Maya. –  Jul 03 '19 at 07:11
  • only thing missing now is that when i move pivot it moves the object , rest all is the way it should be working. –  Jul 03 '19 at 07:54
  • what i saw in the other software is that it keeps updating the object position based on the pivot position. –  Jul 03 '19 at 07:55
  • I saw your profile that you work for elitecad and saw the work being done by elite cad, since it is a designing software i am sure pivot would have the same functionality in elite cad also. –  Jul 03 '19 at 08:07
  • what i am aiming for. –  Jul 03 '19 at 08:07
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/195913/discussion-between-shomit-and-rabbid76). –  Jul 03 '19 at 08:43
  • 1
    Finally I think I've understood what you try to do and what you meant by *"pivot moves the object"*. I tried to sum up what you want to do at the begin of the question and I provided a solution. – Rabbid76 Jul 03 '19 at 18:09
  • Thank you very much for your efforts , Really appreciate your help. –  Jul 04 '19 at 13:51