Jump to content
  • Advertisement
Sign in to follow this  
Xeile

Vector question

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I would like to know if a function exist, before I made one for myself. I got a line, defined by 2 vectors (Start and End are the names for this example). Direction is determined by End - Start. Now I want to know the X and Y value when I give the Z value. Is there a function that does this? The function doesn't have to check if the Start.z and End.z are different to each other.

Share this post


Link to post
Share on other sites
Advertisement
linear interpolation (slerp).

basically, Xmiddle = (Xend - Xstart) * (Zmiddle - Zstart) / (Zend - Zstart)

Share this post


Link to post
Share on other sites
So you want:

d = e - s
for some alpha and given d and z:
αd = (αdx, αdy, αdz) = (x,y,z)
αdz = z
α = z/dz
x = αdx = zdx/dz
y = αdy = zdy/dz

Share this post


Link to post
Share on other sites
This might not be the best way but it should work


float z = 10;
float t = (z - Start.z ) / (End.z - Start.z);

if ( t < 0.0f || t > 1.0f )
{
// not a valid z value
}

x = Start.x + t * (End.x - Start.x);
y = Start.y + t * (End.y - Start.y);



I just wrote this and it's untested.
hope that helps

Share this post


Link to post
Share on other sites
you realy should check if End.z = Start.z
If they are the same then you will get a division by zero (which is bad)
( if you use my code sample that is )

Share this post


Link to post
Share on other sites
Quote:
Original post by glSmurf
you realy should check if End.z = Start.z
If they are the same then you will get a division by zero (which is bad)
( if you use my code sample that is )


Yes I thought of that, but thanks for the reminder.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!