Rotate local xyz around global position (help)

Blitz3D Forums/Blitz3D Programming/Rotate local xyz around global position (help)

Danny(Posted 2007) [#1]
Hi all,

I'm in URGENT need of some mathematical assistance...

I've got a GLOBAL position XYZ and a GLOBAL rotation PYR. Then I have 3 LOCAL coordinates (ABC) that act as a 'local offset'.

My Question is: How do I transform those 3 local coordinates ABC around the global coordinates XYZ using the global rotation PYR and obtain those new coordinates?


I basically need what TFormPoint() does. But since I have to do this 'outside of blitz' (in visual basic) I can't use tFormPoint - or any other Blitz3D commands :((
Any help would be a total life safer! ;)

Thanks in advance,

Danny.


Danny(Posted 2007) [#2]
Ok I got this far - which seems to be good except for some reason my X-coordinate result is not ACCURATE! There's a mistake somewhere...

Does anyone know what I've done wrong here:
(I'm sure some of you are laughing out loud ;)pp )
;I want to rotate the 'local offset' around the global coordinate - by using the global rotation angles.

Graphics3D 800,600,0,2
Cls
Color 255,255,255

 ;original GLOBAL COORDINATE
 ox# = -239.475
 oy# =    2.07104
 oz# =  -81.7197

 ;original GLOBAL ROTATION
 xr# =    9.06404
 yr# = -145.508
 zr# =    0.00

 ;original LOCAL OFFSET
 offx# = 0.0
 offy# = 0.1
 offz# = 0.1


	;Transformation using TFormPoint()
	tmp = CreatePivot()
	PositionEntity tmp, ox,oy,oz, 1		; global pos
	RotateEntity tmp, xr,yr,zr, 1		; global rot
	TFormPoint offx,offy,offz, tmp, 0	; local offset to world coords
	
	ok1x# = TFormedX()
	ok1y# = TFormedY()
	ok1z# = TFormedZ()
	
	Color 160,160,160
	Print "Original coordinate: "+ox+", "+oy+", "+oz
	Print ""
	Color 255,255,255
	Print "TFormed coordintes : "+ok1x+", "+ok1y+", "+ok1z+"     = what I NEED!"
 	Print ""

	; ROTATE THE LOCAL OFFSET around GLOBAL ANGLE (attempt)
	x# = offx#
	y# = offy#
	z# = offz#
	
	;rotate offset around X axis
	newy# = y# * Cos#(xr#) - z# * Sin#(xr#)
	newz# = y# * Sin#(xr#) + z# * Cos#(xr#)
	y# = newy#
	z# = newz#

	;rotate offset around Y axis
	newx# = z# * Sin#(yr#) + x# * Cos#(yr#)
	newz# = z# * Cos#(yr#) - x# * Sin#(yr#)
	x# = newx#
	z# = newz#
	
	;rotate offset around Z axis
	newx# = x# * Cos#(zr#) - y# * Sin#(zr#)
	newy# = x# * Sin#(zr#) + y# * Cos#(zr#)
	x# = newx#
	y# = newy#
	
	;add global coordinates to rotated offset
	x# = x# + ox#
	y# = y# + oy#
	z# = z# + oz#

	Print "transformed coords : "+x+", "+y+", "+z+"     = what I GOT :("
	Print ""
	Color 255,50,50
	Print "INACCURACY (xyz)   : "+(ok1x-x)+", "+(ok1y-y)+", "+(ok1z-z)
	Print ""
	Print ""
	
WaitKey()
End


Thanks,
Danny


Danny(Posted 2007) [#3]
Nobody? :(

It's supposed to be a simple '3d rotation' but for some reason my x result is not perfecly accurate, whilst the rest does seem to be ok.

?!


Brendane(Posted 2007) [#4]
I think the y rotations are inverted so you need to use a neg y angle in your calcs :-

note the -yr

	;rotate offset around Y axis
	newx# = z# * Sin#(-yr#) + x# * Cos#(-yr#)
	newz# = z# * Cos#(-yr#) - x# * Sin#(-yr#)
	x# = newx#
	z# = newz#



Brendane(Posted 2007) [#5]
Oh, and you need to do the rotations in this order :-

z,x,y

	;rotate offset around Z axis
	newx# = x# * Cos#(zr#) - y# * Sin#(zr#)
	newy# = x# * Sin#(zr#) + y# * Cos#(zr#)
	x# = newx#
	y# = newy#
	
	;rotate offset around X axis
	newy# = y# * Cos#(xr#) - z# * Sin#(xr#)
	newz# = y# * Sin#(xr#) + z# * Cos#(xr#)
	y# = newy#
	z# = newz#


	;rotate offset around Y axis
	newx# = z# * Sin#(-yr#) + x# * Cos#(-yr#)
	newz# = z# * Cos#(-yr#) - x# * Sin#(-yr#)
	x# = newx#
	z# = newz#




Danny(Posted 2007) [#6]
THANKS A MILLION Brendane!!!! I could kiss you!! :)
(no worries, straight as an arrow!)

Yes your first comment already fixed 99% of the situation. I found out that in extreme (roll) angles there is still a tiny inaccuracy that's bugging me, but i'll try your last comment and keep me fingers crossed!!!

Thanks again mate! I owe you!

d.


Danny(Posted 2007) [#7]
Badabing! badaboom! (little victory dance going on here..)

Yep - switching the rotation order fixed it completely!!

Thanks again and again man!

Danny


Brendane(Posted 2007) [#8]
Err, yeah, did you see the football last night?

No worries. I did spot the slight floating point inaccuracies, but that's because of the way the transform is calculated internally, either through a matrix or a quat transform - these innacuracies will creep in.

Your alternative is to try to emulate the process using Mark's geometry code as closely as possible.

If the error is small enough for you to not worry about it's probably not worth it. From what I've seen it's usually much less than 0.001

Good luck!


Danny(Posted 2007) [#9]
Everything seems to be working super fine (now), the routine is actually done in visual basic to convert Blitz based camera data to Maya via MEL scripts.
VB spits out floats with 12 digits after the decimal point, so I'm not too worried about accuracy ;)

When I render a still in maya and overlay it with the blitz version in photoshop it's practially pixel perfect. Even with extreme lenses and angles it all matches nicely...

Cheers,
Danny.