I have two problems with the game I’m making. I have a 2D block sandbox game, like Terraria. When I move the camera around, I can still click the blocks to break them, but the blocks are sometimes off when I click. For example, when clicking on one block, it will destroy the one next to it. I believe this had to do with the camera’s position being a float, and the positioning being thrown off by this. How can I fix it?
Does it matter how close you are to the other tile, when you click? Like, does clicking dead center always work right, but clicking towards the edge always destroy the wrong tile? Maybe you should post the code that you use to determine which tile to destroy.
int blockX = x / (int) gameScale / 16 + (int) (camera.getLocation().x / 16f);
int blockY = y / (int) gameScale / 16 + 1 + (int) (camera.getLocation().y / 16f);
I tried rounding the camera / 16 values, but it doesn’t help much.
I think the “f” at the end there is negating the (int). Check this out:
System.out.println((int)17/16);
displays 1
System.out.println((int)17/16f);
displays 1.0625
I think this is why you’re given the wrong tile.
Nope. Doesn’t work, still.
what is gameScale? Does the problem get worse the further you go from (0, 0)?
does this work?
int blockX = (int)(x + camera.getLocation().x) / gameScale / 16;
int blockY = (int)(y + camera.getLocation().y) / gameScale / 16;
gameScale is the scale of everything. It works the same with it on or off.
I would output the mouse and camera coordinates along with the tile that you come up with, maybe when you get the glitch again it’ll help you point out the problem.
I can’t figure anything out from this data.
In my game I can change the scale too by stretching it to the width and height of the applet. I don’t divide my calculations by game scale, I multiply it. Maybe that’s what’s wrong?
Yes you divide by scale.
No, I don’t. Is that a Slick2D thing or something? If the screen is twice as big as intended I multiply by 2. If it’s half I multiply by .5.
Anywho, I’m just trying to help this guy out. What is usually a remedial problem is seeming to have a quite difficult solution.
I can not figure out this problem. I’ve hit a wall, I don’t know what to do.
What does camera.getLocation() return? The center of the camera or the top left corner of the camera’s viewport?
If you also have negative coordinates, your “one off” problem might be because you’re casting to an int, which simply cuts of the decimal. That means it rounds down for positive values (0.5 -> 0, 50.1 -> 50), but UP for negative values (-0.5 -> 0, -50.1 -> -50). Try to use (int)Math.floor(floatValue) instead, which always rounds the value down (0.5 -> 0, -50.1 -> -51).
Please post your current code to calculate which tile is clicked! =)
I think using Math.floor helped, but it still has the problem. Here is my entire code.
x and y are the mouse co-ordinates.
camera.getLocation() returns a 2D vector with the x and y offset of the camera (distance from 0, 0).
blockX and blockY are which block is to be deleted.
You put the floor wrong. Call a carpenter.
int blockX = x / (int) gameScale / 16 + (int) (Math.floor(camera.getLocation().x) / 16);
int blockY = y / (int) gameScale / 16 + 1 + (int) (Math.floor(camera.getLocation().y) / 16);
Try this:
int blockX = Math.floor((camera.getLocation().x + x / gameScale) / 16f);
int blockY = Math.floor((camera.getLocation().y + y / gameScale) / 16f);
Thanks so much! This works perfectly! For some reason I have to add +1 on to the end of the y, but it still works!
I’ve cried blood myself trying to calculate a bounding box for some 2D lights, so I should have helped you earlier… xp