Page 1 of 1

Finding y-coordinate in Quad

Posted: Mon Mar 30, 2015 8:31 pm
by kubatp
Hi,
I create a manual object with quads and I keep the coordinates in memory array. I later need to find out a height (y-coordinate) of the point on this manual object. Manual object is a grid and from the grid I have closest corners (p1, p2, p3 and p4). Somewhere in the quad is a point for which I have x and z coordinate and I need to find y.
Is there an easy way to get it?
xCoordinateInQuad.png
Thank you for help

Re: Finding y-coordinate in Quad

Posted: Tue Mar 31, 2015 9:20 pm
by kubatp
Nobody can help me here? I presumed that many people had similar problems already

Re: Finding y-coordinate in Quad

Posted: Tue Mar 31, 2015 9:52 pm
by Herb
Yes, I've done something similar. I have a large quad and I needed to get the X,Y on it when I click the mouse on the Ogre::Plane. So, I used the wiki's article on Raycasting to the polygon level to do it. Basically you could tweak it to send a raycast from above (doesn't have to be mouse click based) down on your shape to get an intersection to get the Y value. Basic raycasting doesn't work as it's by bounding box, but this article has sample code to get down to the polygon level which will get you the precision you need.

Re: Finding y-coordinate in Quad

Posted: Tue Mar 31, 2015 9:56 pm
by scrawl
This is more of a linear algebra question than an Ogre question, I'm sure you could find a better explanation on other sites.
Check which triangle the point is on. Define a plane equation for that triangle using the 3 points notation. Then solve the plane equation using the s and t values that define your target point.

Re: Finding y-coordinate in Quad

Posted: Tue Mar 31, 2015 10:02 pm
by kubatp
Hi,
thank you for your replies.
Herb wrote:I used the wiki's article on Raycasting to the polygon level to do it
Yes, I considered this option, but I dont really want to do raycasting when I can solve this mathematically.
scrawl wrote:This is more of a linear algebra question
Yes, you are absolutely right. I just wanted to ask if anyone else was already solving this problem and might give me a quick answer. I simply thought that this is something many people solved already before and I didn't want to reinvent the wheel.

Re: Finding y-coordinate in Quad

Posted: Tue Mar 31, 2015 10:07 pm
by c6burns
It's not really an Ogre question, but ...

Equation of plane:
Ax + By + Cz + D = 0

Algebra:
By = -Ax - Cz - D
y = (-Ax - Cz - D) / B

A, B, C and D are known because you defined the plane. So ... plug in your known x and z values to find y. If unclear, google it because I am so shit at maths, so if I'm explaining to this to you then you need to do some reading. I remember covering this in high school algebra.

You can see an implementation of this in Terrain::getHeightAtTerrainPosition where it returns: (note: it treats height as z)
(-plane.normal.x * x - plane.normal.y * y - plane.d) / plane.normal.z;

EDIT: Ah you bums posted while I was posting :lol:

Re: Finding y-coordinate in Quad

Posted: Tue Apr 07, 2015 10:25 am
by kubatp
Hi,
thank you very much for your help, especially pointing to this getHeightAtTerrainPosition. It helped me a lot and solved the problem.