Box rotation around multiple axises using Matrix4f

Display name

The question change a bit, I figured out how to rotate around a single axis

I want to rotate a box around the Y axis using an angle. The box has a size, and a Vector3f to signal the rotation.

To rotate the box correctly what I do is rotate the origin position then rotate the origin position plus the size, and use those two references to render the box.

However this rotation does not work correctly and causes rendering artifacts.

This is my code to rotate the positions:

    Matrix4f matrix = new Matrix4f();

    // Rotate the origin position
    Vector3f pos = new Vector3f(new Vector3f(blockX, blockY, blockZ));

    matrix.m03 = pos.x;
    matrix.m13 = pos.y;
    matrix.m23 = pos.z;

    Vector3f rot = new Vector3f(new Vector3f(0, 1f, 0f));

    Matrix4f.rotate((float) Math.toRadians(45f), rot, matrix, matrix);

    Vector3f locationMin = new Vector3f(matrix.m03, matrix.m13, matrix.m23);

    // Rotate the position with the size
    // Top left back is the position of the block
    Vector3f sizeRot = new Vector3f(new Vector3f(blockX + size, blockY + size, blockZ + size));
    matrix = new Matrix4f();

    matrix.m03 = sizeRot.x;
    matrix.m13 = sizeRot.y;
    matrix.m23 = sizeRot.z;

    rot = new Vector3f(new Vector3f(0, 1f, 0f));

    Matrix4f.rotate((float) Math.toRadians(45f), rot, matrix, matrix);

    Vector3f locationMax = new Vector3f(matrix.m03, matrix.m13, matrix.m23);

    // Then here I use the locationMax and the locationMin to render the cube

What could be wrong with this code? Is the logic I am using to rotate the box correct? as in rotate the origin position then rotate the origin position plus the size..

EDIT: I released that rotating after translating is stupid so instead I just rotated the locationMax which is not translated (it is only the size) then I translated and I still get the same result (Graphical Artifacts).

New Code:

    float rx = blockX, ry = blockY, rz = blockZ;
    Matrix4f matrix = new Matrix4f();

    Vector3f rot = new Vector3f(0, 1f, 0f);
    matrix = new Matrix4f();

    matrix.m03 = size;
    matrix.m13 = size;
    matrix.m23 = size;

    Matrix4f.rotate((float) Math.toRadians(45f), rot, matrix, matrix);
    matrix.translate(new Vector3f(rx, ry, rz), matrix);

    float mx = matrix.m03;
    float my = matrix.m13;
    float mz = matrix.m23;
    // Here is use rx, ry, rz and mx, my, mz to render the box

============ * I figured it out (See below)* =============

EDIT:

This is what I ended up doing:

    // Origin point
    Vector4f a = new Vector4f(blockX, blockY, blockZ, 1);

    // Rotate a matrix 45 degrees
    Matrix4f mat = new Matrix4f();
    mat.rotate((float) Math.toRandians(45f), new Vector3f(
            0, 1f, 0), mat);

    /* Transform the matrix to each point */

    Vector4f c = new Vector4f(size.x, 0, size.z, 1);
    Matrix4f.transform(mat, c, c);
    Vector4f.add(c, a, c);

    Vector4f b = new Vector4f(size.x, 0, 0, 1);
    Matrix4f.transform(mat, b, b);
    Vector4f.add(b, a, b);

    Vector4f d = new Vector4f(0, 0, size.z, 1);
    Matrix4f.transform(mat, d, d);
    Vector4f.add(d, a, d);

    // Here is use a, b, c, and d to render the box.

The problem with this is that I want to rotate around all axises and not only around the Y axis. This makes the code very long and unreadable and There are a lot of bugs when I try to rotate around all axises.

Update Question:

How do I take the above code and make it so I can rotate around all 3 axises. I want to do this so I can have a billboard that will always face the camera.

This is how I calculate the angle between the camera and the object:

    Vector3f angle = new Vector3f();
    // Calculate the distance between camera and object
    Vector3f.sub(game.getCamera().getLocation(),
            new Vector3f(blockX, blockY, blockZ), angle);

    // Calculate the angle around the Y axis.
    float vectorAngle = (float) ((float) Math.atan2(angle.z, angle.x) * -1 + (Math.PI / 2.0f));
Nathan Lafferty

Billboards are a very common application of computer graphics (as I'm sure you've noticed, since you're asking the question!)

Ultimately I think you are over complicating the problem, based on:

as in rotate the origin position then rotate the origin position plus the size..

For computer graphics, the most common transformations are Scaling, Translating, and Rotating, and you do these in an order to achieve a desired effect (traditionally you scale, then rotate about the origin, then translate the vertex's position).

Additionally, you will have three main matrices to render a model in 3d: World Matrix, View Matrix, and Projection Matrix. I believe you are having misunderstandings of transforming from Model Space to World Space.

Graphics TRS and Matrix info. If you are having conceptual problems, or this answer is insufficient, I highly recommend looking at this link. I have yet to find a better resource explaining the fundamentals of computer graphics.

So right at the moment, you have your three angles (in degrees, in a Vector3) corresponding to the angle difference in the X,Y, and Z coordinate spaces from your billboard and your camera. With this information, we generate the View matrix by first gathering all of our matrix transformations in one place.

I'm going to assume that you already have your Translation and Scaling matrices, and that they both work. This means that we only need to generate our Rotation matrix, and then transform that matrix with the scaling matrix, and then transforming that matrix by our translation matrix.

X Rotation X Rotation Matrix

Y RotationY Rotation Matrix

Z RotationZ Rotation Matrix

(Images taken from CodingLabs link above)

So you will generate these three matrices, using the X,Y, and Z angles you calculated earlier, and then transform them to consolidate them into a single matrix, transform that matrix by the scaling matrix, and then transform that matrix by the translation matrix. Now you have your awesome matrix that, when you multiply a a vertex by it, will transform that vertex into the desired size, rotation, and position.

So you transform every single vertex point by this generated matrix.

And then after that, you should be done! Using these techniques will hopefully simplify your code greatly, and set you on the right path :)

So now how about some code?

//I do not guarantee that this code compiles! I did not write it in an IDE nor did I compile it
float angleToRotX = 180f;
float angleToRotY = 90f;
float angleToRotZ = 0f;

// example vertex
Vector4f vertex = new Vector4f(0, 1, 0, 1);

// Rotate vertex's X coordinates by the desired degrees
Matrix4f rotationXMatrix = new Matrix4f();
rotationXMatrix.rotX(angleToRotX);

Matrix4f rotationYMatrix = new Matrix4f();
rotationYMatrix.rotY(angleToRotY);

Matrix4f rotationZMatrix = new Matrix4f();
rotationZMatrix.rotZ(angleToRotZ);

//now let's translate it by 1.5, 1, 1.5 in the X,Y,Z directions
Matrix4f translationMatrix = new Matrix4f();
translationMatrix.setTranslate(new Vector3f(1.5, 1, 1.5));

/*
Now we have our three rotational matrices. So we multiply them (transform them) to get a single matrix to transform all of the points in this model to the desired world coordinates

*/
Matrix4f rotationMatrix = new Matrix4f();
rotationMatrix.mul(rotationXMatrix);
rotationMatrix.mul(rotationYMatrix);
rotationMatrix.mul(rotationZMatrix);

Matrix4f worldMatrix = translationMatrix;
worldMatrix.mul(rotationMatrix);

//now worldMatrix, when applied to a vertex, will rotate it by X,Y,Z degrees about the origin of it's model space, and then translate it by the amount given in translationMatrix

worldMatrix.transform(vertex);

//now vertex should be (1.5, 0, 1.5, 1) with (x,y,z,1)

Now this code could really be simplified, and it is excessively verbose. Try it out! I don't have java downloaded on my machine, but I grabbed the methods from the java documentation Here

Here is an image of what is happening (again, taking from coding labs):

Example World Matrix

(Advanced Info: Quaternions. These are really cool way of orienting a model in 3d space, however I don't quite understand them to the degree I need to in order to explain it to someone else, and I also believe that your problem is more fundamental)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Rotation around an arbitrary point using transformation matrix

From Dev

Rotation around an arbitrary point using transformation matrix

From Dev

Using jquery "keydown" to move "box" around

From Dev

Absolute rotation using Matrix?

From Dev

Opencv Animation Rotation of object using rotation matrix

From Dev

Opengl Model Matrix doesnt rotate around the origin after one rotation

From Dev

CALayer Rotation around center

From Dev

OpenGl Rotation around point

From Dev

Rotation UIImageView around An anchorPoint

From Dev

Minimal Rectangle Bounding box around a region if an image using matlab

From Dev

how to apply space around property vertically when using flex box?

From Dev

Continuous object rotation around point

From Dev

Rotation around camera's axis

From Dev

clockwise (only) rotation around an element

From Dev

OpenCV, Rotation around specific point

From Dev

Strange rotation of rectangle around its center using d3.js

From Dev

calculate box around square

From Dev

Converting glm quaternion to rotation matrix and using it with opengl

From Dev

Using Rotation Matrix to rotate points in space

From Dev

Offset Euler Angles using rotation matrix

From Dev

Rotate the vertexes of a cube using a rotation matrix

From Dev

Offset Euler Angles using rotation matrix

From Dev

Rotating a Svg path around center using matrix

From Dev

Simulate 360 degree object rotation using multiple images on iOS

From Dev

Multiple select box without using ctr or shift

From Dev

How to Customize a multiple Select box using CSS

From Dev

Using radio box selection to display multiple DIVs

From Dev

R box plot using factors in multiple columns

From Dev

Unable to validate multiple select box using jQuery

Related Related

  1. 1

    Rotation around an arbitrary point using transformation matrix

  2. 2

    Rotation around an arbitrary point using transformation matrix

  3. 3

    Using jquery "keydown" to move "box" around

  4. 4

    Absolute rotation using Matrix?

  5. 5

    Opencv Animation Rotation of object using rotation matrix

  6. 6

    Opengl Model Matrix doesnt rotate around the origin after one rotation

  7. 7

    CALayer Rotation around center

  8. 8

    OpenGl Rotation around point

  9. 9

    Rotation UIImageView around An anchorPoint

  10. 10

    Minimal Rectangle Bounding box around a region if an image using matlab

  11. 11

    how to apply space around property vertically when using flex box?

  12. 12

    Continuous object rotation around point

  13. 13

    Rotation around camera's axis

  14. 14

    clockwise (only) rotation around an element

  15. 15

    OpenCV, Rotation around specific point

  16. 16

    Strange rotation of rectangle around its center using d3.js

  17. 17

    calculate box around square

  18. 18

    Converting glm quaternion to rotation matrix and using it with opengl

  19. 19

    Using Rotation Matrix to rotate points in space

  20. 20

    Offset Euler Angles using rotation matrix

  21. 21

    Rotate the vertexes of a cube using a rotation matrix

  22. 22

    Offset Euler Angles using rotation matrix

  23. 23

    Rotating a Svg path around center using matrix

  24. 24

    Simulate 360 degree object rotation using multiple images on iOS

  25. 25

    Multiple select box without using ctr or shift

  26. 26

    How to Customize a multiple Select box using CSS

  27. 27

    Using radio box selection to display multiple DIVs

  28. 28

    R box plot using factors in multiple columns

  29. 29

    Unable to validate multiple select box using jQuery

HotTag

Archive