• Create Account

### #ActualSimonForsman

Posted 04 October 2012 - 12:08 AM

Ive recently began another attempt at being a game developer. In my previous years of development, i had never implemented any moveable NPCs. I have only done main character movement with WASD on the keyboard, which is simple to implement.

I am stuck on making a character/NPC move from one point to another.

Example: Start from point 4,13 heading to destination point 18,6. With a movement speed of 7 points per second at a frame rate of 30 per second = 0.21 points per frame.

Simply adding speed to x or y doesnt work when the character is moving at a non-straight up, left, down, or right direction. I did a bunch of googling but i can only find basic WASD keyboard movement.

Can anyone help me or point me to a resource that can teach me?

if you're at 14,3 and heading to 18,6 the direction vector will be (18,6)-(14,3) = (4,3). (direction = target - position)

if you normalize the direction vector you get: (4/sqrt(4*4+3*3) , 3/(sqrt(4*4+3+3)) = (0.8, 0,6) (to normalize a vector you divide each component (x,y,z,etc) with the length of the vector, this makes the length of the vector 1.0).
So the normalized direction vector is direction = vector2d(0.8 , 0.6) . now it is fairly easy, you just move
player.x += direction.x*speed
player.y += direction.y*speed

(oops, i misread your positions, but you should get the idea anyway).

### #1SimonForsman

Posted 04 October 2012 - 12:03 AM

Ive recently began another attempt at being a game developer. In my previous years of development, i had never implemented any moveable NPCs. I have only done main character movement with WASD on the keyboard, which is simple to implement.

I am stuck on making a character/NPC move from one point to another.

Example: Start from point 4,13 heading to destination point 18,6. With a movement speed of 7 points per second at a frame rate of 30 per second = 0.21 points per frame.

Simply adding speed to x or y doesnt work when the character is moving at a non-straight up, left, down, or right direction. I did a bunch of googling but i can only find basic WASD keyboard movement.

Can anyone help me or point me to a resource that can teach me?

if you're at 14,3 and heading to 18,6 the direction vector will be (18,6)-(14,3) = (4,3).

if you normalize the direction vector you get: (4/sqrt(4*4+3*3) , 3/(sqrt(4*4+3+3)) = (0.8, 0,6)
So the normalized direction vector is direction = vector2d(0.8 , 0.6) . now it is fairly easy, you just move
player.x += direction.x*speed
player.y += direction.y*speed

PARTNERS