• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
javasirc

Stuck on Character Movement (not WASD)

4 posts in this topic

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?
0

Share this post


Link to post
Share on other sites
[quote name='javasirc' timestamp='1349328189' post='4986658']
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?
[/quote]

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). Edited by SimonForsman
2

Share this post


Link to post
Share on other sites
The method above works great, and I would suggest learning it eventually (working with vectors is a good idea in game programing, because getting used to thinking in vectors is mandatory if you want to be successful in 3D programming).

Alternatively, you can try storing your movement as a single direction value (theta) and a distance value (R):

If R is in pixels/second, you will need to get pixels per frame. In general that's just distance_per_second / frames_per_second. Lazyfoo has a great tutorial on moving from pixels per frame to fps independent movement, but for now, get pixels per frame working.

Now, in every frame...
movement_x = R*cos(theta);
movement_y = R*sin(theta);
(Note: A lot of coordinate systems in 2D games use up as y-negative instead of y-positive. If you're using a coordinate space where the top of the screen has the lowest Y, use -R*sin(theta) )

When you move upwards, theta is 90* (or pi/2 if you prefer radians), down is 270* (or 3pi/2), left is 180* (or pi), right is 0* (or 0).
Diagonal movement is as simple as 45* for up/right, 135* for up/left, 225* for down/left and 315* for down/right.
You can multiply a degree by pi/180 to get the radians (and most trig functions like sin and cos take radian inputs).

This is useful if you don't already know where you want the player to end up, and instead only know how far and in what direction they will go. I use it in games with movement controls like asteroid, but the other method works just fine for those games to. Its up to you to chose which way you prefer. Actually, when you get down to it, this is mathematically identical to the final result of the method in the previous post, it just hides the vector math.
0

Share this post


Link to post
Share on other sites
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?
0

Share this post


Link to post
Share on other sites
[quote name='javasirc' timestamp='1349390491' post='4986935']
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?
[/quote]

this is odd: 6*6 and -6*-6 should both be 36 so i think you should post your real code for this part. (I'm assuming you don't put hardcoded positions in your code)
0

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
Sign in to follow this  
Followers 0