Search code examples
javaswingcanvasgame-loop

X and Y as float instead of integers


I decided to start working on a project lately, a small 2D game. I already have some gravity working. I'm starting to get better at java but I had never used the java AWT or anything related to rendering prior to this project and basically here's my problem:

I got a character who's walking at 1 pixel per tick, which is too slow. If I up it to just 2 tick, it's too fast and the character is almost sort of running. I'd like kind of in-between but the problem is that X and Y are stored as int, and if I changed them to float, i would have to convert them to an int with something like (int) which will result in removing the floating numbers.

What I have tried is to update the character's X every 2 ticks instead of 1 tick or making the gameloop go slower but it looks rather choppy instead. If anyone has a good solution to that, please leave a message. Thanks!


Solution

  • Split the View from the Model (and look up MVC pattern!)

    Use floating point coordinates in your model, convert them to integer pixels only in the visual display.