2
$\begingroup$

I am implementing simple rotation but the object is not rotating around the local center instead it is rotating around world origin.

This is the code:

Matrix2D m = Matrix2D.Rotate((float) (Math.PI));

Point p =  new Point(new Vector2D(-0.1f,0.7f),this, new PixelData(255, 255, 0, 0));
Point p2 = new Point(new Vector2D(0,0.8f),this, new PixelData(255, 255, 0, 0));
Point p3 = new Point(new Vector2D(0.1f,0.7f),this, new PixelData(255, 255, 0, 0));

Vector2D v = Matrix2D.MatrixTimesVector2D(m, p.getPosition());
Vector2D v2 = Matrix2D.MatrixTimesVector2D(m, p2.getPosition());
Vector2D v3 = Matrix2D.MatrixTimesVector2D(m, p3.getPosition());

Point z = new Point(v, this, p.getColour());
Point z2 = new Point(v2, this, p.getColour());
Point z3 = new Point(v3, this, p.getColour());

new Triangle(z2.Transform(), z.Transform(), z3.Transform(),this).DrawTriangle();

I checked my matrices and matrix multiplication is correct.

Result without rotation:

enter image description here

And after rotation:

enter image description here

Why i am getting this weird rotation ?

$\endgroup$

1 Answer 1

2
$\begingroup$

Your triangle's coordinates: (-0.1f,0.7f) (0.0f,0.8f) (0.1f,0.7f) are defined with the origin at the center of the screen.

Multiplying a rotation matrix by a vertex position will rotate around the point (0,0,0). In your case, that is the center of the screen.

Possible solution:

  • Define the vertices as: (-0.1f,-0.5f) (0.0f,0.5f,) and (0.1f,-0.5f)
  • Rotate the vertices with your rotation matrix (Like you did):

    Vector2D v = Matrix2D.MatrixTimesVector2D(m, p.getPosition());

    Vector2D v2 = Matrix2D.MatrixTimesVector2D(m, p2.getPosition());

    Vector2D v3 = Matrix2D.MatrixTimesVector2D(m, p3.getPosition());

  • If you want your triangle at the top of you screen, use a translation matrix: enter image description here

I'm not sure what language/library you are using, but create a translation matrix by yourself or using a built in fuction. Above dx, dy and dz are the translations in x,y and z.

Multiply your positions v, v2 and v3 by the translation matrix.

Google tranlsation and rotation matrix and you will understand better! Good luck!

$\endgroup$
1
  • $\begingroup$ Thanks, i thought i need a translation matrix but i didn't know that i messed the order of operation. $\endgroup$
    – user3437
    Jun 7, 2016 at 6:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy