Code archives/3D Graphics - Maths/transform point onto plane

This code has been declared by its author to be Public Domain code.

Download source code

transform point onto plane by Warner2009
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:
http://www.gamedev.net/community/forums/topic.asp?topic_id=399701
;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

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

thanks


Warner2009
Yes, the following rotates a plane, and transforms a number of points onto it.



Stevie G2009
Why not use tformpoint instead?


BlitzSupport2009

Why not use tformpoint instead?



Even if this works out the same, it's good to have the underlying algorithm -- it'd port to BlitzMax, for example.


Code Archives Forum