天行健 君子当自强而不息

Controlling Players and Characters(9)

 

Rotational Control

Rotational control allows the player to rotate the character using the left and right
arrow keys and forward and backward using the up and down arrow keys.

Rotational control is better than directional control is some aspects, because the
movement calculations are more basic. Characters need to store a directional value
now, however, that represents the direction they are facing (with 0.0 pointing along
the positive Z-axis, 1.57 pointing along the positive X-axis, 3.14 pointing along the
negative Z-axis, and 4.71 pointing along the negative X-axis). Assume that the
direction variable is as follows:

float Facing = 0.0f; // Direction character is facing

At this point, you can say that pushing the left control (the left arrow key or left on
a joystick) rotates the character to the left (negative rotation), and pushing right
rotates the character to the right (positive rotation):

// using the Input Core:
// Keyboard = pre-created cInputDevice object for keyboard
Keyboard.Read();

// Rotating left?
if(Keyboard.GetKeyState[KEY_LEFT] == TRUE)
  Facing -= 0.01f; // Rotate left .01 radians

// Rotating right
if(Keyboard.GetKeyState[KEY_RIGHT] == TRUE)
  Facing += 0.01f; // Rotate right .01 radians

From the Facing angle variable, movement forward and backward becomes a matter
of the following:

// Move forward?
if(Keyboard.GetKeyState[KEY_UP] == TRUE) {
  XPos += (float)cos(Facing-1.57f) * Distance;
  ZPos += (float)-sin(Facing-1.57f) * Distance;
}


// Move backward?
if(Keyboard.GetKeyState[KEY_DOWN] == TRUE) {
  XPos += (float)-cos(Facing-1.57f) * Distance;
  ZPos += (float)sin(Facing-1.57f) * Distance;
}

 

First Person Control

The last type of directional control I like to use is one for a first-person style game
in which you see the world through the eyes of the character. This form of control
uses the arrow keys to move characters left, right, forward, and backward and the
mouse to rotate the view (much like turning your head as you look around).

Pressing the up arrow moves characters forward in the direction they are looking,
whereas pressing the down arrow moves characters backward. The left and right
arrows move left and right. First-person control is similar to the rotational control
you just read about, but with first-person control, the mouse turns the character.

This time however, it’s not characters that rotate, but the camera (because the camera
view represents the view from the character’s eyes). This introduces a couple of
new variables that represent the camera angles:

float XAngle = 0.0f, YAngle = 0.0f; // Character viewing angles

The two preceding variables will now hold the viewing angle, which is modified as
the player moves the mouse. Here’s the code to modify the viewing angles:

// Assuming using the Input Core
// Mouse = pre-created cInputDevice for mouse
// same for keyboard
Mouse.Read();
Keyboard.Read();

// Rotate the character based on mouse angle
XAngle += Mouse.GetYDelta() / 200.0f;
YAngle += Mouse.GetXDelta() / 200.0f;

// Move character
if(Keyboard.GetKeyState[KEY_UP] == TRUE) {
  XPos += (float)cos(YAngle-1.57f) * Distance;
  ZPos += (float)-sin(YAngle-1.57f) * Distance;
}

if(Keyboard.GetKeyState[KEY_DOWN] == TRUE) {
  XPos += (float)-cos(YAngle-1.57f) * Distance;
  ZPos += (float)sin(YAngle-1.57f) * Distance;
}

if(Keyboard.GetKeyState[KEY_LEFT] == TRUE) {
  XPos += (float)cos(YAngle-3.14f) * Distance;
  ZPos += (float)-sin(YAngle-3.14f) * Distance;
}

if(Keyboard.GetKeyState[KEY_RIGHT] == TRUE) {
  XPos += (float)cos(YAngle) * Distance;
  ZPos += (float)-sin(YAngle) * Distance;
}

Notice that whenever the user moves the mouse, a delta value (the amount of
movement) is used to rotate the view. From there, calculating which direction to
move the character is easy.


posted on 2007-11-11 23:46 lovedday 阅读(201) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航:   博客园   博客园最新博文   博问   管理


公告

导航

统计

常用链接

随笔分类(178)

3D游戏编程相关链接

搜索

最新评论