Sign in to follow this  

Centering an image and other formulas

This topic is 3505 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

What is the formula for centering an image? Or in other words, what is the formula to get the right x,y values to center an image (in pixels)? What are some other useful 2d formulas?

Share this post


Link to post
Share on other sites
Some questions... where is (0, 0) on the image, bottom-left, top-left, etc?
Do you have the width and height of the texture?

If so, just think about it, maybe draw a little picture on paper, that definitely helps visualize it.

Share this post


Link to post
Share on other sites
Of course 0,0 is the top most left. I tried something like this which I believe is the formula:

rect.x = SCREEN_WIDTH / 2;
rect.y = SCREEN_HEIGHT / 2;

However, this does not center the image...

Share this post


Link to post
Share on other sites
this?
rect.x = SCREEN_WIDTH / 2 - (rect.width / 2);
rect.y = SCREEN_HEIGHT / 2 - (rect.height / 2);

EDIT: nvm, you got it and someone posted before me lol. You just didn't take into account the width and height of the image.

Share this post


Link to post
Share on other sites

This topic is 3505 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this