Advertisement

Tile Picker

Started by September 07, 2017 11:55 PM
4 comments, last by JesterCall 7 years, 2 months ago

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.

 

tilepicker.png.ecbfccb2b178f8f232f643534b36fbc7.png

 

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.

 

 

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.
 

Advertisement

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.

What JTippetts said: Tile picking is easy enough.

Ensure that one pixel on the screen is actually one pixel in your world. This happens if the client size does not match the backbuffer size.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

Thanks for the replies.

I fixed it. "window.pack()" caused the offset of my mouse.

This topic is closed to new replies.

Advertisement