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.
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!
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.
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.
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)
Wow, so simple, but great! Thanks so much!
@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”
On java, cast to int result in truncing/flooring.
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:
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…
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…
from your link:[quote]Note The Int and Fix functions always return a Double value.
[/quote]
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.