天行健 君子当自强而不息

Controlling Players and Characters(8)

 

Controlling Player Characters

Your player is the most important character in the game, so you need complete control
of him. Typical games utilize a simple directional control scheme, similar to the
scheme you read about in the section “Player Navigation.”

A direct control scheme enables you to manually move a character forward, backward,
left, right, up, and throughout the map. Nothing can be more direct, so I
tend to use the direct control method to control characters.

There are two types of direct controls—directional control and rotational control, which
are covered in the following two sections.

 

Using Directional Control
 

Directional control uses controls such as arrow keys or a joystick to move characters
in a single direction. Directional control is best suited to third-person games in which
the camera is viewing the character from above and from the side (as illustrated in Figure 16.1).

To move a character by using directional controls, you need to know the camera’s
angle (along the Y-axis), the character’s position, and the direction in which the
character wants to move (0.0 being up or away from the camera, 1.57 being right,
3.14 being down, and 4.71 being left).

Assume that the character’s coordinates are represented in a pair of variables:

float XPos, ZPos; // YPos = height and is not required yet.

Now the camera’s angle (along the Y-axis) is represented with a variable:

float CameraYAngle; // The angle of view

Last, you have the direction you want to move the character and the distance to move in that direction:

float Direction; // Direction to move
float Distance; // How far to move

Next, set up a sample in which a character is located at the origin of the world and
wants to walk up (relative to the camera view); the camera is positioned to the right
of the origin along the X-axis and is pointed left, as illustrated in Figure 16.2.

Here are the variables to set up and the calculations to perform:

XPos = ZPos = 0.0f; // The character’s position
CameraYAngle = -1.57f; // The camera angle
Direction = 0.0f; // Direction to move
float Distance = 4.0f; // The distance to move character

// The new direction (angle) to move
float NewAngle = CameraYAngle + Direction - 1.57f;

// Move character
XPos += (float)cos(NewAngle) * Distance;
ZPos += (float)-sin(NewAngle) * Distance;

At this point, the character has moved 4.0 units away from the camera, which
means that the character is now located at X=-4.0, Z=0.0, which is just where you
want the character to be. The only problem now is determining the character’s
height (for those using a 3-D engine).

CAUTION
Using distance values can be a problem because you’re not controlling how fast
each frame is updated in your engine. When a game is running full speed on
one computer and is updated 100 times a second, the game’s characters will
move much faster on that computer than they will on a computer that is
updating the game 30 times a second. You need to limit the number of times
your game updates each frame.


posted on 2007-11-11 22:37 lovedday 阅读(159) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


公告

导航

统计

常用链接

随笔分类(178)

3D游戏编程相关链接

搜索

最新评论