Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (117)
games submitted by our members
Games in WIP (563)
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  
  Getting Clicked Block  (Read 1191 times)
0 Members and 1 Guest are viewing this topic.
Offline duce

Senior Newbie





« Posted 2011-11-29 03:22:52 »

I'm making a game like Terraria, 2D sandbox. I'm stuck, I can't figure out how to find out what block a user hit. I have the mouse x and y, but the only way I can think of is to have a Rectangle for each block, then use a for loop to cycle through every block to see which rec contains the co-ordinates, but it seems so inefficient.
Offline loom_weaver

JGO Coder


Medals: 17



« Reply #1 - Posted 2011-11-29 03:30:03 »

I don't have a solution yet although I'm interested.

In my game I iterate over every block.  Inefficient for sure but it works for now...

I think the solution involves using an oct-tree which I imagine to be a 3D version of a binary tree.  If you recursively partition your world into 8 blocks then each comparison can eliminate a large number of the candidates.  For 2D you would recursively partition your world into 4 spaces.

But I haven't done it yet and so I'm all ears!
Offline gbeebe

Senior Member


Medals: 5
Projects: 1



« Reply #2 - Posted 2011-11-29 03:31:18 »

BlockX = (int)(MouseX / BlockWidth)
BlockY = (int)(MouseY / BlockHeight)

This is basically the same calculation I would use in other languages, added the (int) for Java's sake  Smiley
Please note that if the first tile is (1, 1) instead of (0, 0), you need to add 1.   You can also add the offset of the level to MouseX and MouseY if the level moves about.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 346
Projects: 3
Exp: 5 years


I'm the King!


« Reply #3 - Posted 2011-11-29 03:31:44 »

1  
2  
x = (int)(mouseX/blockWidth);
y = (int)(mouseY/blockHeight);


EDIT: gbeebe, I hate you for beating me by 26 seconds Angry

@OP, the cast to int is not necessary if both mouseX/Y and blockWidth/Height are ints.

Offline loom_weaver

JGO Coder


Medals: 17



« Reply #4 - Posted 2011-11-29 03:35:55 »

BlockX = (int)(MouseX / BlockWidth)
BlockY = (int)(MouseY / BlockHeight)

Oh right... that's much simpler for 2D!  Duh me.

Does anyone know a good efficient method for hit-testing a 3D version?  I can calculate the vector representing the ray from the clicked point but I have an awful lot of blocks to hit-test against it.

[edit] one constraint in my game is that the camera is always top-down (if that helps any)
Offline duce

Senior Newbie





« Reply #5 - Posted 2011-11-29 03:38:21 »

Wow, so simple, but great! Thanks so much!
Offline gbeebe

Senior Member


Medals: 5
Projects: 1



« Reply #6 - Posted 2011-11-29 03:44:28 »

@Ra4king, I still have to get used to that.  In some languages (mostly basic dialects), when setting the value of 1.5 to an integer it would round up to 2.

And something in the back of my mind said "Hurry up and answer this, 'cause you know ra4king is working on this too"
Offline ReBirth
« Reply #7 - Posted 2011-11-29 06:46:50 »

On java, cast to int result in truncing/flooring.

Offline BiGF00T

Junior Newbie





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

@Ra4king, I still have to get used to that.  In some languages (mostly basic dialects), when setting the value of 1.5 to an integer it would round up to 2.

Wow, I just wanted to say "that sounds like BS, no sane language would do this" but then I googled a bit and it seems that I am right (about the language). VBA does this http://msdn.microsoft.com/en-us/library/Aa188474 and the article says:

Quote from: MSDN
Int(-8.2)

Using the Int function, on the other hand, yields "-9":
Which makes sense, although in a very strange way. If the digit right of the dot is below five, then go to the next number below. If the number is negative, this would result in an unexpected decrease.

Well, one never stops learning... and wondering....
Offline gbeebe

Senior Member


Medals: 5
Projects: 1



« Reply #9 - Posted 2011-11-29 16:36:14 »

I don't understand, are you saying I'm right or wrong?  PureBasic did this.
It only makes sense to me, 'cause 1.99999 is better represented as 2 than 1.

Edit:  Ok, I was wrong about the .5 rounding up, it's .6 and higher that round up.  But still, using Int(number) in basic languages drop the decimal point entirely which is why I posted the code above with (int) in it.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline BiGF00T

Junior Newbie





« Reply #10 - Posted 2011-11-29 18:53:43 »

I don't understand, are you saying I'm right or wrong?  PureBasic did this.
It only makes sense to me, 'cause 1.99999 is better represented as 2 than 1.

Edit:  Ok, I was wrong about the .5 rounding up, it's .6 and higher that round up.  But still, using Int(number) in basic languages drop the decimal point entirely which is why I posted the code above with (int) in it.

You are right that 1.99999 is much nearer to 2 than to 1. But I was used to all decimal places being truncated when assigning a floating value to an int variable. After I had read your post, I read on MSDN that this not the case in Basic (at least VBA). The article seems to say that -8.2 is rounded to -9. I'm still a bit confused about that...
Offline gbeebe

Senior Member


Medals: 5
Projects: 1



« Reply #11 - Posted 2011-11-29 19:57:03 »

from your link:
Quote
Note   The Int and Fix functions always return a Double value.
  Int() returns a double?  And people wonder why I stay away from Microsoft  Cheesy
Offline BiGF00T

Junior Newbie





« Reply #12 - Posted 2011-11-29 20:32:13 »

Int() returns a double?  And people wonder why I stay away from Microsoft  Cheesy

What? I didn't read that. So -8.2 float is converted to -9 double?!?! I'm confused... Luckily Java behaves differently.
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.

Grunnt (12 views)
2014-09-23 14:38:19

radar3301 (14 views)
2014-09-21 23:33:17

BurntPizza (31 views)
2014-09-21 02:42:18

BurntPizza (22 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (29 views)
2014-09-20 20:14:06

BurntPizza (33 views)
2014-09-19 03:14:18

Dwinin (48 views)
2014-09-12 09:08:26

Norakomi (75 views)
2014-09-10 13:57:51

TehJavaDev (105 views)
2014-09-10 06:39:09
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!