Skip to main content
deleted 24 characters in body; edited title
Source Link
user1430
user1430

Correct What is the correct multiplication order for a 2D Matrixmatrix?

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.

Thanks in advance :)

Correct multiplication order for a 2D Matrix

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.

Thanks in advance :)

What is the correct multiplication order for a 2D matrix?

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.
Tweeted twitter.com/#!/StackGameDev/status/511210945847099392
added 53 characters in body
Source Link
Marco
  • 21
  • 2

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.

Thanks in advance :)

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)

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.

Thanks in advance :)

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.

Thanks in advance :)

Source Link
Marco
  • 21
  • 2

Correct multiplication order for a 2D Matrix

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)

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.

Thanks in advance :)