<p>Think about matrix multiplications as transforming data from one coordinate system to another. So initially, your cubes' local coordinate system is identical to the &quot;global&quot; coordinate system. Every translation in the &quot;global&quot; system along one of its coordinate axes is a single component in the translation vector.</p>
<p>Now you rotate your cube and its local coordinate system so that they are no longer aligned with the &quot;global&quot; one. By the transformation order you chose, the translations are applied in the &quot;global&quot; system. If you want to apply a translation that was defined in your cubes' local coordinate system, you have to transform the translation vector into the &quot;global&quot; one first. In your case, the green line is one of the axes in the local coordinate system. So locally, the translation vector along this axis has one coordinate different from 0. But in the &quot;global&quot; one, all components might differ from 0.</p>
<p>So how do we find this vector? Fortunately, it is pretty simple since you already know how to transform between both coordinate systems. You already did it with the cube. So just apply the <strong>rotations</strong> you used for your vertices.
Unless I missed something in your code or made any other mistake, the function should look like this:</p>
<pre class="lang-cpp prettyprint-override"><code>void Mesh::moveAlongLocalAxis(QVector3D coordinates)
{   
    // transform local translation to global coordinate system
    coordinates = this-&gt;rotationMatrix * coordinates
    this-&gt;moveAlongGlobalAxis(coordinates); 
}
</code></pre>
<p>Note that I oversimplified some things in the text above that was tailored to help you out with your specific problem.
Coordinate systems and transformations are not particularly hard to understand, but it needs some time to adapt the correct mental model. You always have to keep in mind in which coordinate system you ended up after a set of transformations and how rotations and translations have to be applied to get you to the next one. I recommend <a href="https://learnopengl.com/Getting-started/Coordinate-Systems" rel="nofollow noreferrer">this site</a> to get a better understanding of the topic.</p>

CC BY-SA 4.0
2022-06-15T08:13:19.737Z
2022-06-15T08:13:19.737Z
wychmaster
<p>What do I have now?
I have a cube consisting of 6 planes. All these planes I generate in XY coordinates and then place them by matrix transformations.</p>
<p><a href="https://i.sstatic.net/PLe8w.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/PLe8w.png" alt="введите сюда описание изображения" /></a></p>
<p>I need to rotate my cube along the global axis and move my planes along cube rotation;</p>
<p>I rotate the cube
<a href="https://i.sstatic.net/elfwB.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/elfwB.png" alt="введите сюда описание изображения" /></a></p>
<p>Then I need to move one of the planes correctly. But the planes move along the global axis. The red line shows how the plane moves now. The green line shows how it should move.
<a href="https://i.sstatic.net/uZXso.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/uZXso.png" alt="введите сюда описание изображения" /></a></p>
<p>How I create a cube. All vertices are in the range (0,0) - (2, 2);</p>
<pre><code>    planeXY.setupMesh();
    planeXY.setOrigin({1, 1, 1});
    
    planeXY1.setupMesh();
    planeXY1.setOrigin({1, 1, 1});
    planeXY1.moveAlongGlobalAxis(QVector3D(0.0, 0.0, 2.0));
    
    planeZY.setupMesh();
    planeZY.setOrigin({1, 1, 1});
    planeZY.rotate(QVector3D(0.0f, -90.0f, 0.0f));
    planeZY.moveAlongGlobalAxis(QVector3D(-2.0, 0.0, 0.0));
    
    
    planeZY1.setupMesh();
    planeZY1.setOrigin({1, 1, 1});
    planeZY1.rotate(QVector3D(0.0f, -90.0f, 0.0f));
    
    planeXZ.setupMesh();
    planeXZ.setOrigin({1, 1, 1});
    planeXZ.rotate(QVector3D(90.0f, 0.0f, 0.0f));
    planeXZ.moveAlongGlobalAxis(QVector3D(0.0, -2.0, 0.0));
    
    planeXZ1.setupMesh();
    planeXZ1.setOrigin({1, 1, 1});
    planeXZ1.rotate(QVector3D(90.0f, 0.0f, 0.0f));
</code></pre>
<p>Mesh.cpp</p>
<pre><code>void Mesh::moveAlongGlobalAxis(QVector3D coordinates)
{
    QMatrix4x4 identityMatrix;   
    identityMatrix.translate(coordinates);
    position += coordinates;
    this-&gt;translationMatrix = identityMatrix * translationMatrix;
}

void Mesh::moveAlongLocalAxis(QVector3D coordinates)
{   
    this-&gt;moveAlongGlobalAxis(coordinates); 
}

void Mesh::rotate(QVector3D rotation)
{
    QMatrix4x4 identityMatrix;
    
    identityMatrix.translate((-1) * this-&gt;position + this-&gt;origin);
    identityMatrix.rotate(rotation.x(), QVector3D(1.0, 0.0, 0.0));
    identityMatrix.rotate(rotation.y(), QVector3D(0.0, 1.0, 0.0));
    identityMatrix.rotate(rotation.z(), QVector3D(0.0, 0.0, 1.0));
    identityMatrix.translate(this-&gt;position - this-&gt;origin);
    this-&gt;rotationMatrix =  identityMatrix * this-&gt;rotationMatrix;
}


void Mesh::setOrigin(QVector3D origin)
{
    this-&gt;origin = origin;
}

const QMatrix4x4 Mesh::getModelMatrix() const
{
    
    return translationMatrix * rotationMatrix;
}

</code></pre>
<p>This function I want to implement:</p>
<pre><code>void Mesh::moveAlongLocalAxis(QVector3D coordinates)
{   
    this-&gt;moveAlongGlobalAxis(coordinates); 
}
</code></pre>
<p>How I rotate my cube</p>
<pre><code>else if(ev-&gt;key() == Qt::Key_R)
    {
        
        
        
        planeXY.rotate(QVector3D(20.0f, 0.0f, 0.0f));
        planeXY1.rotate(QVector3D(20.0f, 0.0f, 0.0f));
        planeZY.rotate(QVector3D(20.0f, 0.0f, 0.0f));
        planeZY1.rotate(QVector3D(20.0f, 0.0f, 0.0f));
        planeXZ.rotate(QVector3D(20.0f, 0.0f, 0.0f));
        planeXZ1.rotate(QVector3D(20.0f, 0.0f, 0.0f));
    
    }
</code></pre>

<opengl><c++>
How to rotate and move group of objects in OpenGL?
Андрей Петров
# How to rotate and move group of objects in OpenGL?

(Asked by: username_0 on 2022-06-14)

What do I have now?
I have a cube consisting of 6 planes. All these planes I generate in XY coordinates and then place them by matrix transformations.

[![введите сюда описание изображения](https://i.sstatic.net/PLe8w.png)](https://i.sstatic.net/PLe8w.png)

I need to rotate my cube along the global axis and move my planes along cube rotation;

I rotate the cube
[![введите сюда описание изображения](https://i.sstatic.net/elfwB.png)](https://i.sstatic.net/elfwB.png)

Then I need to move one of the planes correctly. But the planes move along the global axis. The red line shows how the plane moves now. The green line shows how it should move.
[![введите сюда описание изображения](https://i.sstatic.net/uZXso.png)](https://i.sstatic.net/uZXso.png)

How I create a cube. All vertices are in the range (0,0) - (2, 2);

```
    planeXY.setupMesh();
    planeXY.setOrigin({1, 1, 1});
    
    planeXY1.setupMesh();
    planeXY1.setOrigin({1, 1, 1});
    planeXY1.moveAlongGlobalAxis(QVector3D(0.0, 0.0, 2.0));
    
    planeZY.setupMesh();
    planeZY.setOrigin({1, 1, 1});
    planeZY.rotate(QVector3D(0.0f, -90.0f, 0.0f));
    planeZY.moveAlongGlobalAxis(QVector3D(-2.0, 0.0, 0.0));
    
    
    planeZY1.setupMesh();
    planeZY1.setOrigin({1, 1, 1});
    planeZY1.rotate(QVector3D(0.0f, -90.0f, 0.0f));
    
    planeXZ.setupMesh();
    planeXZ.setOrigin({1, 1, 1});
    planeXZ.rotate(QVector3D(90.0f, 0.0f, 0.0f));
    planeXZ.moveAlongGlobalAxis(QVector3D(0.0, -2.0, 0.0));
    
    planeXZ1.setupMesh();
    planeXZ1.setOrigin({1, 1, 1});
    planeXZ1.rotate(QVector3D(90.0f, 0.0f, 0.0f));

```

Mesh.cpp

```
void Mesh::moveAlongGlobalAxis(QVector3D coordinates)
{
    QMatrix4x4 identityMatrix;   
    identityMatrix.translate(coordinates);
    position += coordinates;
    this->translationMatrix = identityMatrix * translationMatrix;
}

void Mesh::moveAlongLocalAxis(QVector3D coordinates)
{   
    this->moveAlongGlobalAxis(coordinates); 
}

void Mesh::rotate(QVector3D rotation)
{
    QMatrix4x4 identityMatrix;
    
    identityMatrix.translate((-1) * this->position + this->origin);
    identityMatrix.rotate(rotation.x(), QVector3D(1.0, 0.0, 0.0));
    identityMatrix.rotate(rotation.y(), QVector3D(0.0, 1.0, 0.0));
    identityMatrix.rotate(rotation.z(), QVector3D(0.0, 0.0, 1.0));
    identityMatrix.translate(this->position - this->origin);
    this->rotationMatrix =  identityMatrix * this->rotationMatrix;
}

void Mesh::setOrigin(QVector3D origin)
{
    this->origin = origin;
}

const QMatrix4x4 Mesh::getModelMatrix() const
{
    
    return translationMatrix * rotationMatrix;
}

```

This function I want to implement:

```
void Mesh::moveAlongLocalAxis(QVector3D coordinates)
{   
    this->moveAlongGlobalAxis(coordinates); 
}

```

How I rotate my cube

```
else if(ev->key() == Qt::Key_R)
    {
        
        
        
        planeXY.rotate(QVector3D(20.0f, 0.0f, 0.0f));
        planeXY1.rotate(QVector3D(20.0f, 0.0f, 0.0f));
        planeZY.rotate(QVector3D(20.0f, 0.0f, 0.0f));
        planeZY1.rotate(QVector3D(20.0f, 0.0f, 0.0f));
        planeXZ.rotate(QVector3D(20.0f, 0.0f, 0.0f));
        planeXZ1.rotate(QVector3D(20.0f, 0.0f, 0.0f));
    
    }

```

**Tags:** opengl, c++


---

## 1 answers

### Answer by username_1 on 2022-06-15. Score: 1

Think about matrix multiplications as transforming data from one coordinate system to another. So initially, your cubes' local coordinate system is identical to the "global" coordinate system. Every translation in the "global" system along one of its coordinate axes is a single component in the translation vector.

Now you rotate your cube and its local coordinate system so that they are no longer aligned with the "global" one. By the transformation order you chose, the translations are applied in the "global" system. If you want to apply a translation that was defined in your cubes' local coordinate system, you have to transform the translation vector into the "global" one first. In your case, the green line is one of the axes in the local coordinate system. So locally, the translation vector along this axis has one coordinate different from 0. But in the "global" one, all components might differ from 0.

So how do we find this vector? Fortunately, it is pretty simple since you already know how to transform between both coordinate systems. You already did it with the cube. So just apply the **rotations** you used for your vertices.
Unless I missed something in your code or made any other mistake, the function should look like this:

```cpp
void Mesh::moveAlongLocalAxis(QVector3D coordinates)
{   
    // transform local translation to global coordinate system
    coordinates = this->rotationMatrix * coordinates
    this->moveAlongGlobalAxis(coordinates); 
}

```

Note that I oversimplified some things in the text above that was tailored to help you out with your specific problem.
Coordinate systems and transformations are not particularly hard to understand, but it needs some time to adapt the correct mental model. You always have to keep in mind in which coordinate system you ended up after a set of transformations and how rotations and translations have to be applied to get you to the next one. I recommend [this site](https://learnopengl.com/Getting-started/Coordinate-Systems) to get a better understanding of the topic.


---

