GP32 Centering X & Y Axis To Sprite


solarice

Member
Joined
Jan 29, 2004
Messages
120
Location
UK
Website
Visit site
hi can someone tell me how you go about centering the x and y axis to a sprite, so that its set in the middle of the image instead of the bottom right at (0,0).

Thanks.
 
In actual code, should avoid /2 or dividing by any power of two. Divide is done through a SWI and takes around 100 cycles. The ARM chip has a barrel shifter, so if you use >> 1, (Left shift by 1), it takes NO extra instructions.

X_sprite=(X_size_screen-X_size_sprite)>>1;
Y_sprite=(Y_size_screen-Y_size_sprite)>>1;

However, now I look at it, that draws the sprite in the middle of the screen, which isn't what I think you want. I think you want it so that the when you plot at (x, y), the centre of the sprite is at (x,y), not the bottom left hand corner.
This is simply case of moving the sprite left and up by half it's width and height

Xnew=x - (sprite_width>>1);
Ynew=y - (sprite_height>>1);

You may need to change some of those positive and negatives round, can't remember the coordinate system on the gp32 at the moment...
 
MojoJojo, would the compiler not optimize that divide out since it is a constant. I've been always led to believe that you should leave them in for readability since compilers optimize out the constants anyway. ie. Dividing normally gets optimized to a multiply, dividing or multiplying by a power of 2 gets optimized to a shift.

I got told this by professional game developers, so I assume they are correct.
 
Back
Top