babadahal
babadahal

Reputation: 41

Calculate a perpendicular vector from my camera lookat

My camera lookat is in a orthographic perspective (much like in a 2D space) it's staring at the ground from top. I'm trying to derive direction so that my character is moving left and right.

Given vector, how can I derive perpendicular vectors? So it can move left and right.

Upvotes: 1

Views: 2521

Answers (1)

vallentin
vallentin

Reputation: 26245

Any 3D vector (not counting zero vectors) have multiple valid perpendicular vectors.

You can calculate a perpendicular vector, by calculating the cross product given 2 non-parallel vectors. So you'd have another vector which to calculate it against, say a reference.

Thus assuming you have a direction/forward vector:

var forward = new THREE.Vector3(0, 0, 1);

Then using another vector such as THREE.Vector3(0, 1, 0). (If y is up, then this would be the upwards unit vector.) Then by calculating the cross product, we can calculate the right direction vector (or left direction by flipping the order or simply negate the right vector):

var right = forward.cross(new THREE.Vector3(0, 1, 0)).normalize();

You can then also get the relative up vector, by doing:

var up = right.cross(forward).normalize();

Which rendered would look something like this. The blue arrow being the forward vector, the red arrow being the right vector, the green arrow being up and the darker green being the reference it's calculated against.

Here's an animated version of the above example.

Example

Upvotes: 2

Related Questions