0

I want to write Custom Matrix class that will allow me to do following things:

Matrix m = Matrix().identity();
m.rotateAngle(30);
m.scale(2.0);
m.scale(1.5);
m.rotateAngle(30);

All operations above are clear for me but Now I want such thing to do: I want to write a function like: setRotationAngle or setScale which will set value what's passed no matter what's in current value

// for example:
m.setAngle(45); // clears the currentRotationAngle and then sets rotation to 45

Is there a way to do that? or Game Engines and such stuff just rebuild the matrix if someone wants to set some property?

Is there some tutorial which will help me to understand that problem?

kjhkjhkjh
  • 412
  • 3
  • 12

1 Answers1

2

I think that you do not really understand how transformation matrices work and how they are composed. I might be wrong, but if you ask this, I suspect it to be.

You should ask yourself this: How would I answer to this (this is about 2D)?

Consider a square at the origin.
Rotate is with 4 degrees.
Translate it with (4,4).
Set the rotation to 10 degrees.

What would you do at the step in bold? To me, this makes totally no sense.

So, I guess you will have to recompose a matrix. Consider using a matrix stack to which you can push matrixes to which you want to come back later by popping them back off.

Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
  • You can't do push and pop because I have only one Transformation matrix for each object: Class Triangle { Matrix transformation; ... }; and I'm doing all operations to that matrix.. – kjhkjhkjh Dec 26 '13 at 14:11
  • 1
    @user1268492: Well then implement a stack. It's not really that hard, just a linked list/queue where you push down copies of the current state when you, well, push it. And poping restores the current state with the topmost element of the list, removing it. – datenwolf Dec 26 '13 at 15:27
  • But it will take too much memory.. so that means that I have to save different matrices for different operations – kjhkjhkjh Dec 26 '13 at 16:21
  • Yes, it will take memory: To store 1000 2D matrixes: 39 kilobytes. – Martijn Courteaux Dec 26 '13 at 16:25
  • I think the thing I need is explained here: http://stackoverflow.com/questions/4361242/extract-rotation-scale-values-from-2d-transformation-matrix/4361442#4361442 //So it will be like: Matrix m = triangle.transform.getRotationMatrix(); triangle.transform.multMatrix(m.inverseMatrix()); triangle.transform.rotate(AngleIWantToRotate); Doesn't it seem right? – kjhkjhkjh Dec 26 '13 at 16:44