Jump to content
  • Advertisement
Sign in to follow this  

[Java] Help with Grid Movement and Collisions

This topic is 2932 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

[font=verdana, sans-serif][size=2]I'm fairly new to Java, and I'm trying to make an RPG for my own learning purposes. I am trying to make it move on a grid (à la Pokemon grid movement), but I am having trouble with this grid movement and collisions as well. I am wondering if someone could help explain to me the best ways to do grid movement and collisions, or how I can fix the code I have right now. Like I said before, I'm fairly new to Java so my code is probably not the most efficient. But any help would be greatly appreciated!

I'll try to explain the problems I am having now. The grid movement semi-works... if I tap on a key to move the player, it will correctly move 16 pixels (that is my grid size) and then stop. But if I hold the key and then release, it won't move exactly to a grid coordinate (for example it would move to an X coordinate of like 4.25 instead of moving to 4.00).

Collisions are also a problem. If I tap the movement key a few times to slowly approach an NPC, it will detect the collision. But if the key is held down, it will not to detect the collision. Also, sometimes whenever it tries to detect the collision, the game crashes and gives me a "java.lang.NullPointerException", saying it is from a method that gets the height of the sprites image. For some reason, something messes up when getting the height of a sprite in my collision check method.

I hope this all makes sense, and if anyone could either help explain what I am doing wrong, or explain efficient ways of doing grid movement like the Pokemon games and collisions, that would be great!

Here is my source code:

Share this post

Link to post
Share on other sites

Sorry I don't have time to review your source.

Tile based collisions are typically really easy, though. You just need to set a timer to iterate through "steps" every so often as the character walks (wherein you can plan transition animations between tiles), and then compare where the character is moving to against the array of tiles in the map to determine if it's collisionable or not.

I'm not sure how you're doing movement, but if it isn't tile-by-tile (with only an aesthetic coverup in the form of an animation), it will make the process a bit more difficult (certainly still doable, but not as simple).

On move (whether initiated by a single key tap, or by a timer based on holding the key down), I would test the tile position that the character is going to move in. If it's OK, I'd update the character's position to that tile. If it's not OK, I would follow the process that tile prescribes (I used to like to do fun things with it, like damaging tiles, etc.).

I hope that gives you some ideas...


Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!