View Full Version : Apply transformation to a Vector2

MarkomDances

04-09-2013, 12:48 PM

I need a method for my java game that transforms a 2D vector normal by a matrix.

I have basic knowledge about matrices.

Someone explained the method as following:

TransformNormal() used for direction, tangent vectors. This reserves the matrix.

In Math: retVect = srcVec x M.

Vector2.TransformNormal() only applies the Scale and Rotational parts of the Matrix to the vector.

I do not fully understand this.

tonyo_au

04-09-2013, 05:47 PM

Vector2.TransformNormal() only applies the Scale and Rotational parts of the Matrix to the vector.

A normal is a direction vector so it does not require a displacement, ie 2 parallel planes have the same normal.

I am not sure why you need normals in a 2d game. They are mostly used for lighing in 3D.

MarkomDances

04-10-2013, 01:22 AM

It is used in a pixel perfect collision method with support of rotated images.

Do you know the formula to apply a matrix's scale and rotational parts to a vector2?

Dan Bartlett

04-10-2013, 07:33 AM

See http://www.arcsynthesis.org/gltut/Illumination/Tut09%20Normal%20Transformation.html or http://www.lighthouse3d.com/tutorials/glsl-tutorial/the-normal-matrix/

If you are translating your points in 2D with a matrix as well as rotating them, you will need a 3x3 matrix, where the points you transform require an extra term w which equals 1:

[x,y,1] * M = [x', y', 1]

If you only have uniform scaling (or no scaling), then for vectors such as normals you can use the same matrix you used to transform the points but set the w term equal to 0 which will ignore the translation:

[nx, ny, 0] * M = [nx', ny', 0]

However, if you have non-uniform scaling then you need to use the inverse transpose to transform the normals:

[nx, ny, 0] * transpose(inverse(M)) = [nx', ny', 0]

MarkomDances

04-10-2013, 02:15 PM

Thank you for the help. Unfortunately I did not fully understand your formulas.

I have a 3x3 Matrix like this.

And I have a Vector2, holding x and y.

I want to apply the Scale and Rotational parts of the Matrix to the vector.

Dan Bartlett

04-10-2013, 05:42 PM

If you have no scaling (or only uniform scaling), then just multiply [x, y, 0] by the 3x3 matrix. If you have scaling, you will need to first invert the matrix, then transpose it, then multiple [x, y, 0] by it.

tonyo_au

04-10-2013, 05:53 PM

See if this helps

http://www.cs.trinity.edu/~jhowland/cs2322/2d/2d/

MarkomDances

04-11-2013, 06:46 AM

If you have no scaling (or only uniform scaling), then just multiply [x, y, 0] by the 3x3 matrix. If you have scaling, you will need to first invert the matrix, then transpose it, then multiple [x, y, 0] by it.

Thanks! I think I can solve this now.

Powered by vBulletin® Version 4.2.5 Copyright © 2018 vBulletin Solutions Inc. All rights reserved.