Follow the withe rabbit ... all the secret of the matrix here

I’ll work on disentangling that… :slight_smile:

.@aciolino I think what we want is a simple way to combine rotations, translations, scaling, by using as much as possible the functions already available in Codea? If so then try this code:

-- 0 matrix

-- Use this function to perform your initial setup
function setup()
    print("---- test 1 ------")
    print("start with identity")
    local m0 = matrix(1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,0 )
    print(m0)
    
    print("translate(1,2,3)")
    m = m0:translate(1,2,3)
    print(m)
    
    print("rotate(90,0,1,0)")
    m = m:rotate(90,0,1,0)
    print(m)
    
    print("scale(1,1,2)")  
    m = m:scale(1,1,2)
    print(m)
    
    print("translate(-1,-1,-1)")     
    m = m:translate(-1,-1,-1)
    print(m)
    
    print("rotate(-90,0,1,0)")     
    m = m:rotate(-90,0,1,0)
    print(m)
    
    print"-----  test 2  --------"
    print("start with identity")
    local m0 = matrix(1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,0 )
    print(m0)
    
    print("matTranslate(m0,1,0,0)")
    m = matTranslate(m0,1,0,0)
    print(m)
    
    print("rotate(90,0,1,0)")
    m = m:rotate(90,0,1,0)
    print(m)
    
    print("scale(1,1,2)")  
    m = m:scale(1,1,2)
    print(m)
    
    print("matTranslate(m,-1,-1,-1)")     
    m = matTranslate(m,-1,-1,-1)
    print(m)
    
    print("rotate(-90,0,1,0)")     
    m = m:rotate(-90,0,1,0)
    print(m)
end

function matTranslate(m,x,y,z)
    local m1 = matrix(x,x,x,0, y,y,y,0, z,z,z,0, 0,0,0,0)
    m1 = m1 + m
    return m1
end

function draw()
end

Test 1 shows you (and me) what really happens: the rotation and scalings seems to work in complete independance with translations.

So if we want to combine both we have to force the translation in the top part. This is what my function matTranslate does. If you look carefully at test 2 results, first column of the matrix, you’ll see that now the first column rotate, scales AND translates correctly (ie in combination with rotations and scalings). And you don’t have to reverse the order of operations… So i will use:

Put the vector x,y,z in m[1],m[5],m[9]: m[1],m[5],m[9] = x,y,z

Apply rotate, scale and matTranslation

Read back the values m[1],m[5],m[9] into x,y,z: x,y,z = m[1],m[5],m[9]

You could also use the other 2 columns to calculate 2 more vectors in parallel.

@andrew_stacey i don’t know if this is the ‘best’ way to achieve the desired result. I have not been able from your explanations to find out where to put x,y,z in the matrix to get the above result: my attemps all failed, i probably missed a crutial point somewhere. If you know a way to work with the codea translate function in combination with rotations and scalings, simply tell exactly where to write x,y,z in the matrix and where to read the results. Thanks!

At this stage, I’m going back to basics - the matrix maths are helpful if you are a person whi really understands them, and at this point, I am not. So, heading back to macro-land, I’m working without matrices to get my desired result. I’m not 00% there yet, but almost.

What I want to do it take an independent mesh, have it rotate around it’s center point, and move off on a vector. So far, I’ve gotten the mesh to rotate around it’s 0-coordinate, so I have to do the translate() that was mentioned earlier before the rotate and the translate back after to get the rotation. After that; it’s a simple translate to more the object to it’s intended destination.

What bugs me is that I originally thought that I should moove the vertices on the object to move it quickly - I eralize now that idea was bad, becuase I was moving my object from IT’S OWN CENTER POINT OF REFERENCE. Bad idea, as rotate was really skewed.

Currently I am trying to get the center point of my mesh, which I’m sure is simple math, but I don’t want to have to rip through all vertices to find it :frowning:

Moving the vertices on the object is not a good idea. Then they have to be read back to the GPU every cycle. Changing the model matrix is a much faster way to achieve the same end.

As for the centre of your mesh, it is where you want it to be. There isn’t a definitive notion of “centre” of a generic shape.

The simplest method would be to define your triangles so that their natural origin is the rotation point and then you only have to do the rotation followed by translation (which you put the other way around in your code).

I’ve managed to solve the issue, though I don’t know how I got it to work.
I ended up doing two translates of the mesh in question after findiong the center point, but both translates were in the negative direction.

Pseudocode:

local x,y,z = GetCenter(thisMesh)
translate(-x,-y,-z)
translate(locx, locy, locz)
rotate(rot,1,1,1)   ---Sir Spin-a-lot
translate(-x,-y, z)
scale(.99)  --Sire Decay-a-little
thisMesh:draw()

So…I am no longer coding matrices by hand. Boo hoo.