Hi, I'm relatively new to 3D programming and I'm having a hard time conceptualizing something. I want to make a game that uses FPS style controls, where S and D rotate my character, and W and S move forward/back. I understand how key inputs work, and rotation and have a decent knowledge of vectors, but I can't wrap my head around how I would store the direction my character is facing and then move him in that direction (as opposed to just moving +2 in the Z direction when I push W).
If this is confusing let me know, any help is greatly appreciated.
Directional-Based Character Movement
-
- Posts: 64
- Joined: Mon Aug 01, 2005 5:06 am
It's rather easy
You store the directional vector, for example a = (1,0,0) meaning that your character moves 1 meter in x direction per second.
You implement a mainstep() function that is called once per frame in your games mainloop.
Whenever you call the function you store the GetTickCount() the function is called. When it is called the first time you just store the GetTickcount() value and do nothing else.
Every subsequent frame you calculate the delta time from the last mainframe() call as such: delta = GetTickCount() - lastframetickcount
This gives you the number of milliseconds since the last call. Now all you have to do is calculate a vector that represents the distance travelled since the last mainframe call as such: b = (a * deltatime) / 1000.0f
This is the distance in meters travelled since the last frame. Now you take the loast known psition, say it was c = (100, 100, 100) and add the new vector to it to get the new position.
This approach keeps your moving speed constant regardless of the frame rate at which your program runs on different computers. The same type of approach can be used for everything that changes at a constant rate over time, for example the rate at which healthpoints are restored or so.
You store the directional vector, for example a = (1,0,0) meaning that your character moves 1 meter in x direction per second.
You implement a mainstep() function that is called once per frame in your games mainloop.
Whenever you call the function you store the GetTickCount() the function is called. When it is called the first time you just store the GetTickcount() value and do nothing else.
Every subsequent frame you calculate the delta time from the last mainframe() call as such: delta = GetTickCount() - lastframetickcount
This gives you the number of milliseconds since the last call. Now all you have to do is calculate a vector that represents the distance travelled since the last mainframe call as such: b = (a * deltatime) / 1000.0f
This is the distance in meters travelled since the last frame. Now you take the loast known psition, say it was c = (100, 100, 100) and add the new vector to it to get the new position.
This approach keeps your moving speed constant regardless of the frame rate at which your program runs on different computers. The same type of approach can be used for everything that changes at a constant rate over time, for example the rate at which healthpoints are restored or so.
Or check this:
http://irrlicht.sourceforge.net/phpBB2/ ... php?t=4680
http://irrlicht.sourceforge.net/phpBB2/ ... php?t=4680