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

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.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Forum Statistics

    • Total Topics
    • Total Posts
  • Similar Content

    • By shillhunter
      I'm looking to add some new experience so I can get better at development, so I'm looking to join a team. I'm great with retro pixel art, even better with reference images (below is an example of my pixel art work), and I just started learning about mobile development. More specifically, Android Studio, though I am trying to expand my horizons. I have great programming experience, and am versatile. I took one year of C++ at my school, and studied Java independently. I'd like to find a team to join that's working on a 2D pixel art game for Android phones.

    • By sang_261198
      Hi everyone.
      My name is Sang and I am a student in primary school. I studied Java and  i am really like it.
      and now I want to learn make game a game use Java in Java Swing. Would you like give me some websites teach make game java for begginers, please?
      Thank you very much!
    • By yoel1234
      i created 10 more programming tutorial videos about how to use my game framework.
      i hope this will help people develop games and learn game programming in general.
      if you have any feedback i would love to hear about it.
    • By Giannis Marinakis
      Hello everyone!
      I have a GitHub repository on which I'm developing a Open Network Library for Java Developers and I'd like to share it so more people come in and contribute. Till now the repo is being developed only by me so the library is not 100% bug free. I'd really like this repo to be famous and got people working on it!
      [You will find more information in the repo]
      Here's the link for the repo:
    • By PaCkEtPiRaTe
      I'm currently remaking a game I made a few years back using Slick2D (as opposed to Swing/AWT, which was a terrible idea). I've fleshed out a lot of the background architecture, but I'm starting to run into issues with my architecture and I'm not sure how to proceed.
      The game I'm making is an overhead shooter. It's wave-based, with hordes of zombies coming at you. There are 10 weapons to choose from.
      Specifically, my latest issue is with a particular "weapon" I'm designing; the Laser Barrier. In the previous game, I had a "Laser Wire" weapon, which when two terminals were placed on the ground, created a wire made of laser on the ground that would damage enemies that passed through. Problem was that it didn't do enough damage in the short time that the enemies would be colliding with it for it to be of any use, so it was a waste of money.
      In the remake, I'm instead creating the "Laser Barrier", which visually looks the same, but instead of damaging enemies that touch it, it will act as an obstacle that enemies can't walk through. The enemies damage the shield while they are in contact with it until the laser barrier collapses. Projectiles however, can still pass through the barrier, allowing the player, and certain enemies, to shoot through them.
      The issue I'm running into, though, is the method of communicating between the Laser Wire itself and the enemy touching it. I'm currently able to detect a collision between the enemy and wire projected between the two laser terminals, but I'm not sure how I can implement the actual movement blocking part.
      It would take too much text to explain how it works, so here are the relevant files in my project:
      Player class - the checkProjectiles() method on line 344 is where the game loop checks for collisions between the player's weapon projectiles and the an enemy passed as an argument. On line 350, you can see that when there is a collision between the enemy and the LaserNode object (collision method is linked below), the laser node takes damage so that it will eventually be destroyed. I figured this is where the "movement blocking" should be, as I have access to the terminal and the enemy, and this is where a collision is confirmed. LaserNode class - this is the class representing the laser terminals on the ground that project the laser beam between them. The checkCollision() method on line 57 is used to determine if the enemy is touching either of the terminals, or if it is touching the beam itself. Enemy class - this is the base class for all game enemies. You can see what methods are available to all enemies, so perhaps this can provide some insight into what could be done to communicate with the LaserNode. I realize it's a lot to ask considering the scope of my project, but could someone give me an idea of how to communicate between the LaserNode and Enemy so that the enemy knows not to move when touching the LaserNode? The only methods I can think of seem cumbersome and it seems like I'd be adding a lot to the Enemy class just to get this one feature working.
      I'd love to script these weapons with LUA, but I never learned how to integrate a scripting language into my game architecture. I also have limited experience writing game engines, so I'm sure there's a lot of refinement that could be done to make my game architecture less restricting.
      I don't expect anyone to actually comb through my project and make suggestions, but I would super appreciate it.
  • Popular Now