Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (404)
games submitted by our members
Games in WIP (289)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [SOLVED] 2D Block Problems  (Read 1492 times)
0 Members and 1 Guest are viewing this topic.
Offline duce

Senior Newbie





« Posted 2011-12-03 03: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?
Offline gbeebe

Senior Member


Medals: 5
Projects: 1



« Reply #1 - Posted 2011-12-03 03: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.
Offline duce

Senior Newbie





« Reply #2 - Posted 2011-12-03 03: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!
Legends of Yore - The Casual Retro Roguelike
Offline gbeebe

Senior Member


Medals: 5
Projects: 1



« Reply #3 - Posted 2011-12-03 04: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.
Offline duce

Senior Newbie





« Reply #4 - Posted 2011-12-03 05:44:20 »

Nope. Doesn't work, still.  Sad
Offline gbeebe

Senior Member


Medals: 5
Projects: 1



« Reply #5 - Posted 2011-12-03 05: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;
Offline duce

Senior Newbie





« Reply #6 - Posted 2011-12-03 06:14:50 »

gameScale is the scale of everything. It works the same with it on or off.
Offline gbeebe

Senior Member


Medals: 5
Projects: 1



« Reply #7 - Posted 2011-12-03 06: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.
Offline duce

Senior Newbie





« Reply #8 - Posted 2011-12-03 07:03:11 »

I can't figure anything out from this data.
Offline gbeebe

Senior Member


Medals: 5
Projects: 1



« Reply #9 - Posted 2011-12-04 01: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!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 264
Projects: 2


I'm the King!


« Reply #10 - Posted 2011-12-04 03:56:11 »

Yes you divide by scale.

Offline gbeebe

Senior Member


Medals: 5
Projects: 1



« Reply #11 - Posted 2011-12-04 06: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.
Offline duce

Senior Newbie





« Reply #12 - Posted 2011-12-04 07:48:45 »

I can not figure out this problem. I've hit a wall, I don't know what to do.
Offline theagentd
« Reply #13 - Posted 2011-12-04 08: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.
Offline duce

Senior Newbie





« Reply #14 - Posted 2011-12-04 12: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
Offline theagentd
« Reply #15 - Posted 2011-12-04 12: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.
Offline duce

Senior Newbie





« Reply #16 - Posted 2011-12-04 20:13:41 »

Cheesy 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!
Offline theagentd
« Reply #17 - Posted 2011-12-04 20: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.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Get high quality music tracks for your game!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (34 views)
2013-05-17 21:29:12

alaslipknot (42 views)
2013-05-16 21:24:48

gouessej (72 views)
2013-05-16 00:53:38

gouessej (71 views)
2013-05-16 00:17:58

theagentd (79 views)
2013-05-15 15:01:13

theagentd (74 views)
2013-05-15 15:00:54

StreetDoggy (116 views)
2013-05-14 15:56:26

kutucuk (139 views)
2013-05-12 17:10:36

kutucuk (140 views)
2013-05-12 15:36:09

UnluckyDevil (147 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.159 seconds with 21 queries.