Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Calculate position along 3D line

Author
Message
Rudolpho
20
Years of Service
User Offline
Joined: 28th Dec 2005
Location: Sweden
Posted: 16th Aug 2012 17:34
I know this is simple stuff and yet I've been scratching my head about it for too long.

I have a pair of 3D coordinates that make up a line between them in 3D space. Now I need to find the position along this line that is a set distance from one of these end points.

Do anybody know how to achieve this? I suppose it could be done using either trigonometry or vectors.


Thanks for any help,
Rudolpho


"Why do programmers get Halloween and Christmas mixed up?"
Ortu
DBPro Master
18
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 16th Aug 2012 18:52 Edited at: 16th Aug 2012 18:53
there are a couple of ways, some depend on the environment. is this free open space where the line can go in any direction?

the easy way is to use the 'point object', 'move object' commands.

if it is on a surface, use sin/cos

going from point 1 towards point 2, first get the angle.

you could do it with maths, but assuming you are trying to move/position an object along this line we can take a short cut:

point object someobject,point2.x,point2.y,point2.z
Yangle# = object angle y(someobject)

then calculate the new position:
newx# = point1.x + sin(Yangle#)*distance#
newz# = point1.z + cos(Yangle#)*distance#

the new Y position will need to get the ground height at newx#,newz# if it is on a terrain or something, if it's just a flat plane then there is no change.

Dar13
18
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 16th Aug 2012 20:56
Here's some psuedo-code:


This code isn't tested in any sense of the word, though I think my math is correct.

P.S. Phaelax, could you double-check this?

The Weeping Corpse
14
Years of Service
User Offline
Joined: 19th Sep 2011
Location: United Kingdom
Posted: 16th Aug 2012 20:57
ok, this is pseudo code not db code but the math is correct
and uses simple linear interpolation

` sx, sy, sz = start of line
` ex, ey, ez = end of line
` dist = distance along line, so for example:
` 0 = start of line
` 1 = end of line
` 0.5 = mid line etc etc
` 0.75 = 75 percent along line
` 0.25 = 25 percent along line

function CalcPositionAlongLine3D(sx, sy, sz, ex, ey, ez, dist)
dim dx, dy, dz

dx = ex - sx
dy = ey - sy
dz = ez - sz

output.x = sx + (dx * dist)
output.y = sy + (dy * dist)
output.z = sz + (dz * dist)
end function

MadBit
VIP Member
Gold Codemaster
17
Years of Service
User Offline
Joined: 25th Jun 2009
Location: Germany
Posted: 16th Aug 2012 21:20
If you are using my eXtendet Rotation plugin than you can write.


point_at_line is the absolut position in world space.
unit_at_line are the units from start towards end position. If unit_at_line greater as the distance between start and end position, then the calculated point_at_line are behint the end point.

With Computers you can solve Problems that you have never befor.
Share your knowledge. It's a way to achieve immortality. (Tenzin Gyatso)
Fallout
23
Years of Service
User Offline
Joined: 1st Sep 2002
Location: Basingstoke, England
Posted: 16th Aug 2012 21:39 Edited at: 16th Aug 2012 21:40
Here you go:



Le Verdier
14
Years of Service
User Offline
Joined: 10th Jan 2012
Location: In the mosh-pit
Posted: 16th Aug 2012 22:02 Edited at: 17th Aug 2012 19:27
Vector version:


Assuming that the vectors are created and initialized with coords, result in the vector 3....

Rudolpho
20
Years of Service
User Offline
Joined: 28th Dec 2005
Location: Sweden
Posted: 17th Aug 2012 01:23
Ah, thanks for the suggestions. I managed to get it working eventually using vector with scalar multiplication, however some of your versions look much cleaner so I might try switching for one of those tomorrow


@Ortu: yeah, I know about that, however this time I needed to do it with only maths. Wouldv'e posted in the Programming Talk forum instead but... well, let's face it, the reply rate over there is about 1 per two or three days.


"Why do programmers get Halloween and Christmas mixed up?"
MrValentine
AGK Backer
15
Years of Service
User Offline
Joined: 5th Dec 2010
Playing: FFVII
Posted: 17th Aug 2012 11:35
I had a corny method using Dark AI pop into my head, but I guess it depends how you wanted to get the point...

Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 24th Aug 2012 11:26 Edited at: 24th Aug 2012 11:26
Quote: "
Here's some psuedo-code:


This code isn't tested in any sense of the word, though I think my math is correct.

P.S. Phaelax, could you double-check this?
"


Looks good, same method I would've used.



@The Weeping Corpse, your method wouldn't be accurate. That looks more like linear interpolation for a single dimension. You would need to normalize the difference first.


If you didn't want to use vectors, here's another clean way to do it. (hopefully I didn't make any typos)



"You're not going crazy. You're going sane in a crazy world!" ~Tick
The Weeping Corpse
14
Years of Service
User Offline
Joined: 19th Sep 2011
Location: United Kingdom
Posted: 25th Aug 2012 10:20 Edited at: 25th Aug 2012 10:22
@Phaelax

You are wrong.

It uses the parametric equation of the given line in all 3 dimensions.

It's perfectly accurate AND it doesn't require a sqrt, which is why I use it.

Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 27th Aug 2012 18:12
My bad, I overlooked what value range you were using for dist.

"You're not going crazy. You're going sane in a crazy world!" ~Tick

Login to post a reply

Server time is: 2026-07-07 10:11:51
Your offset time is: 2026-07-07 10:11:51