Help with AlignToVector

Blitz3D Forums/Blitz3D Programming/Help with AlignToVector

Roland(Posted 2006) [#1]
hey all,

I'm trying to figure out how to transform an object's rotation into a vector using tformvector(), then to convert that vector back to rotation to rotate a different object. This is probably useless within blitz, but necessary to work with another program that i am using.

Unfortunately, when I try to do this, I find that the rotations do not match. I wrote this little program to test it out, and you can see if you run it that they deviate. Can you help me figure out what I'm doing wrong?

Thanks,
roland


Graphics3D 640,480,16,2
SetBuffer BackBuffer()
SeedRnd MilliSecs()

Global lx#
Global ly#
Global lz#

Global cam = CreateCamera()
PositionEntity cam,0,10,-20
Global l=CreateLight()
RotateEntity l,0,0,0

;primary object (source of rotation)
cyl = CreateCylinder()
ScaleEntity cyl,.2,4,.2
PositionEntity cyl,-10,4,0

;secondary object that we'll try to make match "cyl"
cyl2 = CreateCylinder()
ScaleEntity cyl2,.2,4,.2
PositionEntity cyl2,10,4,0
EntityColor cyl2,255,0,0


While Not KeyHit(1)

Delay 100

TurnEntity cyl,.1,.1,.1

;call function to transform object's rotation to vector
lightangle(cyl)

;align cyl2 to cyl's vector
AlignToVector cyl2,lx,ly,lz,2

UpdateWorld()

RenderWorld()

Text 0,0,EntityPitch(cyl)+" "+lx+" "+EntityPitch(cyl2)
Text 0,12,EntityYaw(cyl)+" "+ly+" "+EntityYaw(cyl2)
Text 0,24,EntityRoll(cyl)+" "+lz+" "+EntityRoll(cyl2)

Flip

Wend


Function lightangle(light)
	TFormVector(0,1,0,light,0)
	lx = TFormedX()
	ly = TFormedY()
	lz = TFormedZ()
End Function



DJWoodgate(Posted 2006) [#2]
Try aligning on two axis, Y and Z. This will work most of the time but it is not perfect. Occasionally things are still left slightly out of kilter, perhaps due to floating point rounding issues.


Roland(Posted 2006) [#3]
Hey David, thanks for the quick reply... Would you mind showing me how to align to two axis?

thanks!

roland


Stevie G(Posted 2006) [#4]
Would recommend aligning both x and z axis like so ....

Graphics3D 640,480,16,2
SetBuffer BackBuffer()
SeedRnd MilliSecs()

Global lx#
Global ly#
Global lz#

Global cam = CreateCamera()
PositionEntity cam,0,10,-20
Global l=CreateLight()
RotateEntity l,0,0,0

;primary object (source of rotation)
cyl = CreateCylinder()
ScaleEntity cyl,.2,4,.2
PositionEntity cyl,-10,4,0

;secondary object that we'll try to make match "cyl"
cyl2 = CreateCylinder()
ScaleEntity cyl2,.2,4,.2
PositionEntity cyl2,10,4,0
EntityColor cyl2,255,0,0


While Not KeyHit(1)

Delay 100

TurnEntity cyl,.1,.1,.1

;call function to transform object's rotation to vector
lightangle(cyl, 1, 0, 0 )
;align cyl2 to cyl's vector
AlignToVector cyl2,lx,ly,lz,1
lightangle( cyl, 0, 0, 1 )
AlignToVector cyl2,lx,ly,lz,3

UpdateWorld()

RenderWorld()

Text 0,0,EntityPitch(cyl)+" "+lx+" "+EntityPitch(cyl2)
Text 0,12,EntityYaw(cyl)+" "+ly+" "+EntityYaw(cyl2)
Text 0,24,EntityRoll(cyl)+" "+lz+" "+EntityRoll(cyl2)

Flip

Wend


Function lightangle(light , x#, y#, z#)
	TFormVector(x,y,z,light,0)
	lx = TFormedX()
	ly = TFormedY()
	lz = TFormedZ()
End Function



DJWoodgate(Posted 2006) [#5]
Its the same as you are doing at the moment for the Y axis, you just do it for the Z axis as well.
In a nutshell:
Tformvector 0,1,0,entity1,0
Aligntovector entity2,tformedx(),tformedy(),tformedz(),2
Tformvector 0,0,1,entity1,0
Aligntovector entity2,tformedx(),tformedy(),tformedz(),3


Edit. Ah StevieG beat me too it. Looks like he has a slightly better approach although aligning Y and Z seems to give similar values?


Roland(Posted 2006) [#6]
Hey guys, those both work great, but after looking at what I need, I'm not sure I will be able to have both of those values to transform.

I'm using my blitz app to export and import from a new free rendering engine called indigo for rendering scenes. Indigo's skylight uses a single 3-component vector for indicating the sun's direction... Here's the explanation from indigo's site:

"The sundir element defines the 3-vector direction towards the sun. the Z axis is up, e.g. (0,0,1) places the sun directly overhead."

So when I load in a scene that's defined in indigo's format, I'll only have that one set of vectors. How can I rotate the object representing the sunlight work with those? I think that I can use the exact code you supplied for some other features, but I don't know if it will apply directly to this case... will it?

thanks so much for your help on this -- please let me know if you have some more ideas!

roland


Roland(Posted 2006) [#7]
oh-- i guess i should explain that the "lightangle" function in my first post that transforms the light along the zaxis was my attempt to create that "sundir" direction for export. If this method was incorrect, that would probably be a hindrance here as well :) Thanks again,

Roland


Stevie G(Posted 2006) [#8]
The sundir vector will need to first be amended to reflect blitz axiss'.

So ... (0,0,1 ) = ( 0,1,0 )

From this you could rotate the light entity correctly using vectorpitch / yaw. Probably not necessary to touch the roll axis. Note that although the sun is above it needs to be pointing down, hence the -ve.

Yaw# = vectoryaw( 0 , -1 , 0 )
Pitch# = vectorpitch( 0,-1,0 )
Rotateentity Light, Pitch, Yaw, 0



DJWoodgate(Posted 2006) [#9]
Well they are right you only need one vector to determine the sun direction. You want the inverse of this vector to point the light in the right direction. So align -lx,-ly,-lz. I am not sure what the implications of +Z being up are at the moment. Maybe you align to -lx,-lz,-ly instead of -lx,-ly,-lz.

hah. Stevie beat me to it again, and his reply makes more sense to boot.


Roland(Posted 2006) [#10]
Woa, vectoryaw, etc...? I had no idea these commands existed. Are they undocumented?

I was aware of the axis difference, and forgot to mention it. I think that using these vectoryaw and vectorpitch options will be exactly what i need... thanks guys!

cheers,
roland


Roland(Posted 2006) [#11]
o.k., I think that this works, in principle. I found that I had to add 90 to the "Pitch" variable in order to get the rotations to match visually. But they don't seem to match by the numbers. Does this look like the correct implementation to you guys? I realize that I'll have to make these negative for the light vector, but other than that, is this right?


Graphics3D 640,480,16,2
SetBuffer BackBuffer()
SeedRnd MilliSecs()

Global lx#
Global ly#
Global lz#

Global cam = CreateCamera()
PositionEntity cam,0,10,-20
Global l=CreateLight()
RotateEntity l,0,0,0

cyl = CreateCone()
ScaleEntity cyl,.2,4,.2
PositionEntity cyl,-1,4,0

cyl2 = CreateCone()
ScaleEntity cyl2,.2,4,.2
PositionEntity cyl2,1,4,0
EntityColor cyl2,255,0,0


While Not KeyHit(1)

;Delay 1000

TurnEntity cyl,.5,.5,.5

lightangle(cyl)


Yaw# = VectorYaw( lx ,ly, lz )
Pitch# = VectorPitch(lx,ly,lz )

RotateEntity cyl2,Pitch+90,Yaw,0

UpdateWorld()

RenderWorld()

Text 0,0,EntityPitch(cyl)+" "+lx+" "+EntityPitch(cyl2)
Text 0,12,EntityYaw(cyl)+" "+ly+" "+EntityYaw(cyl2)
Text 0,24,EntityRoll(cyl)+" "+lz+" "+EntityRoll(cyl2)

Flip

Wend


Function lightangle(light)
	TFormVector(0,1,0,light,0)
	lx = TFormedX()
	ly = TFormedY()
	lz = TFormedZ()
End Function



thanks again for all your help with this. Nothing beat's blitz's community!

best,
roland


Sir Gak(Posted 2006) [#12]
Scallop the lights.