Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  [SOLVED] 2D Block Problems  (Read 1021 times)
0 Members and 2 Guests are viewing this topic.
Offline duce

JGO n00b
*

Posts: 47



« on: 2011-12-02 21: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

Full Member
**

Posts: 145
Medals: 5



« Reply #1 on: 2011-12-02 21: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

JGO n00b
*

Posts: 47



« Reply #2 on: 2011-12-02 21: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! Go get 'em!
Offline gbeebe

Full Member
**

Posts: 145
Medals: 5



« Reply #3 on: 2011-12-02 22: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

JGO n00b
*

Posts: 47



« Reply #4 on: 2011-12-02 23:44:20 »

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

Full Member
**

Posts: 145
Medals: 5



« Reply #5 on: 2011-12-02 23: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

JGO n00b
*

Posts: 47



« Reply #6 on: 2011-12-03 00:14:50 »

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

Full Member
**

Posts: 145
Medals: 5



« Reply #7 on: 2011-12-03 00: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

JGO n00b
*

Posts: 47



« Reply #8 on: 2011-12-03 01:03:11 »

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

Full Member
**

Posts: 145
Medals: 5



« Reply #9 on: 2011-12-03 19: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! Go get 'em!
Offline ra4king

JGO Kernel
*****

Posts: 3131
Medals: 195


I'm the King!


« Reply #10 on: 2011-12-03 21:56:11 »

Yes you divide by scale.

Offline gbeebe

Full Member
**

Posts: 145
Medals: 5



« Reply #11 on: 2011-12-04 00: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

JGO n00b
*

Posts: 47



« Reply #12 on: 2011-12-04 01:48:45 »

I can not figure out this problem. I've hit a wall, I don't know what to do.
Offline theagentd

JGO Wizard
****

Posts: 1384
Medals: 88



« Reply #13 on: 2011-12-04 02: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! =)

There is no god.
Offline duce

JGO n00b
*

Posts: 47



« Reply #14 on: 2011-12-04 06: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

JGO Wizard
****

Posts: 1384
Medals: 88



« Reply #15 on: 2011-12-04 06: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);

There is no god.
Offline duce

JGO n00b
*

Posts: 47



« Reply #16 on: 2011-12-04 14: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

JGO Wizard
****

Posts: 1384
Medals: 88



« Reply #17 on: 2011-12-04 14: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

There is no god.
Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.12 seconds with 19 queries.