duce
Senior Newbie 
|
 |
«
Posted
2011-12-03 02:14:02 » |
|
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?
|
|
|
|
gbeebe
|
 |
«
Reply #1 - Posted
2011-12-03 02:19:32 » |
|
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.
|
|
|
|
duce
Senior Newbie 
|
 |
«
Reply #2 - Posted
2011-12-03 02:23:29 » |
|
1 2
| 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.
|
|
|
|
Games published by our own members! Check 'em out!
|
|
gbeebe
|
 |
«
Reply #3 - Posted
2011-12-03 03:02:52 » |
|
I think the "f" at the end there is negating the (int). Check this out: 1
| System.out.println((int)17/16); |
displays 1 1
| System.out.println((int)17/16f); |
displays 1.0625 I think this is why you're given the wrong tile.
|
|
|
|
duce
Senior Newbie 
|
 |
«
Reply #4 - Posted
2011-12-03 04:44:20 » |
|
Nope. Doesn't work, still. 
|
|
|
|
gbeebe
|
 |
«
Reply #5 - Posted
2011-12-03 04:57:12 » |
|
what is gameScale? Does the problem get worse the further you go from (0, 0)? does this work? 1 2
| int blockX = (int)(x + camera.getLocation().x) / gameScale / 16; int blockY = (int)(y + camera.getLocation().y) / gameScale / 16; |
|
|
|
|
duce
Senior Newbie 
|
 |
«
Reply #6 - Posted
2011-12-03 05:14:50 » |
|
gameScale is the scale of everything. It works the same with it on or off.
|
|
|
|
gbeebe
|
 |
«
Reply #7 - Posted
2011-12-03 05:36:33 » |
|
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.
|
|
|
|
duce
Senior Newbie 
|
 |
«
Reply #8 - Posted
2011-12-03 06:03:11 » |
|
I can't figure anything out from this data.
|
|
|
|
gbeebe
|
 |
«
Reply #9 - Posted
2011-12-04 00:03:43 » |
|
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?
|
|
|
|
Games published by our own members! Check 'em out!
|
|
ra4king
|
 |
«
Reply #10 - Posted
2011-12-04 02:56:11 » |
|
Yes you divide by scale.
|
|
|
|
gbeebe
|
 |
«
Reply #11 - Posted
2011-12-04 05:01:24 » |
|
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.
|
|
|
|
duce
Senior Newbie 
|
 |
«
Reply #12 - Posted
2011-12-04 06:48:45 » |
|
I can not figure out this problem. I've hit a wall, I don't know what to do.
|
|
|
|
theagentd
|
 |
«
Reply #13 - Posted
2011-12-04 07:18:45 » |
|
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! =)
|
Myomyomyo.
|
|
|
duce
Senior Newbie 
|
 |
«
Reply #14 - Posted
2011-12-04 11:24:44 » |
|
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. http://pastebin.com/TRaUXDbg
|
|
|
|
theagentd
|
 |
«
Reply #15 - Posted
2011-12-04 11:41:50 » |
|
You put the floor wrong. Call a carpenter. 1 2
| 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: 1 2
| int blockX = Math.floor((camera.getLocation().x + x / gameScale) / 16f); int blockY = Math.floor((camera.getLocation().y + y / gameScale) / 16f); |
|
Myomyomyo.
|
|
|
duce
Senior Newbie 
|
 |
«
Reply #16 - Posted
2011-12-04 19:13:41 » |
|
 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!
|
|
|
|
theagentd
|
 |
«
Reply #17 - Posted
2011-12-04 19:49:08 » |
|
I've cried blood myself trying to calculate a bounding box for some 2D lights, so I should have helped you earlier... xp
|
Myomyomyo.
|
|
|
|