Jump to content
Posted 03 October 2012 - 11:23 PM
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?
Edited by SimonForsman, 04 October 2012 - 12:08 AM.
Posted 04 October 2012 - 12:25 AM
Posted 04 October 2012 - 04:41 PM
Posted 05 October 2012 - 12:46 AM
Thanks so much for the replies. I tested it out on my calculator and paper and it works. I remember doing Vectors in high school and failing that subject. I guess i should have paid more attension in class.
I have one more question reguarding the direction the character is moving though.
If the character is moving Up+Right (not at a 45 degree angle, but when the direction is (+x, -y)... let me show an equation:
Current point: A=(1,-2) and destination point: B=(8,-8) so the vector is (8 - 1, -8 - -2) = (7,-6)
The amount added to the character's X coord is: (7/sqrt((7*7)+(-6*-6)) * 0.21 = .224
The amount added to the character's Y coord is: (-6/sqrt((7*7)+(-6*-6)) * 0.21 = -.408
To test if the character is moving at 0.21 points per frame, sqrt((.224*.224) + (-.408*-.408)) = .465, which is not correct.
However if i make the negative number positive by multiplying it by -1, it works as shown:
(7/sqrt((7*7)+((-1*-6)*(-1*-6)))*0.21 = .159
(6/sqrt((7*7)+((-1*-6)*(-1*-6)))*0.21 = .137
And to test it: sqrt((.159*.159) + (.137*.137)) = 0.21 which is correct.
I know its easy to determine if the character is moving UpRight, DownRight, DownLeft, DownRight, as i already do for my method to figure out what degree/direction the character is travelling, but is it nessessary to do this, or is it possible to use one equation reguardless of what direction the character is going?