# Calculating height at an arbitrary position on a quad

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

## Recommended Posts

Hey all, having a pretty tough time with this math problem, wondering if anyone here can push me in the right direction. Here is the problem:

You are given a 3D quad Q, where all 4 vertices are known. You are also given a point P where Px and Pz are known, and lie within the bounds of Q. It is also safe to assume that the x and z components of the vertices of Q form a square (as in, if you looked at Q down the Y-axis, you would see a square). Given this information, find Py.

It seems as if the cross product would be useful here, but I'm not sure which vectors to cross to get my answer. Anyone have any ideas?

##### Share on other sites
In short: calculate the plane of the quad. Solve the plane equation for Y.

An attempt at a few more details...

Solving for y: a plane equation is in the form Ax + By + Cz + D = 0, so y = -(Ax + Cz + D) / B.

Plane for the quad: the cross product of two adjacent edges in the quad gives the normal of the plane (A, B, C) and then any point in the quad can be used to solve for D.

[indent=1]N = (Q2 - Q1) x (Q1 - Q0)

[indent=1]A = Nx

[indent=1]B = Ny

[indent=1]C = Nz

[indent=1]D = -(N dot Q0)

(where Q0 through Q3 are the points of the quad. D could be rewritten as D = -(A*x + B*y + C*z) where x,y,z are the components of Q0, if that makes more sense to you.)

so expanding it out a bit:

[indent=1]Py = -(Nx * Px + Nz * Pz - (N dot Q0)) / Ny

I'm sure a google search or two could explain the details better than I have, but hopefully this is enough info to get you going.

##### Share on other sites
Given that the quad is a square on the X/Z plane, formed by V1, V2, V3 and V4 (top left, top right, bottom left, bottom right) you can do:

 s=(Px-V1.x) / (V2.x-V1.x) t=(Pz-V1.z) / (V3.z-V1.z) y1 = V1.y + s*(V2.y-V1.y) y2 = V3.y + s*(V4.y-V3.y) Py = y1 + t*(y2-y1) 

In effect, you are calculating the parameters s and t that correspond to the point, P, on the plane. Then you are using these parameters to interpolate the Y values of the 4 points.

##### Share on other sites
Hey all,

Thanks for the help! I was able to figure out the solution using Ray/Plane intersection, which looks similar to what you guys did. Here is the code snipit that performs the primary calculations, for future people:

 vec4 S = vec4(Px, 0.0f, Pz, 1.0f); // Ray start position vec4 V = vec4(0.0f, 1.0f, 0.0f, 0.0f); // Ray directional vec3 N = glm::normalize(glm::cross(P1 - P0, P2 - P0)); // Normal Vector vec4 L = vec4(N, glm::dot(-N, P0)); // Plane Vector float Py = -(glm::dot(L, S) / glm::dot(L, V)); // Calculate final Y value return Py; 

Thanks again for the response.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 16
• 9
• 15
• 9
• 11