Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (754) Games in Android Showcase (229) games submitted by our members Games in WIP (842) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Getting Clicked Block  (Read 3060 times) 0 Members and 1 Guest are viewing this topic.
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.
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!
gbeebe

Senior Devvie

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
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.
ra4king

JGO Kernel

Medals: 507
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

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

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.

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

Senior Newbie

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

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

Senior Devvie

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"
ReBirth
 « Reply #7 - Posted 2011-11-29 06:46:50 »

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

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....
gbeebe

Senior Devvie

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.
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...
gbeebe

Senior Devvie

Medals: 5
Projects: 1

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

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
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

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

 DesertCoockie (20 views) 2018-05-13 18:23:11 nelsongames (68 views) 2018-04-24 18:15:36 nelsongames (65 views) 2018-04-24 18:14:32 ivj94 (748 views) 2018-03-24 14:47:39 ivj94 (79 views) 2018-03-24 14:46:31 ivj94 (579 views) 2018-03-24 14:43:53 Solater (94 views) 2018-03-17 05:04:08 nelsongames (168 views) 2018-03-05 17:56:34 Gornova (362 views) 2018-03-02 22:15:33 buddyBro (1022 views) 2018-02-28 16:59:18
 Java Gaming Resourcesby philfrei2017-12-05 19:38:37Java Gaming Resourcesby philfrei2017-12-05 19:37:39Java Gaming Resourcesby philfrei2017-12-05 19:36:10Java Gaming Resourcesby philfrei2017-12-05 19:33:10List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-03-02 08:44:05
 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