{"id":1549,"date":"2016-10-17T10:27:54","date_gmt":"2016-10-17T10:27:54","guid":{"rendered":"http:\/\/www.migenius.com\/?p=1549"},"modified":"2016-10-25T03:46:58","modified_gmt":"2016-10-25T03:46:58","slug":"3d-transformations-part1-matrices","status":"publish","type":"post","link":"https:\/\/www.migenius.com\/articles\/3d-transformations-part1-matrices","title":{"rendered":"3D Transformations – Part 1 Matrices"},"content":{"rendered":"
Transformations are fundamental to working with 3D scenes and something that can be frequently confusing to those that haven’t worked in 3D before. In this, the first of two articles I will show you how to encode 3D transformations as a single 4×4 matrix which you can then pass into the appropriate RealityServer command to position, orient and scale objects in your scene. In a second part I will dive into a newer method of specifying transformations in RealityServer called SRT transformations which also allows for the easy animation of objects.<\/p>\n
<\/p>\n
There is a bit of math in this article since we are dealing with matrices<\/a>. A basic knowledge of vectors and matrices is assumed, however you don’t need to have done a full blown vector calculus course to understand how to make practical use of the information here. If you are so inclined however there is a lot of great information online, Google is your friend.<\/p>\n Also just a quick note, for angles expressed in this article we will be using radians<\/a> rather than degrees. While it is common for user interfaces to present angles in degrees, the built in trigonometric functions of most programming languages expect angles in radians. You can easily code the conversion if needed using the following formula.<\/p>\n\n\n\n Another important thing to remember is that RealityServer uses a right handed coordinate system<\/a>. This dictates which way rotations will proceed around an axis.\u00a0In our examples we will be assuming Z<\/em> is our up axis. However this is a completely arbitrary choice and you can easily use Y<\/em> as your up axis if you like (or any other axis for that matter). This only affects how you setup your camera. Up-ness<\/em> is a topic for another article however.<\/p>\n In RealityServer, all transformations are encoded as a world to object space affine transformation<\/em> encoded as a 4×4 matrix in row major order. Since the transform is from world to object space it may be the inverse of what you commonly see in other 3D applications. By default, when instances of objects are created they are assigned the default transformation matrix which is the identity matrix. This has no effect on the object at all.<\/p>\n\n\n\n We will be setting the values in this matrix in order to translate, rotate and scale our object. Note that object transformations can be nested along the scene graph. So if you had an instance which translated an object 3 units in the x direction which is contained within a group that is translated another 2 units in the x direction then the object will be translated by 5 units in the end.<\/p>\n If are already working with an application that uses transformation matrices but they are in object to world space form, you can usually simply invert the matrix before use in RealityServer. It if is also in column major rather than row major form then you will also need to transpose<\/a> the matrix before inverting it. If building your matrices from scratch then if you follow this article you should be fine.<\/p>\n The examples in this article are the raw JSON-RPC command sequences that get sent to RealityServer. If you have not done so already please read the article on Exploring the RealityServer JSON-RPC API<\/a>.<\/p>\n Throughout this article we will be applying transformations to a simple object in a test scene I\u00a0have created. I’ll use a box which is 4 units long, 2 units high and 1 unit thick. The large grid areas on the floor are 1 unit in size and the small grid areas are 0.2 units. You can see our initial scene with our test object in it at its initial position to the right (it has the identity transformation applied to it).<\/p>\n First I will cover the three fundamental elements of transformations – translation, rotation and scaling. I’ll show you how to create matrices to represent each of these and then how to combine all three operations into a single matrix using matrix multiplication.<\/p>\n Even if you do not understand the mathematics involved you should still be able to follow along well enough to code transformations into your application by following the examples.<\/p>\n<\/div><\/div>Starting Point<\/h3>\n