Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (579)
games submitted by our members
Games in WIP (500)
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 1765 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: 322
Projects: 2
Exp: 4 years


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.

 

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

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (37 views)
2014-04-15 18:08:23

BurntPizza (33 views)
2014-04-15 03:46:01

UprightPath (49 views)
2014-04-14 17:39:50

UprightPath (31 views)
2014-04-14 17:35:47

Porlus (48 views)
2014-04-14 15:48:38

tom_mai78101 (69 views)
2014-04-10 04:04:31

BurntPizza (129 views)
2014-04-08 23:06:04

tom_mai78101 (229 views)
2014-04-05 13:34:39

trollwarrior1 (193 views)
2014-04-04 12:06:45

CJLetsGame (200 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
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!