January 20, 2021, 09:30:40 AM
Welcome,
Guest
. Please
login
or
register
.
Did you miss your
activation email
?
1 Hour
1 Day
1 Week
1 Month
Forever
Login with username, password and session length
Home
Forum
Help
Search
Gallery
Login
Register
SyntaxBomb - Indie Coders
»
Languages & Coding
»
Blitz Code Archives
»
3D Graphics - Maths
»
[bb] Point distance to a Line by Danny [ 1+ years ago ]
« previous
next »
Print
Pages: [
1
]
Go Down
Author
Topic: [bb] Point distance to a Line by Danny [ 1+ years ago ] (Read 1454 times)
BlitzBot
Jr. Member
Posts: 1
[bb] Point distance to a Line by Danny [ 1+ years ago ]
«
on:
June 29, 2017, 12:28:43 AM »
Title :
Point distance to a Line
Author :
Danny
Posted :
1+ years ago
Description :
Given 2 3d points A and B (that make up for a line-segment) and a point P; this function will return the shortest distance between that point P and the line.
I use this for my vertex lighting system where I have a 'linear light' (a light defined by 2 3d coordinates) to check if a vertex (point P) is 'within range' of that light, and it's distance determines it's fall-off.
You could also use this to determin if the player is 'too close' to a laser beam for example (or some other linear-object) like: IF 'player-distance-to-line' <= 'heath-range-of-beam' THEN 'add damage to player'
Danny
Code :
Code: BlitzBasic
Function
PointDistanceToLine#
(
ax#,ay#,az#, bx#,by#,bz#, px#,py#,pz#
)
;| Calculates the shortest distance between a point P(xyz) and a line segment defined by A(xyz) and B(xyz) - danny.
;get the length of each side of the triangle ABP
ab# =
Sqr
(
(
bx-ax
)
*
(
bx-ax
)
+
(
by-ay
)
*
(
by-ay
)
+
(
bz-az
)
*
(
bz-az
)
)
bp# =
Sqr
(
(
px-bx
)
*
(
px-bx
)
+
(
py-by
)
*
(
py-by
)
+
(
pz-bz
)
*
(
pz-bz
)
)
pa# =
Sqr
(
(
ax-px
)
*
(
ax-px
)
+
(
ay-py
)
*
(
ay-py
)
+
(
az-pz
)
*
(
az-pz
)
)
;get the triangle's semiperimeter
semi# =
(
ab+bp+pa
)
/
2.0
;get the triangle's area
area# =
Sqr
(
semi *
(
semi-ab
)
*
(
semi-bp
)
*
(
semi-pa
)
)
;return closest distance P to AB
Return
(
2.0
*
(
area/ab
)
)
End Function
Comments :
Craig H. Nisbet(Posted 1+ years ago)
That works great! Thanks for posting!
puki(Posted 1+ years ago)
<div class="quote"> I use this for my vertex lighting system </div>Clear off "Nisbet" - this is clearly my code."Danny" - old buddy/pal.You forgot to post the vertex lighting system - no need to worry - just email it to me - I'll check it over.
Logged
Print
Pages: [
1
]
Go Up
« previous
next »
SyntaxBomb - Indie Coders
»
Languages & Coding
»
Blitz Code Archives
»
3D Graphics - Maths
»
[bb] Point distance to a Line by Danny [ 1+ years ago ]
SimplePortal 2.3.6 © 2008-2014, SimplePortal