Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Camera.translate and Matrix.preTranslate or Matrix.postTranslate?

We use Camera to do 3D transformations in canvas.We usually rotate camera and get it's Matrix then translate it.But Camera also has translate method.The results of using methods are different.

My question is : What is difference between Camera.translate and Matrix.preTranslate or Matrix.postTranslate?

like image 504
hasanghaforian Avatar asked Nov 17 '12 09:11

hasanghaforian


2 Answers

The reason there are both, is because matrix multiplication must be done in a certain order to achieve the proper result (as you may already know).

The sequence of translations/rotations/scales are done in reverse order as you type them.

So if you do something like this:

Camera.rotate(15, 0, 0);
Camera.scale(.5f, .5f, .5f);
Camera.translate(70, 70, 70);

You're first translating 70,70,70 then scaling by 50% in all directions, then rotating 15 degrees about the X axis.

So Matrix has a pre and post translate (well, pre and post everything), because maybe you want to actually rotate it first by 15 degrees and then translate it, and then finally scale it.

So that answers the pre and post translates. Now the reason Camera has a straight rotate and translate is for people that know how this works already (like me!), so I never use Matrix or Camera for that matter, because I can simply do my rotations and translations directly on the Canvas. You can too as long as you know that translations, scales, and rotates are done in reverse order.

Also, if you know what I have told you, it gives you more power. You can do a sequence of 10 matrices without surrounding them in multiple Matrix objects for each one (for example you want to do a swing motion that swings outward AND rotates about the center to simulate centrifugal force). This would need to be done with multiple rotates and translations (surrounded by multiple Matrix objects being passed into one another), but if you know how each translate works, you can simply do a series of .translate(), .rotate(), and .scale().

This information is especially useful if you ever do 3D graphics, because that's when these matrices give people headaches.

I hope this helps!

like image 99
Michael Dotson Avatar answered Nov 19 '22 15:11

Michael Dotson


The result would be visually the same if you i.e. do not touch the canvas but rotate the camera 90 degs or keep camera still but rotate the canvas it looks at by -90 degs.

like image 44
Marcin Orlowski Avatar answered Nov 19 '22 15:11

Marcin Orlowski