Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers AppGameKit Corner / Setting sprite transform properties by transform matrix

Author
Message
Jugival
1
Years of Service
User Offline
Joined: 28th Feb 2018
Location:
Posted: 14th Mar 2019 08:54
I use composed sprites where child sprites are transformed in the coordinate system of the parent sprite. Transformation properties are stored in matrices.
It seems AppGameKit has no command to directly set sprite transform matrices. Extracting properties like scale, rotation... from a matrix does not work in all situations.
How should I deal with this problem?
Using a custom vertex shader?
Or did I miss some other useful command in AppGameKit?
JugiMap - a map editor
fubarpk
Moderator
14
Years of Service
User Offline
Joined: 11th Jan 2005
Location: Adelaide
Posted: 14th Mar 2019 09:07
not sure what you are asking but there are the following commands
fubar
Bengismo
1
Years of Service
User Offline
Joined: 20th Nov 2017
Location: Yorkshire, England
Posted: 14th Mar 2019 09:30
Getting world coordinates relative to parent sprite can be done with GetWorldXFromSprite()/GetWorldYFromSprite()

So if the parent sprite is rotated or positioned anywhere then a child sprite can be positioned relative to its parents easily using these functions. Then its just set the sprites rotation value to be its own rotation plus that of its its parents. Same with the scaling factor too.

You could also use the built in skeleton system but that is really only useful if you have Spriter or some other 2d animation package and you want to manipulate bones directly.

There is no way to set the matrix for the sprite as this is done internally and already transformed vertices are sent to the sprite shader I thought.

A custom shader would fix it and allow relative movements by setting a transform matrix but all your sprites would be located at 0,0 and so collision functions wouldn't work correctly.

You could implement it all with quads and a shader and matrix transforms being set but as above....you would have to write your own collision routines.
fubarpk
Moderator
14
Years of Service
User Offline
Joined: 11th Jan 2005
Location: Adelaide
Posted: 14th Mar 2019 09:34
Thanks for that clarification @Bengismo
fubar
Jugival
1
Years of Service
User Offline
Joined: 28th Feb 2018
Location:
Posted: 14th Mar 2019 13:40
Thank you Bengismo!
It looks that command is exactly what I need.

JugiMap - a map editor
Bengismo
1
Years of Service
User Offline
Joined: 20th Nov 2017
Location: Yorkshire, England
Posted: 14th Mar 2019 13:44 Edited at: 14th Mar 2019 13:46
No problem

Had a think about this over lunch and I dont think its that its all that bad to just set the sprite properties from matrix values if you have them...

// lets say you have a 2d matrix for each sprite that you are somehow calculating yourself.
[a b c]
[d e f]

// set the position
SetSpritePositionByOffset(spr, c,f)

// Get the scale (if your not scaling then you can skip this step)
sx = sign(a)*sqrt(a*a + b*b)
sy = sign(d)*sqrt(d*d + e*e)
SetSpriteScale(spr,sx,sy)

// set the angle
Angle = atan2(-b,a) (or atan2 (d,e))
SetSpriteAngle(spr,Angle)

You could set up a function to do this easily for any sprite where you just give it a sprite ID and a matrix (An array in AGK) and it sets the position rotation and scale?

The method of using the world X values from the sprite is much easier though....id go with that.

Jugival
1
Years of Service
User Offline
Joined: 28th Feb 2018
Location:
Posted: 16th Mar 2019 21:06
This approach with directly extracting parameters does not always work correctly in my case, probably because I am also storing flips (as negative scale) into matrix.

At the end I got transformation values for child sprites in the following way :
- world position from parent matrix
- scale is a cumulative scale from the top parent sprite to the bottom child sprite in hierarchy
- flip is a cumulative flip where every flip "flips" the cumulated flip value
- rotation is also a cumulative value but with an important detail - if the parent sprite flip X or the flip Y is set (but not both), the rotation value of a child sprite changes sign




JugiMap - a map editor

Login to post a reply

Server time is: 2019-07-18 01:24:19
Your offset time is: 2019-07-18 01:24:19