 November 25, 2020, 05:32:38 AM

### Author Topic: [bb] transform point onto plane by Warner [ 1+ years ago ]  (Read 770 times)

#### BlitzBot

• Jr. Member
•  • Posts: 1 ##### [bb] transform point onto plane by Warner [ 1+ years ago ]
« on: June 29, 2017, 12:28:39 AM »
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[/url]

Code :
Code: BlitzBasic
1. ;input  = (X,Y) on plane
2. ;output = Transformed point
3. Global resultx#, resulty#, resultz#
4. Function PointOntoPlane(x#, y#, z#, nx#, ny#, nz#, d#)
5.
6.         a# = 0
7.         c# = ATan2(nx, ny)
8.         b# = ATan2(nz, Sqr(nx*nx+ny*ny))
9.
10.         ;apply yaw to point
11.         kx# = (Cos(-c) * z) - (Sin(-c) * x)
12.         ky# = y
13.         kz# = (Sin(-c) * z) + (Cos(-c) * x)
14.
15.     ;apply pitch to point
16.         jx# = kx
17.         jy# = (Cos(-b) * ky) - (Sin(-b) * kz)
18.         jz# = (Sin(-b) * ky) + (Cos(-b) * kz)
19.
20.     ;apply roll to point
21.         ix# = (Cos(-a) * jx) - (Sin(-a) * jy)
22.         iy# = (Sin(-a) * jx) + (Cos(-a) * jy)
23.         iz# = jz
24.
25.     ;apply plane offset
26.         resultx# = ix# - nx*d
27.         resulty# = iy# - ny*d
28.         resultz# = iz# - nz*d
29.
30. End Function
31. ;Resources:
32. ;http://www.geocities.com/siliconvalley/2151/math3d.html
33. ;http://www.gamedev.net/community/forums/topic.asp?topic_id=399701

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.
Code: [Select]
`;input  = (X,Y) on plane;output = Transformed pointGlobal 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*dEnd 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)