<p>I'm trying to write a little program with a bouncing cube but I can't find the right x, y, z and angle for it. I have these lines:</p>
<pre><code>modelMatrix = glm::mat4(1)

modelMatrix *= Translate(x, y, z)

modelMatrix *= RotateOZ(angle)

RenderMesh(mesh[&quot;cube&quot;], shaders[&quot;V&quot;], modelMatrix)
</code></pre>
<p>I added a picture to make it easier to understand. My cube will start at x0, y0, z0(we can leave the z out of this because for now I want the cube to move only in the OX axis and OY). I added a arrow the cube so I can represent the rotation better. First he will move forward on the OX and OY axis while rotating in the range of [0 - 90] grades when he will hit X1(which is X / 2) and Y1 then he will rotate in the range of [90 - 180] grades while moving forward on the OX and descending on the OY. I hope that made sense.</p>
<p><a href="https://i.sstatic.net/r0yU7.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/r0yU7.png" alt="enter image description here" /></a></p>
<p>My problem is that I can't find the right x, y and angle to give to these transformations so I can get the animation that I want. I tried to add to x, y and angle the same value such as 0.1 and the result is close but not perfect.
RotateOZ return a mat4:</p>
<pre><code>return glm::mat4(
        cos(angle), -sin(angle), 0, 0,
        sin(angle), cos(angle), 0, 0,
        0, 0, 1, 0,
        0, 0, 0, 1
    );
</code></pre>
<p>Thank you for your time!</p>

CC BY-SA 4.0
2020-12-11T06:25:07.237Z
Not sure if it solves your problem, but you should always rotate your objects first before moving them to their destination in the scene. Otherwise, the translations get "mixed" into the rotations since rotation matrices always rotate around the origin. Further information can be found [here](https://learnopengl.com/Getting-started/Transformations).
wychmaster
CC BY-SA 4.0
2020-12-12T17:50:46.097Z
Like @wychamaster said the matrix operations are not commutative wrt multiplication so you get a different result with: translate * rotate then you do with: rotate * translate. To get what you want do a "translate to origin", "then do the rotation", "then translate to position".
pmw1234
<3d><transformations><physics>
3D Bouncing Cube
NeedSomeHeelp
# 3D Bouncing Cube

(Asked by: username_0 on 2020-12-10)

I'm trying to write a little program with a bouncing cube but I can't find the right x, y, z and angle for it. I have these lines:

```
modelMatrix = glm::mat4(1)

modelMatrix *= Translate(x, y, z)

modelMatrix *= RotateOZ(angle)

RenderMesh(mesh["cube"], shaders["V"], modelMatrix)

```

I added a picture to make it easier to understand. My cube will start at x0, y0, z0(we can leave the z out of this because for now I want the cube to move only in the OX axis and OY). I added a arrow the cube so I can represent the rotation better. First he will move forward on the OX and OY axis while rotating in the range of [0 - 90] grades when he will hit X1(which is X / 2) and Y1 then he will rotate in the range of [90 - 180] grades while moving forward on the OX and descending on the OY. I hope that made sense.

[![enter image description here](https://i.sstatic.net/r0yU7.png)](https://i.sstatic.net/r0yU7.png)

My problem is that I can't find the right x, y and angle to give to these transformations so I can get the animation that I want. I tried to add to x, y and angle the same value such as 0.1 and the result is close but not perfect.
RotateOZ return a mat4:

```
return glm::mat4(
        cos(angle), -sin(angle), 0, 0,
        sin(angle), cos(angle), 0, 0,
        0, 0, 1, 0,
        0, 0, 0, 1
    );

```

Thank you for your time!

**Tags:** 3d, transformations, physics

> **Comments:**
> **username_1**: Not sure if it solves your problem, but you should always rotate your objects first before moving them to their destination in the scene. Otherwise, the translations get "mixed" into the rotations since rotation matrices always rotate around the origin. Further information can be found [here](https://learnopengl.com/Getting-started/Transformations).  
> **username_2**: Like @wychamaster said the matrix operations are not commutative wrt multiplication so you get a different result with: translate * rotate then you do with: rotate * translate. To get what you want do a "translate to origin", "then do the rotation", "then translate to position".  

---

## 0 answers

