Right, rather than derail the 'XEdition' thread I'll start a new topic. The aim - to create a working AlignToVector function.
Here's my attempt:
And example code (BMX):
Example code (B3D):
The bmx version must work the same as the b3d version. The above version works some of the time, but some vector inputs won't work - such as x=0,y=-1,0,z=0.0,axis=2.
Also, don't forget that this command can be used twice or three times to combine the axis rotations.
Here's my attempt:
Function AlignToVector(vx#,vy#,vz#,axis,rate#=1.0) Local pitch#=Abs(EntityPitch()) Local yaw#=Abs(EntityYaw()) Local roll#=Abs(EntityRoll()) Select axis Case 1 Local dest_yaw#=Abs(ATan2(vz#,vx#)) Local dest_roll#=Abs(ATan2(vy#,vx#)) yaw#=UpdateValue#(yaw#,dest_yaw#,rate#) roll#=UpdateValue#(roll#,dest_roll#,rate#) Case 2 Local dest_pitch#=Abs(ATan2(vz#,vy#)) Local dest_roll#=Abs(-ATan2(vx#,vy#)) pitch#=UpdateValue#(pitch#,dest_pitch#,rate#) roll#=UpdateValue#(roll#,dest_roll#,rate#) Case 3 Local dest_pitch#=Abs(-ATan2(vy#,vz#)) Local dest_yaw#=Abs(-ATan2(vx#,vz#)) pitch#=UpdateValue#(pitch#,dest_pitch#,rate#) yaw#=UpdateValue#(yaw#,dest_yaw#,rate#) End Select Self.RotateEntity pitch#,yaw#,roll# End Function Function UpdateValue#(current#,destination#,rate#) 'rate#=1.0/rate# current#=current#+((destination#-current#)*rate#) Return current# End Function
And example code (BMX):
Import "../MiniB3D.bmx" Graphics3D 640,480 ' set this to 1, 2 Or 3 axis=1 ' set vector vx#=0.0 vy#=1.0 vz#=0.0 camera=CreateCamera() PositionEntity camera,0,2,-5 RotateEntity camera,15,0,0 light=CreateLight() RotateEntity light,90,0,0 cone=CreateCone() xaxis=CreateCylinder(8,True,cone) RotateMesh xaxis,0,0,90 ScaleMesh xaxis,5.0,0.2,0.2 EntityColor xaxis,127,0,0 yaxis=CreateCylinder(8,True,cone) ScaleMesh yaxis,0.2,5.0,0.2 EntityColor yaxis,0,127,0 zaxis=CreateCylinder(8,True,cone) RotateMesh zaxis,90,0,0 ScaleMesh zaxis,0.2,0.2,5.0 EntityColor zaxis,0,0,127 marker=CreateSphere() ScaleEntity marker,0.2,0.2,0.2 PositionEntity marker,vx#,vy#,vz# ' change color of marker depending on axis selected Select axis Case 1 EntityColor marker,255,0,0 Case 2 EntityColor marker,0,255,0 Case 3 EntityColor marker,0,0,255 End Select While Not KeyDown(KEY_ESCAPE) AlignToVector cone,vx,vy,vz,axis,0.01 RenderWorld glColor3f 255,0,0 Text 0,0,"X" glColor3f 0,255,0 Text 0,20,"Y" glColor3f 0,0,255 Text 0,40,"Z" Flip Wend End Function Text(x,y,text$) ' reset texture matrix glMatrixMode(GL_TEXTURE) glLoadIdentity() ' set active texture to texture 0 so gldrawtext will work correctly glActiveTextureARB(GL_TEXTURE0) glClientActiveTextureARB(GL_TEXTURE0) ' enable texture 2D glEnable(GL_TEXTURE_2D) glDisable(GL_LIGHTING) GLDrawText text$,x,y glEnable(GL_LIGHTING) ' disable texture 2D - needed as gldrawtext enables it, but doesn't disable after use glDisable(GL_TEXTURE_2D) End Function
Example code (B3D):
Graphics3D 640,480 ; set this to 1, 2 or 3 axis=3 ; set vector vx#=0.0 vy#=1.0 vz#=0.0 camera=CreateCamera() PositionEntity camera,0,2,-5 RotateEntity camera,15,0,0 light=CreateLight() RotateEntity light,90,0,0 cone=CreateCone() xaxis=CreateCylinder(8,True,cone) RotateMesh xaxis,0,0,90 ScaleMesh xaxis,5.0,0.2,0.2 EntityColor xaxis,127,0,0 yaxis=CreateCylinder(8,True,cone) ScaleMesh yaxis,0.2,5.0,0.2 EntityColor yaxis,0,127,0 zaxis=CreateCylinder(8,True,cone) RotateMesh zaxis,90,0,0 ScaleMesh zaxis,0.2,0.2,5.0 EntityColor zaxis,0,0,127 marker=CreateSphere() ScaleEntity marker,0.2,0.2,0.2 PositionEntity marker,vx#,vy#,vz# ; change color of marker depending on axis selected Select axis Case 1 EntityColor marker,255,0,0 Case 2 EntityColor marker,0,255,0 Case 3 EntityColor marker,0,0,255 End Select While Not KeyDown(1) AlignToVector cone,vx,vy,vz,axis,0.01 RenderWorld Color 255,0,0 Text 0,0,"X" Color 0,255,0 Text 0,20,"Y" Color 0,0,255 Text 0,40,"Z" Flip Wend End
The bmx version must work the same as the b3d version. The above version works some of the time, but some vector inputs won't work - such as x=0,y=-1,0,z=0.0,axis=2.
Also, don't forget that this command can be used twice or three times to combine the axis rotations.