Skip to main content
GameDev.net gamedev.net
🔒 Locked

Finding which Block Clicked

Started by TheDuceCat Nov 29, 2011 at 1:40 AM 1 replies 1.1k views
Original Post
TheDuceCat
TheDuceCat
I am making a very simple 2D sandbox game, kind of a clone of Terraria. When the player clicks a block, I want it to delete. I'm not sure how I'm going to do this. The only way I could think was to make a Rectangle object surround the block, then using a for loop to cycle through every single block and check if the mouse's co-ordinates are contained in the Rectangle, but it seemed way too inefficient.

I'm using Java and Slick2D.
Servant of the Lord
Servant of the Lord
How are you drawing the blocks? Use the same positioning you use to draw the blocks to get their physical location in the game world, and calculate the location of the mouse in the game world.

Example:
Block = (MousePos + CameraPos) / BlockSize;

Block is the cell that a block may or may not be in. You can usually use this to get the index into your array of blocks, if you have one, with something like this:
Index = (Block.y * NumBlocksWide) + Block.x;
(If your blocks are stored that way in memory).

CameraPos is the location of the camera (usually the upper-left corner of the screen) in the game world, in pixels. MousePos is the location, in pixels, relative to the upper-left of the screen (which is why MousePos + CameraPos equals the position of the mouse in the game world). BlockSize is the constant size of the grid cells of your world.l

This is assuming you are using a grid-based system for blocks. If not, you can partition your game world into chunks, and have a list of objects in each chunk, and then only check if the mouse is in the bounding box of any object in that chunk.
TheDuceCat
TheDuceCat

How are you drawing the blocks? Use the same positioning you use to draw the blocks to get their physical location in the game world, and calculate the location of the mouse in the game world.

Example:
Block = (MousePos + CameraPos) / BlockSize;

Block is the cell that a block may or may not be in. You can usually use this to get the index into your array of blocks, if you have one, with something like this:
Index = (Block.y * NumBlocksWide) + Block.x;
(If your blocks are stored that way in memory).

CameraPos is the location of the camera (usually the upper-left corner of the screen) in the game world, in pixels. MousePos is the location, in pixels, relative to the upper-left of the screen (which is why MousePos + CameraPos equals the position of the mouse in the game world). BlockSize is the constant size of the grid cells of your world.l

This is assuming you are using a grid-based system for blocks. If not, you can partition your game world into chunks, and have a list of objects in each chunk, and then only check if the mouse is in the bounding box of any object in that chunk.


Yeah, I can't believe how simple this solution is. I completely forgot.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.