gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Coding > How to attach sprites to scaled background

#3271 - blindfold - Fri Feb 21, 2003 3:04 pm

Hi.
I have a little problem.

I have a scaled background and I placed a sprites on it. Now I zoom sprites and background (same factor), but I want a sprite to keep it's position according to background.

I tried to calculate percentage of a orginal sprite position, but it just don't work. I know that it is also a math problem, but if someone had a similar problem.......just help me...

Thanks.

PS. no rotation is made, so it's easier.

#3279 - CoolMan - Fri Feb 21, 2003 5:31 pm

Perhaps if you are converting the sprite's position from map coordinates, then you can scale the coordinate the same way as the BG. Might be too slow tho...
_________________
Moron! You don't herd chickens with a shotgun!

--CoolMan

#3280 - DekuTree64 - Fri Feb 21, 2003 5:39 pm

Subtract the BG center position, multiply by the scale factor, and add the center pos back. Basically you make 0,0 the center while you do your actual calculations. You can also multiply by sin/cos values (x*cos, y*sin, if I remember correctly. Haven't used that in ages) for rotation before you add your center pos back.

#3320 - blindfold - Sat Feb 22, 2003 8:18 pm

I have tried it all, still doesn't work.
I will explain my problem again, but now I will make it very simple...

I have a scaled background (no need to rotate).
Now at position 16, 16 I have a sprite.
When the scale value is 0x100 (no zoom), sprite is exacly at 3,3 tile (3rd row, 3rd column).
Then I scale a background (let's say it's a 2x zoomed). So I want to have my sprite also at a zoomed position 32,32.
But how to calculate it for any zoom value?
Scale value isn't a percentage, or any other known to me value, therefore when I increase it, the screen is zooming out, not in....
I know it's so easy, although I'm so tired of it that I need help.

#3326 - blindfold - Sat Feb 22, 2003 9:09 pm

Well, as for now, I figured out that its a

u16 x= (16*256)/g_Zoom;

for Horizontal walue x, where 16 is a horisontal position
I told that was very easy, I hope It's all right...

#3358 - blindfold - Sun Feb 23, 2003 3:35 pm

Yessss, that was correct, thank a lot, now it works 100%, just in case anybody has a similar problem...


u16 x= ((16+16)*256)/g_Zoom - 32;
u16 y= ((40+16)*256)/g_Zoom - 32;


where:

x is a horizontal position (after zooming)
y is vertical position (after zooming)

sprite is at 16, 40 position before zoom

g_Zoom is a scale factor


the sprite is 32 width and height, so I have to substract a half of it

that's all folks...

#3363 - ampz - Sun Feb 23, 2003 4:58 pm

I'd say attaching sprites to mode7 backgrounds is a bit more tricky :-)

#3376 - CoolMan - Sun Feb 23, 2003 7:33 pm

ampz wrote:
I'd say attaching sprites to mode7 backgrounds is a bit more tricky :-)


I would hate to be the commercial coder assigned to that problem...
_________________
Moron! You don't herd chickens with a shotgun!

--CoolMan