2
\$\begingroup\$

I'm currently trying to create a camera and entity/model matrix for my 2D game similar to that of Unity3D. I've already tried to find answers to this question on stackoverflow/gamedev but i couldn't find an answer that explains how to center the camera and image.

Goal:

  1. Have a camera matrix wich is centered at the position of the camera.
  2. Have a entity matrix wich draws everything centered

Current implementation:

camera: matrix = Matrix.translation(Screen.width * 0.5, Screen.height * 0.5) *
                 Matrix.rotation(camera.rotation_radians) *
                 Matrix.scale(camera.scale.x, camera.scale.y) *
                 Matrix.translation(camera.x, camera.y);

entity: matrix = Matrix.rotation(entity.rotation_radians) *
                 Matrix.scale(entity.scale.x, entity.scale.y) *
                 Matrix.translation(entity.x, entity.y);

image component draw function:
                 setMatrix(camera.matrix * entity.matrix);
                 drawImage(x=0, y=0, image) // position is already included in the entity matrix

Results:

  1. Image rotates around the upper-left corner of the screen not around it's center
  2. Image is not centered. The image's origin is at its upper-left corner.
  3. The camera matrix seems to be correct.

Questions:

  1. What is the correct multiplication order for the entity/model matrix?
  2. Can i use a single matrix for all components of a entity or do i need to calculate in the width/height of the image/text/animation component.
\$\endgroup\$
  • 2
    \$\begingroup\$ Remember SCROT - scale rotate translate. \$\endgroup\$ – Syntac_ Sep 14 '14 at 17:12
  • \$\begingroup\$ Also remember that if you use row-major matrices the multiplication order is from left-to-right and vice versa for column-major. \$\endgroup\$ – JarkkoL Sep 15 '14 at 23:20
  • \$\begingroup\$ Most camera implementations I used did the camera view matrix slightly different. It started with translating to (-camera.x, -camera.y), then rotate and scale (order of these 2 is not important) followed by translating to the screen center. You start with translating to (-camera.x, -camera.y) to translate everything from word coordinates to screen coordinates. \$\endgroup\$ – RdeGier Mar 15 '15 at 7:50
  • \$\begingroup\$ @JarkkoL what you say makes sense only if your matrix library has operators where left and right means anything. if it has functions, it is parameter placement. In both cases, the library will have a convention, most surely respecting mathematical order. Finally the way it is stored, is irrelevant. it only changes how scalars are fetched in these respective functions/operator. but it is transparent to the user. it only bleeds and starts to become a concern if you prepare raw data yourself to pass to the library's functions. \$\endgroup\$ – v.oddou May 14 '15 at 3:13
  • \$\begingroup\$ @v.oddou the matrix lib itself can be convention agnostic (if it doesn't expose things like where the translation is stored etc) but the engine utilizing the matrix lib has a convention, which isn't always mathematical one, i.e column major. I'm not talking about physical convention but logical though those should match or it's pain to debug. \$\endgroup\$ – JarkkoL May 15 '15 at 11:02
1
\$\begingroup\$

What is the correct multiplication order for the entity/model matrix?

Scale then rotate then translate.

Can i use a single matrix for all components of a entity or do i need to calculate in the width/height of the image/text/animation component.

No each component part of your scene has its own matrix as the matrix represents the combination of scale, rotation and position for that object.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.