Directional-Based Character Movement

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
matticusrex
Posts: 6
Joined: Tue Apr 29, 2008 6:45 pm

Directional-Based Character Movement

Post by matticusrex »

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.
playerdark
Posts: 64
Joined: Mon Aug 01, 2005 5:06 am

Post by playerdark »

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.
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
Post Reply