Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Finding coordinates along a path between two points

Jexx Rayner
Registered User
Join date: 30 Jun 2006
Posts: 16
02-24-2007 12:28
Hi again! I'm getting closer to having all of the components I need to be able to have an object slide between two points, eg a sphere slide along a rod from one end to the other. I can script the basics of getting the sphere to move to endpta and then move to endptb in one quick move, but I'd like to slow this down so it makes very steady small moves along the path. I visualize that via a timer where each call to the timer() event checks to see if the sphere is at its detination yet, and if not make another small move towards it.

So my questions is, given the following

integer num_segments = 30;
vector pos1 = <x,y,z>;
vector pos2 = <x,y,z>;

what techniques would I use to determine how to find the segments along the path from pos1 to pos2 so that I had num_segments of them in total? I suspect some kind of calculation involving the distance formula and dividing that by the num_segments but I am not sure yet. Any help would be greatly appreciated!

Jexx
Ged Larsen
thwarted by quaternions
Join date: 4 Dec 2006
Posts: 294
02-24-2007 12:51
From: Jexx Rayner
So my questions is, given the following

integer num_segments = 30;
vector pos1 = <x,y,z>;
vector pos2 = <x,y,z>;

what techniques would I use to determine how to find the segments along the path from pos1 to pos2 so that I had num_segments of them in total? I suspect some kind of calculation involving the distance formula and dividing that by the num_segments but I am not sure yet.


It's much easier than that.

Define: vector increment = (pos2 - pos1) / num_segments

Then, loop: integer segment from 0 to num_segments - 1

The location for each segment would be: pos1 + segment*increment

(Depending on what you are trying to do, you might have a fencepost issue, of whether you want an extra segment at the very end.)
_____________________
- LoopRez, flexi prim skirt generating tool
- LinkRez, a necklace chain generator
Lee Ponzu
What Would Steve Do?
Join date: 28 Jun 2006
Posts: 1,770
02-24-2007 12:52
This is a good question, and it has been answered before here in the forums. Try searching...
Also, a builder/scripter needs to be up to speed on basic vector geometry, and there are lots of web sites with good tutorials.

Given that...

if P1 and P2 are two vectors representing the start and end point, then

vector P3 = P2 - P1;

will be a vector from P1 to P2, like an arrow all the way from P1 to P2. You want a shorter arrow, try

vector P4 = (P2 - P1)/10;

P4 is only 1/10 of the length from P1 to P2. If you current position on the path from P1 to P2 is C, then C+P4 will be a little bit closer.

This is not the only way to do this, but I am trying to help you see the underlying logic, not just give you an answer.
Jexx Rayner
Registered User
Join date: 30 Jun 2006
Posts: 16
02-24-2007 16:31
I greatly appreciate your replies, and the exra background info as well ( been awhile since I have studied vector/matrices, etc ). I hope to have this stuff working soon, thanks for taking the time to answer my questions! :)
-Jexx