Jump to content
  • Advertisement

Tile Picker

This topic is 431 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

First of all sorry, for my terrible writing im not good with it, but there is no need to hold back with your answers i only have problems when it comes to writing. 

I started to write my own 2D Game Engine in Java for practice(and for fun) reasons. Until now i've made very good progress but i got a problem, which i got stuck on. 

I started to build a basic tile picker (left click: setTile | right click: getTile). So far so fine, but here is the problem. The further away my mouse gets from the upper left corner of the screen, the more inaccurate the tilepicker gets.

In the picture, you can see the overall tile i clicked on (black cross) and the accurat point (orange dot). This would get me the blue tile underneath.




This is the code i used to translate the mouse coordinates to tile coordinates. 

if(input.mouseleft.isClicked()) {
	tx = (int) (Math.floor(world.getCamera().getX() + input.getMouseX()) / world.getTileSize());
	ty = (int) (Math.floor(world.getCamera().getY() + input.getMouseY()) / world.getTileSize());

	// Checks if the coordinates are within the tilemap
	if(tx >= 0 && tx <= world.getWidth() && ty >= 0 && ty <= world.getHeight() )
  		world.setTile(holdtileid, tx, ty);


I hope somone can help me and thanks in advance.



Share this post

Link to post
Share on other sites

I am not going to try to understand your code, but I would suggest that you try to place a mouse cursor into the game world where you think the mouse cursor should be located. If the mouse cursor in the game world matches with your actual mouse cursor, everywhere on the screen, you know the mouse position is good. Then you can worry about picking the correct tile. The fastest way to trouble shoot that would be to continuously highlight the tile underneath the mouse. If the mouse is over a tile and a different tile is highlighted, you know you have a problem with your math.

Share this post

Link to post
Share on other sites

In addition to what slayemin said, I also suggest you ensure that your tile drawing code is working the way it's supposed to. I've seen instances where errors in the drawing code caused tiles to not be drawn in the correct locations. A problem like that could look exactly like a problem with tile picking.

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!