[bb] transform point onto plane by Warner [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:39

Previous topic - Next topic

BlitzBot

Title : transform point onto plane
Author : Warner
Posted : 1+ years ago

Description : transforms a point (x, y, 0) on a plane to real world coords.
The plane is defined by (nx,ny,nz,d), or: ax + by + cz + d = 0
where (a,b,c)=(nx,ny,nz)

The code is not optimised, and I could not solve the 'roll' problem yet. At certain situations, the transformation turns 180 degrees upside down:
<a href="http://www.gamedev.net/community/forums/topic.asp?topic_id=399701" target="_blank">http://www.gamedev.net/community/forums/topic.asp?topic_id=399701</a>


Code :
Code (blitzbasic) Select
;input  = (X,Y) on plane
;output = Transformed point
Global resultx#, resulty#, resultz#
Function PointOntoPlane(x#, y#, z#, nx#, ny#, nz#, d#)

a# = 0
c# = ATan2(nx, ny)
b# = ATan2(nz, Sqr(nx*nx+ny*ny))

;apply yaw to point
kx# = (Cos(-c) * z) - (Sin(-c) * x)
ky# = y
kz# = (Sin(-c) * z) + (Cos(-c) * x)

    ;apply pitch to point
jx# = kx
jy# = (Cos(-b) * ky) - (Sin(-b) * kz)
jz# = (Sin(-b) * ky) + (Cos(-b) * kz)

    ;apply roll to point
ix# = (Cos(-a) * jx) - (Sin(-a) * jy)
iy# = (Sin(-a) * jx) + (Cos(-a) * jy)
iz# = jz

    ;apply plane offset
resultx# = ix# - nx*d
resulty# = iy# - ny*d
resultz# = iz# - nz*d

End Function
;Resources:
;http://www.geocities.com/siliconvalley/2151/math3d.html
;http://www.gamedev.net/community/forums/topic.asp?topic_id=399701


Comments :


patmaba(Posted 1+ years ago)

 Hi, have you a small code to see your funtion in action, please ?thanks


Warner(Posted 1+ years ago)

 Yes, the following rotates a plane, and transforms a number of points onto it.
;input  = (X,Y) on plane
;output = Transformed point
Global resultx#, resulty#, resultz#
Function PointOntoPlane(x#, y#, z#, nx#,ny#,nz#,d#)

a# = 0
c# = ATan2(nx, ny)
b# = ATan2(nz, Sqr(nx*nx+ny*ny))

        ;apply pitch to point
ix# = x
iy# = (Cos(-b) * y) - (Sin(-b) * z)
iz# = (Sin(-b) * y) + (Cos(-b) * z)

        ;apply roll to point
jx# = (Cos(-a) * ix) - (Sin(-a) * iy)
jy# = (Sin(-a) * ix) + (Cos(-a) * iy)
jz# = iz

        ;apply plane offset
resultx# = jx# - nx*d
resulty# = jy# - ny*d
resultz# = jz# - nz*d

End Function
;Resource: <a href="http://www.geocities.com/siliconvalley/2151/math3d.html" target="_blank">http://www.geocities.com/siliconvalley/2151/math3d.html</a>

;------------------------------------------------------------------------------------------------------------------------------------
; EXAMPLE
;------------------------------------------------------------------------------------------------------------------------------------

;setup 3D
Graphics3D 800, 600, 0, 2
SetBuffer BackBuffer()

;create camera
MoveEntity CreateCamera(), 0, 0, -5

Dim sph(9)

;create 10 spheres
For i = 0 To 9
sph(i) = CreateSphere()
ScaleEntity sph(i), 0.1, 0.1, 0.1 ;make sphere smaller
EntityColor sph(i), 255, 0, 0     ;make sphere red
Next

;basically, the plane is rotated around the y-axis
;and the spheres are places accordingly
Repeat

;rotate plane

angle = angle + 1  ;increase 'angle'
nx# = Cos(angle)   ;calculate normal = vector pointing forward from plane
ny# = 0
nz# = Sin(angle)
d# = 0             ;this is the distance of the plane from (0, 0, 0)

;place spheres
For i = 0 To 9

x# = Cos(i * 36) ;calculate position of sphere in 2D space
y# = Sin(i * 36)

PointOntoPlane(x, y, 0, nx,ny,nz,d)   ;transform position from 2D space to 3D space
PositionEntity sph(i), resultx, resulty, resultz  ;apply position

Next


RenderWorld
Flip

;esc=exit
Until KeyHit(1)

End



Stevie G(Posted 1+ years ago)

 Why not use tformpoint instead?


BlitzSupport(Posted 1+ years ago)

 <div class="quote"> Why not use tformpoint instead? </div>Even if this works out the same, it's good to have the underlying algorithm -- it'd port to BlitzMax, for example. [/i]