Sprite Origin positions |
Sprite Origin positions |
Sanette
Guest
|
a nice geometry exercice. Here is what I found (only rotation, without scaling)
assume that the top left corner of your initial rect is (x0,y0). I call (x0+cx,y0+cy) the position of the center of rotation. Here is the formula for the top-left position of the rect containing the rotated sprite: x1 = x0 + cx - cx*cosp - cy*sinp + min(cosp,0)*w + min(sinp,0)*h y1 = y0 + cy + cx*sinp - cy*cosp + min(cosp,0)*h - max(sinp,0)*w note that the angle is computed counterclock-wise (standard math convention) cosp=cos(angle) sinp=sin(angle) For the scaling, I cannot answer right now, because I need to know if you apply the scaling before rotation or after rotation (if scalex <> scaley, this makes a difference) S. Le 19/09/2014 01:09, MrTAToad a écrit :
|
|||||||||||||||
|
MrTAToad
|
I presume SDL2 does scaling first (or rather it acts on a defined size) and then rotation.
|
|||||||||||
|
Sprite Origin positions |
Sanette
Guest
|
I made some quick tests and it seems that SDL_RenderCopyEx does all the calculations for you:
if you rotate a texture around some point p, then the rotated image is displayed the way you want, that is, the point p has not moved. But, if you also scale the image by providing a dstRect, then SDL_RenderCopyEx does not scale p for you; it first scales the texture, and then rotates around the point given by the original coordinates of p. Therefore, the only thing you have to do, is simply to scale p before rotating. Of course, since you want that the image to be stretched around p you need to make the following transforms: srcRect = (x,y,w,h), center=(cx,cy) ===> dstRect = (x+cx*(1-scalex), y+cy*(1-scaley), w*cx, h*cy) and THEN, apply SDL_RenderCopyEx It should do exactly what you want Le 19/09/2014 11:48, MrTAToad a écrit :
|
|||||||||||||||
|
Sprite Origin positions |
Sanette
Guest
|
Le 19/09/2014 17:33, Sanette a écrit :
|
|||||||||||||||||||||
|
Sprite Origin positions |
arosian
|
Hi,
Look in to using SDL_RenderCopyEx. One of the parameters is described as such: " a pointer to a point indicating the point around which dstrect will be rotated (if NULL, rotation will be done aroud dstrect.w/2, dstrect.h/2)" On Sep 18, 2014 7:09 PM, "MrTAToad" wrote:
|
|||||||||||||
|
Sprite Origin positions |
Sanette
Guest
|
Le 19/09/2014 17:38, Sanette a écrit :
now it works (I have tested it) ! |
|||||||||||||||||||||
|
Sprite Origin positions |
Jonny D
|
Just to put it out there... Â SDL_gpu blits images at their center points by default, so positioning with scaling and rotation works without any extra effort. Â When a pivot point is used (GPU_BlitTransformX()), then the image is rendered relative to that point (i.e. you can use 0,0 as the hotspot, which is what SDL does normally).
Jonny D |
|||||||||||
|
Re: Sprite Origin positions |
MrTAToad
|
Dont forget that is the point for which rotation is performed around, not the origin for plotting the sprite.
|
|||||||||||||||
|
Re: Sprite Origin positions |
MrTAToad
|
Having a slight problem with it at the moment. The following is rotating and scaling around an origin (100,100). Unfortunately the coordinates aren't resulting in the centre of the sprite being there :
[img]https://onedrive.live.com/redir?resid=DD5B5E7788984419!6230&authkey=!AKX9BHox8k0YKoA&v=3&ithint=photo%2cpng[/img]
|
|||||||||||||||||||||||||
|
MrTAToad
|
I think I may have solved it with a quick change of the calculations to :
Which produces a correct position of : [img]https://onedrive.live.com/redir?resid=DD5B5E7788984419!6231&authkey=!AOKt2b7arja3N4o&v=3&ithint=photo%2cpng[/img] I just need to check now to make sure that positions are correct when the origin is the top-left and bottom-right of a sprite now, but initial tests look fine... |
|||||||||||||
|
MrTAToad
|
My change did work fine with all 9 possible origin positions.
My rendering code is now thus :
|
|||||||||||||
|