Jump to content
  • Advertisement
Sign in to follow this  
leggyguy

Find the point of a line x away from end point of line

This topic is 4519 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

I have a line in my little space game, described by the vertices startPosition and endPosition. My ship travels along the vector endPosition - startPosition, and it stops when it gets to endPosition. Now, my endPosition is the position of a planet, (the center of it) and I dont want my ship to actually hit the planet and travel to the centre. Instead, I want it to travel as far as it can on this vector until it gets to planetRadius * 2 from the centre of the planet. In effect, if a planet has radius of 5.0f, I want the ship to travel along the vector nutil it gets to 10.0f from the planet's position vert. Now, I am not a huge math wiz, and I am not sure how to calculate this. Could someone let me know, in layman's terms :), how I would get this point?

Share this post


Link to post
Share on other sites
Advertisement
If your planets position is vPlanet and your ship is at vShip and the planet radius is X

The point you are looking for vMysteryPoint! will be...


vDirection = (vShip-vPlanet).unit();
// this is a vector from the center of the planet,
//pointing at your ship. It is a unit vector (length set to be 1).

vMysteryPoint = vPlanet + X*vDirection;
// so we take our starting point, the middle of the planet,
// and move along the direction we found
// above X units. That will give you the point X units from the center
// of your planet in the direction of the ship.

Share this post


Link to post
Share on other sites
Theres a couple ways to accomplish this.
If you want to be really simple about it, each time you update the ship's position, you could just check the distance from it's current coordinate to the planet center, and if it is less than that radius, Stop.

If you want the Exact coordinate of that point, youre basically intersecting a line with a circle. This can be solved with a system of equations (I'm not going to though but its an approach you can work out on paper)

Last, since you said that the vector endpoint was the center of the planet and the center of your stop position radius: you know that the stopradius is the same as the distance remaining along the vector when the ship stops.
By comparing that dist-remaining to the total length of the vector, you can get a ratio which you can then scale the entire vector by, so the new scaled vector will have an endpoint where the ship should stop at.

length is length of yourvector
shortvector=yourvector*(length-stopradius)/length
startpoint+shortvector= endpoint

not sure how you actually do your ship's motion, but from that you can get either the new vector it will travel, or the specific stopping coordinates
so it should be enough...

EDIT:
ok, CombatWombat got in first, actually, his solution is a lot more elegant

Share this post


Link to post
Share on other sites
Thanks guys.

Yep I used combatwombat's method before I got to read your post, Hap. It works fine, and using the normal makes so much sense, I feel mad I didn't think of it. I was thinking it was so much more complicated.

Thanks for the help.

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!