How to define diagonal directions? I recently ran into an issue when applying forces to an object. I was working with this question. I am trying to add forces to my character as certain keys are pressed. Here are the keys and their direction of force:

W = forward

S = backward

A = leftward

D = rightward

As I hit any of the keys, a force is added to its direction. However, a problem arises with diagonal movements as a different technique would be needed to define direction. I am having trouble with defining diagonal directions. For instance, (10, 10, 0) would add the force 10 on x and y. This will not work. I need to add 10 to a new direction; this direction will be the diagonal consisting of the two directions combined. I will add the force to this direction. I am puzzled on how I would define such a direction with the (x, y, z) format. How could I define a diagonal direction?
As I hit any of the keys, a force is added to its direction.


This makes them separate. But you say you want them combined.

So combine them. Don't apply them immediately, but gather them together, then apply a vector with the length you want.

The easiest way is to just add together the different impulses, normalize the resulting vector, scale it to the desired size, and apply the scaled result to the object.

Example:

from mathutils import Vector

combined_force = Vector()
vector_length = 10.0

if is_forward_key_pressed:
  combined_force += Vector((1.0, 0.0, 0.0))

if is_backward_key_pressed:
  combined_force += Vector((-1.0, 0.0, 0.0))

if is_left_key_pressed:
  combined_force += Vector((0.0, -1.0, 0.0))

if is_right_key_pressed:
  combined_force += Vector((0.0, 1.0, 0.0))

combined_force.normalize()
combined_force *= vector_length

game_object.applyForce(combined_force)


This calculation would occur at most once each frame, instead of repeating it for each key pressed.

A more complex way would be to track the direction as Euler angles, Quaternions, or Matrices, and calculating a final vector from those.