0

I only superficially understand the idea and I can't find any thorough explanation (partially because I can't find very good keywords to Google - English is not my first language):

Although I partially understand some things (next paragraph), my question is what data structure would you use to store a body's position and rotation and how does everything come together? I want to be able to translate that object to a (x, y) position, rotate it about the origin or about its centre (then fetch these things and draw a sprite using that position and rotation). Very similar to Unity's Transform object.

From my understanding I will need (at least) a 2/3-dimensional vector and a quaternion and then I should be able to generate a model matrix from them. How? Multiply what to what and in what order? I certainly have no idea how to get a 4 x 4 matrix from a 2/3-dimensional vector and a quaternion. Then if I change the position, I should probably update the model matrix again.

I'm using OpenGL ES 2 on Android, but I wouldn't mind an agnostic answer.

async
  • 745
  • 1
  • 7
  • 23

1 Answers1

2

I use directx but it should be the same. There are some functions that create a matrix from a vector. You can search for "transformation matrix". ex. a 2d translation matrix:

[1 0 x]
[0 1 y]
[0 0 1]

a 2d scaling matrix: [sx 0 0] [0 sy 0] [0 0 1]

to obtain the final world matrix you have to multiply them, but be careful the order is important ( matrices haven't commutative property) so: if you do -scale*position= the model is scaled about its centre and then translated -position* scale = the model is first translated and then scaled (it will move to the origin) -position* rotation = the model will rotate around the origin and not around its centre

usually you need world = rotation*scale*position.

In directx there is a function for each transformation: XMMatrixTranslation(x,y,z) XMMatrixRotationZ(r) .... each of them returns a matrix.

I think in opengl there are function like these so you dont need vector to stroe value, of course you can but you can use: float px,py,pz; float sx,sy,sz; float rx,ry,rz; or three arrays and then call translationMatrix=XMMAtrixTranslation(px,py,pz) or ( XMMatrixTranslation(position[0],position[1],position[2])

Liuka
  • 585
  • 2
  • 4
  • 19