Tile movement toward mouse not working correctly

Hey guys, i’m playing around with code for a tower defense game i’m working on…or it will be a tower defense game when i’m done. Right now, i’ve been trying to play around with pathfinding and a movement system…I did some googling and tried to implement a system that will move the sprite tile by tile to where the mouse clicks. I haven’t implemented astar or anything yet…I’m trying to get a basic movement system setup so I can move the sprite one tile at a time toward a mouse click…However, it isn’t working. If I click below or to the left of the sprite, it will move extremely fast to the tile clicked. If I click above or to the right of the sprite, nothing happens.

I’m a little embarrassed that i’m having trouble doing this. I’m still a newbie but have been putting a little more time into learning lately. I’m having a problem getting things structured correctly and figuring out things like this. I’m not sure how to turn it into a decent working movement system. Here is the code i’m having a problem with. My tiles are 32x32, when I call the moveTo() function, i’m breaking the mouse position down into tile position by dividing by 32.


public void moveTo(int px, int py) {
		state = state.WALKING;
		
		destx = px;
		desty = py;
		
		dx = px - x;
		dy = py - y;
		
		float length = (float) Math.sqrt(dx*dx+dy*dy);
		
		dx=dx/length;
		dy=dy/length;
		
		
		
	}
	
	
	
	public void update(float delta) {
		
			if (state == state.WALKING) {
				
			if (x != destx) {
				x += (dx * speed) * delta;	
			}else{
				dx = 0;
			}
				
			if (y != desty) {
				y += (dy * speed) * delta;			
			}else{
				dy = 0;
			}
			
			if (dx == 0 && dy == 0) {
				state = state.IDLE;
				destx = 0;
				desty = 0;
			}
			
			System.out.println("x: " + x + 
					" y: " + y +
					" dx: " + dx +
					" dy: " + dy);
			
			}
		//System.out.println("X: " + x + " Y: " + y);
	}

When I face a problem like this (one in which it seems simple, yet I can’t get it to work no matter what), I do one of two things:

  1. I start from scratch and develop a system that works (not recommended)
  2. I start simple. In your case, making a function that successfully moves an entity from one tile to an adjacent tile and keep improving on this until you have reached the desired effect.

I would try first getting adjacent tile movement working (where you can freely move an entity around one tile at a time using wasd or any simple movement controls).

I would then work on automating movement along the axes so that I could move the entity any number of spaces in any of the four directions (or 8 if you are allowing diagonal movements).

Once both of these are functioning properly, I would use the axis aligned movement to create an ‘L’ shaped path to the point where I want the entity to move.

From there, you should be able to implement any kind of path-finding algorithm you would like.
(most of them could probably be done without the axis aligned movement)

As far as the “move extremely fast to the tile clicked” issue…try doing some research on delta-time for game development.

More code please, especially what type dx, dy are.

Cheers,

Kev

Here is the rest of the class. I’m still learning so please ignore the dirty code. I was playing around with switching everything to vectors as well so ignore the unused variables!

dx/dy are floating points.

I’m using delta time already.

The sprite actually moves one tile at a time now, it just only moves in a negative direction. I took a break from the code. I’m going to go through it again and try to figure out what i’m doing wrong.

EDIT I actually got it working. I had it setup incorrectly. Here is the new pastebin
http://pastebin.com/yY3UMDUz