# How to make a player look in a direction?

This topic is 1522 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm trying to achieve what he did : http://www.gamedev.net/topic/655859-rotate-character-spine-so-weapon-aims-at-target/
to be honest, I don't understand many things. How to make a character look and shoot at a particular direction? I know I should do at pick ray from the center of screen  then I would have the vector between weapon and picked point but how to use this vector to make player aim in a direction? I'm making a FPS game but I would need that because I would have to display other players in my game as 3D characters not just hands and gun since its multiplayer project. This screenshot explains it better :

As you can see that I can only see my hands and gun from my first person perspective but I can see the full body of other players and where they are looking so I must make other players in the game look where they're looking from their first person perspective on their PC.

Edited by newtechnology

##### Share on other sites

Most commonly in FPS games, the user's weapons (as you've shown them) are always drawn in one position (center-screen). The user uses the mouse or keypresses to turn his character left/right/up/down, and, with either a mouse button or keypress, fires the weapon. The projectile then travels (in the 3D world) from the weapon position (at that moment) in the direction the user is facing (at that moment.) There's no picking needed, in that case. The player simply has, as attributes, a position and direction which the player manipulates.

If that's not what you'd like to happen, can you explain a bit more?

##### Share on other sites

Most commonly in FPS games, the user's weapons (as you've shown them) are always drawn in one position (center-screen). The user uses the mouse or keypresses to turn his character left/right/up/down, and, with either a mouse button or keypress, fires the weapon. The projectile then travels (in the 3D world) from the weapon position (at that moment) in the direction the user is facing (at that moment.) There's no picking needed, in that case. The player simply has, as attributes, a position and direction which the player manipulates.

If that's not what you'd like to happen, can you explain a bit more?

Well that's not i asked and i already know that. My game is going to support multiplayer so i need to display full 3d model of the character of other players and therefore they should look and aim at the point where the player is looking i.e client sends information regarding where he is looking and we use this information to display and to make the 3? model look in the direction where client is looking.

For example: i'm playing an online fps game with my friend and when he moves in his world, i can see a 3d model of him moving in my world (he can see only his hands and gun in his perspective since its fps game while i can see his whole 3d model and the same way, i can only see my hands and gun while he can see full 3d model of mine). The same way when he looks at the sky, i can see a 3d model of him looking at the sky. This is what i'm taking about. How to make him look in a sky i.e in a particular direction?

##### Share on other sites

How to make him look in a sky i.e in a particular direction?

Are you familiar with skinned meshes and animating them? If not, I'd suggest you setup a project to render animated skinned meshes, and get a thorough understanding of how bone orientations work. You'll need to set up a routine for orienting the world matrix for a character, and routine(s) for orienting individual bones (neck bone rotation for tilting up/down and rotating left/right).

That is, when another player orients himself, you'll need to get (over the network) his position and orientation, both general direction and specific "pose" direction/information. On your screen, his character will be rendered with specific bones oriented appropriately. If he's looking up to his right, you'll have to decide what bones to rotate to tilt his head back and rotate it in the direction he's looking.

That will require calculating relative angles between a character's facing direction, and the direction up/down/left/right for various bones (neck, hip, etc.) You'll have to decide whether to calculate bone orientations using forward or inverse kinetics.

Edited by Buckeye

##### Share on other sites

How to make him look in a sky i.e in a particular direction?

Are you familiar with skinned meshes and animating them? If not, I'd suggest you setup a project to render animated skinned meshes, and get a thorough understanding of how bone orientations work. You'll need to set up a routine for orienting the world matrix for a character, and routine(s) for orienting individual bones (neck bone rotation for tilting up/down and rotating left/right).

That is, when another player orients himself, you'll need to get (over the network) his position and orientation, both general direction and specific "pose" direction/information. On your screen, his character will be rendered with specific bones oriented appropriately. If he's looking up to his right, you'll have to decide what bones to rotate to tilt his head back and rotate it in the direction he's looking.

That will require calculating relative angles between a character's facing direction, and the direction up/down/left/right for various bones (neck, hip, etc.) You'll have to decide whether to calculate bone orientations using forward or inverse kinetics.
Yeah, I'm familiar with the concepts of animation and skinned mesh. In fact, I already have skinned models implemented into my game. Can you please explain me the math of this and what is inverse/forward kinematics? I think its related to physics.

##### Share on other sites
Have you considered the method suggested in the second-to-last post in that thread that you referenced?

##### Share on other sites

Have you considered the method suggested in the second-to-last post in that thread that you referenced?

tbh, I don't understand it. I just want to know the theory behind what I asked.
Edited by newtechnology

##### Share on other sites
Hello. You should watch the GDC video that I linked in that post, it explains more about additive animation.

It goes like this. You retrieve the mouse velocity for the current frame. It's a 2D vector representing the amount of pixels that the cursor moved from the last frame. You rotate your camera and weapon based on that, with the X component serving as the (negative) yaw rotation (Y axis) and the Y component serving as the pitch rotation (X axis).

You retrieve the world-space Euler rotation angles of your first-person camera, and send these angles to the other players.
In their clients, they will represent you with a 3rd person character that will aim based on the additive blend of 3 character poses: one with the character aiming down (+90º in the X axis), one with him aiming at the horizon, and one with him aiming at the sky (-90º in the X axis).
You will blend between these poses based on the sine of the X angle of the camera from the player they are trying to represent in 3rd person. This is a linear interpolation.

In pseudo-code, the expression for the final animated 3rd person pose be something like this:
If ( otherPlayer.eulerX > 0.0 ) {
// Player is facing down. Represent him with a blend between aim-horizon and aim-down poses.
otherPlayer.SetPose( otherPlayer.aimHorizon.Blend( otherPlayer.aimDown, Sin( otherPlayer.eulerX ) ) )
}Else {
// Player is facing up. Represent him with a blend between aim-horizon and aim-up poses.
otherPlayer.SetPose( otherPlayer.aimHorizon.Blend( otherPlayer.aimUp, Abs( Sin( otherPlayer.eulerX ) ) ) )
}
Using the Sin() value as the "alpha" in a linear interpolation.

##### Share on other sites

Hello. You should watch the GDC video that I linked in that post, it explains more about additive animation.

It goes like this. You retrieve the mouse velocity for the current frame. It's a 2D vector representing the amount of pixels that the cursor moved from the last frame. You rotate your camera and weapon based on that, with the X component serving as the (negative) yaw rotation (Y axis) and the Y component serving as the pitch rotation (X axis).

You retrieve the world-space Euler rotation angles of your first-person camera, and send these angles to the other players.
In their clients, they will represent you with a 3rd person character that will aim based on the additive blend of 3 character poses: one with the character aiming down (+90º in the X axis), one with him aiming at the horizon, and one with him aiming at the sky (-90º in the X axis).
You will blend between these poses based on the sine of the X angle of the camera from the player they are trying to represent in 3rd person. This is a linear interpolation.

In pseudo-code, the expression for the final animated 3rd person pose be something like this:

If ( otherPlayer.eulerX > 0.0 ) {
// Player is facing down. Represent him with a blend between aim-horizon and aim-down poses.
otherPlayer.SetPose( otherPlayer.aimHorizon.Blend( otherPlayer.aimDown, Sin( otherPlayer.eulerX ) ) )
}Else {
// Player is facing up. Represent him with a blend between aim-horizon and aim-up poses.
otherPlayer.SetPose( otherPlayer.aimHorizon.Blend( otherPlayer.aimUp, Abs( Sin( otherPlayer.eulerX ) ) ) )
}
Using the Sin() value as the "alpha" in a linear interpolation.

Okay so I've started understanding this but can I pre-calculate interpolation? maybe not. Also What is alpha? (The Angle is basically used as blend factor, right?)
Is there any other simple technique (Like bone rotation.) ?

##### Share on other sites
- I believe that you can pre-calculate the interpolated poses from aiming up all the way to aiming down as a sequence of 180 frames (one frame for each degree of front rotation).
This would lose the benefit of interpolating poses in real time, which spares memory, but would be simpler to use - get the X rotation of the player from his FPS view and use that angle to pick a pose to represent him as a 3rd person character, and layer this pose with running or jumping sequences etc. so that the 3rd person character can move around while aiming in that direction.
otherPlayer.SetPose( otherPlayer.aimPoses( Int( otherPlayer.eulerX + 90.0 ) ) )
You don't need to pre-calculate the interpolated poses in programming, if you're going in this direction. You only need a 180 frame animation sequence with aim-up in the first frame as a keyframe, and aim-down as the last frame as a keyframe. You can do this in 3ds Max or Blender, which will interpolate between the two poses for each of the 180 frames, and back in your game engine you pick the frame that represents the player rotation. Just make sure that the tangents for the keyframes are set to "LINEAR," so that each frame accounts for an equal amount of change.

- "Alpha" is a possible way to name the interpolation factor; this comes from graphics programming and alpha blending. I believe "s" and "t" are also used to name this parameter.
What you use to calculate this value depends on what you're going to do with the poses. To calculate a blend factor you can use the angle of the player, or the sine function of that angle plus some offset etc.
https://en.wikipedia.org/wiki/Linear_interpolation#Programming_language_support

- I don't think there's any simpler way to do this. When you can set up poses for each critical point (down, middle and up), you can position every limb like they should be, and the software will interpolate between them and make it look natural.
Doing this in code requires much more time and effort, and may not look as natural.

In any case, your own experimentation will give you more answers than what most could say here.

1. 1
2. 2
3. 3
4. 4
frob
13
5. 5

• 16
• 13
• 20
• 12
• 19
• ### Forum Statistics

• Total Topics
632169
• Total Posts
3004545

×