Sign in to follow this  
armond

Coordinates as int or float?

Recommended Posts

armond    168
In my previous game which was a side scrolling shooting game, I made all my coordinates as integers. This game me problems with regards to precision and the way my objbects moved, was way too fast. Coming from a Java background, I made accessor and mutators for my class members like x and y which I later on just made public and sometimes, access is encapsulated through a move(x, y) method. I just want to ask, would it be a better idea to make my coordinates float? And would it be better to default access to coordinates to public? Thanks!

Share this post


Link to post
Share on other sites
Simian Man    1022
Quote:
Original post by armond
I just want to ask, would it be a better idea to make my coordinates float? And would it be better to default access to coordinates to public? Thanks!


The data type of your coordinates depends on the type of game you're making. For example, chess would obviously need integral coordinates. If you are going for discrete movement, however, you are right in thinking that floats would be better.

If by "default access to coordinates to public" you mean have public member variables, I would say no. Generally you should not allow random code to assign values to member variables. What if, while testing, the coordinates are being set out of range? If you make accessor functions, you can perform the bounds checking in one place rather than have to hunt through all of your code to find where they are being given illegal values.

Share this post


Link to post
Share on other sites
dave    2187
The type you use to represent the position of something is entirely dependant on the unit. If you are storing the position of something in pixels in screen space then use integers, if in screen space as a percentage then use floats. In 3D it will be floats, typically.

Also, there is no reason to leave the coordinates public. Use accessors.

Share this post


Link to post
Share on other sites

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