# Basic algebra

## How to: Translate - Rotate - Scale

Basic algebra for users. When I hear about Matrixes and Vectors I am not scared, but I know that troubles are near, because vector algebra can cause me headache. What I usualy need is a basic cookbook with most basic user recipes, "How to do it," and that is what I want to offer here.

The most basic space is simple: one origin (point 0, 0, 0), one x-axis (vector 1, 0, 0), y-axis and z-axis. But whenever we place an object in such space, we are not necessarily aware that we also created a subspace - the object space. From this moment, we can place other objects not only in the main space but also in this subspace, using the method addChild, or addParent. Each space is defined by its coordination system, for short **CS**:

Object3D cube = Primitives.getCube(1f); world.addObject(cube); Object3D sphere = Primitives.getSphere(1f); world.addObject(sphere); cube.addChild(sphere); cube.translate(0f,5f,0f); sphere.translate(5f,0f,0f);

The cube in this example is defined in the main CS translated by 5 in the Y direction, but because the sphere is the cubes child, it is defined in the cubes CS, so it is moved by 5 in X direction, but not from the origin of the main CS, but from the origin of the cubes CS.

Similar examples can be found also for rotation and scale and when all those features combine, it can be hard to track them.

## Scale

Probably the most simple transformation is scale. This transformation changes size of the object from the selected point called **pivot**. There are three catches using scale:

1) Each object has its own base size coming from the objects mesh.

Solution: The base mesh size can be found, look at the help of the method Mesh.getBoundingBox()

float[] bbox = object.getMesh().getBoundingBox();

2) The objects pivot is calculated point which means that the basic scale is not well predictable

Solution: The pivot is calculated during the Object3D.build() command. After this line, the pivot can be read and edited

object.build(); SimpleVector pivot = object.getRotationPivot(); object.setRotationPivot(newPivot);

3) The object is also scaled by all the scales of its parents, so if the size of the mesh is 1 and the objects scale is 1 that does not mean that the size will be 1, because the object can have a parent with a scale not-equal 1.

Solution: Calculate the size from the scale of all objects parents.

public static SimpleVector getSize(Object3D object){ float[] bbox = object.getMesh().getBoundingBox(); float s = object.getScale(); Object3D[] par; par = object.getParents(); while(par.length>0){ s = s*par[0].getScale(); par = par[0].getParents(); } SimpleVector out = new SimpleVector((bbox[1]-bbox[0])*s,(bbox[3]-bbox[2])*s,(bbox[5]-bbox[4])*s); return out; }

## Rotate

There is no problem with rotation, as long as the object or its parent is translated or scaled, the achieved rotation is always the same. But if you start to mess around with rotation of the parent or setting up the pivot point, the result can be suprising. Whenever some objects ancestor is rotated, its CSs is rotated as well so the final rotation of our object is done by different axis than you may think. Whenever the objects or its ancestors pivot is translated, the final orientation will be the same, but the placement of the rotated object may be totally off.

red.addParent(blue); green.setRotationPivot(new SimpleVector(-5f,0f,0f)); blue.translate(0f,5f,0f); ... green.translate(5f,0f,0f); green.rotateZ((float)(Math.PI)); // The same way also the yellow and the red

On the example, the yellow, the green and the red cube is translated by the same vector 5,0,0 and rotated by the same angle rotateZ(PI), but:

1) The red cube is in the CS of the blue cube

2) The green cube has translated pivot by vector -5,0,0

## From CS to CS

One big problem can be when you need to translate an object, which is already not in the original CS. If you have the new coordination in the original CS, but you need to translate the object in the CS in which it is placed.

I solved such a problem with following method. This method takes a vector toRecalc and calculates the vector I have to translate the object forObject to get it to the toRecalc point.

public static void recalcVector(SimpleVector toRecalc, Object3D forObject){ toRecalc.sub(forObject.getWorldTransformation().getTranslation()); Object3D[] p = currObj.getParents(); while(p.length>0){ toRecalc.matMul(p[0].getRotationMatrix().invert()); p = p[0].getParents(); } }

Problem: In this example I don't count with scale, I expect that the scale of parent can influence the translation of child, so if the scale should be count in, I would have to scalarMul() the vector toRecalc by 1/totalScale of all the parents of the forObject. I first used the method:

toRecalc.sub(forObject.getWorldTransformation().getTranslation());

This way I have the vector I want. The only problem is that the vector needs to be rotated, because it is still in the original CS. That is why I used the second method:

toRecalc.matMul(p[0].getRotationMatrix().invert());

This way I rotated the vector using inverted Rotation matrix of the object p[0].