About Monkey 2 › Forums › Monkey 2 Programming Help › Matrix scale
Tagged: AffineMat3, Matrix
This topic contains 3 replies, has 3 voices, and was last updated by 
 Ethernaut
 2 years, 4 months ago.
- 
		AuthorPosts
 - 
		
			
				
December 3, 2016 at 8:32 am #5556
Maybe this is more of a math question than a M2 question: Is there a way to obtain the current scale factor from an existing AffineMat3?
I know I can scale it using the Scale method, but I was wondering if that value can be extracted back without storing it in separate variables. It would just be a lot simpler in my case to pass a single matrix as an argument, as opposed to storing the current scale separately, then passing it as arguments to a different method in a different class.
Thanks!
December 3, 2016 at 9:43 am #5558TLDR; I keep rotation and position in a couple of vectors – its not a waste to use an extra one for scale….
its the diagonal values… (NB code isn’t using mx2’s affine matrix but my own version)
for example if I want to create a scale matrix (to multiply with a matrix I want to scale)
[/crayon]Monkey123456[crayon-5cb9d02d5d7ca984210464 inline="true" ] Method scale(x:Float,y:Float,z:Float)identity()mat[0] = xmat[5] = ymat[10] = zEndhowever once you then rotate a matrix etc, these values are going to get lost in the aggregation
for example once you multiply it with a rotation matrix (which you could make like this)
[/crayon]Monkey123456789101112131415161718192021222324[crayon-5cb9d02d5d7cf956302483 inline="true" ] Method rotationX(rad:Float)Local s:Float = Sin(rad)Local c:Float = Cos(rad)mat[ 0] = 1.0mat[ 1] = 0.0mat[ 2] = 0.0mat[ 3] = 0.0mat[ 4] = 0.0mat[ 5] = cmat[ 6] = smat[ 7] = 0.0mat[ 8] = 0.0mat[ 9] = -smat[10] = cmat[11] = 0.0mat[12] = 0.0mat[13] = 0.0mat[14] = 0.0mat[15] = 1.0Endnotice element 5 and 10….
In general what I have tended to do is use a position vector and quat rotation – stored in a 4 element vector… (if you need scale use another vector to store scale – why not its not a great waste!)
It used to be fashionable to say you should never store position and rotation ONLY in a matrix, if you only ever needed to add (or subtract!) incremental values from position and rotation, it has worked fine for me in the past – just bare in mind while you can easily extract position from a matrix BUT there are multiple axial rotations (and orders of rotation) that can give the same orientation.
December 4, 2016 at 6:10 am #5575Maybe this is more of a math question than a M2 question: Is there a way to obtain the current scale factor from an existing AffineMat3?
I have the following AffineMat3 Extension for my own use:
[/crayon]Monkey12345678910111213141516171819202122232425262728293031323334[crayon-5cb9d02d621ae710225591 inline="true" ]Struct AffineMat3<T> ExtensionMethod GetScale:Vec2f()Local sxsq:=Pow(Self.i.x,2)+Pow(Self.i.y,2)Local sysq:=Pow(Self.j.x,2)+Pow(Self.j.y,2)If sxsq=sysq 'to gain calculation time because I generaly use 1:1 scalesLocal sc:=Sqrt(sxsq)Return New Vec2f (sc,sc)ElseReturn New Vec2f (Sqrt(sxsq),Sqrt(sysq))EndifEndMethod GetRotation:Float()Local f:Float=Self.j.y 'to prevent integer division giving integerIf f<>0Return ATan(-Self.i.y/f)ElsePrint "Matrix is not a valid transform, could not get Rotation" 'should throw instead of print ..?Return 0.0EndifEndMethod GetTranslation:Vec2<T>()Return Self.t ' a bit basic but it makes all three methods being a logical groupEndEndIt’s Based on this answer: http://stackoverflow.com/questions/4361242/extract-rotation-scale-values-from-2d-transformation-matrix
But I had to change the rotation sign for Axis convention reasons I suppose.
If you use only 1:1 scales you will have a consistent GetScale() value whatever transforms order you make. If not then the length of the scale will be consistent but you won’t be able to find your original scale if you made a rotation in the “wrong” order..
December 4, 2016 at 7:57 am #5579Thanks for the replies, that is very helpful!
codifies, I definitely keep a vector for each entities’ local scale, but I ran into the need to “extract” the scale when I’m dealing with hierarchies inheriting position, rotation and scale. The way I’m doing it, each entity has a matrix that expresses the transformation for all entities in the hierarchy up to that entity, so extracting the scale from that already existing matrix could be cleaner than searching Up the hierarchy, multiplying each local scale.
The other case where I wanted it was to obtain the current scale from Mojo’s default window canvas. This is a situation where the scale is not set in my code (depends on window size and virtual resolution, etc.), so I couldn’t store it in a vector without obtaining it first.
Thanks!
 - 
		AuthorPosts
 
You must be logged in to reply to this topic.