How to project a vector to a plane?

I cannot find any method in document(pc.Vec3). there is only one method call “project” but this function is project a point to a vector.

Please help.

Hi @ZipZapWxh,

You can use something similar with the example provided in the documentation for the Vec3 project method.

The normal in this case would be the vector pointing outwards from the surface:

// vec is your vector

// normal is the vector pointing outwards from the horizontal surface
var normal = new pc.Vec3(0, 1, 0);


console.log("The result of the vector projection is: " + vec.toString());
1 Like

Thank you so much. I will try this when I wake up. It’s 5am China time. :yawning_face:


It’s seems not work in my case…

Can you provide us with a sample project?

for anyone else searching for this answer:
as of writing there is no built-in project on plane function (Vec3.project is something else entirely) but you can write your own using Vec3.cross like so:

ExampleScript.prototype.projectOnPlane = function (direction, normal) { //vec3, vec3
    direction.cross (direction, normal);
    direction.cross (normal, direction);
    return direction.clone ();

note that this function doesn’t normalise the inputs or outputs, which won’t affect the direction it returns, but will affect the length (magnitude) of that direction, so if you want to make your output direction a certain length you’ll need to normalise and scale it once it’s returned.

i’m still not sure when clones should be returned instead of references but it seems like the safe way to go in this example. if someone else knows otherwise please do correct me.

1 Like