Page 1 of 1

TransformSprite() Z-Depth Basics

Posted: Thu Aug 25, 2016 10:11 pm
by J. Baker
This is just a simple explanation of the Z-Depth parameter for the TransformSprite() function. Hopefully this shall give someone basic understanding of this parameter. If you have more than two sides with different lengths or continuously moving XY values, it will take a higher mathematical skill than explained here.

The Z-Depth values fix irregular displayed sprites from looking distorted. If you remove the optional Z-Depth values from the TransformSprite() examples below, you will understand their purpose.

As you can see. The shortest side is divided by the longer/opposing side. The divided value is then used as the Z-Depth for the short side. The remaining two sides are of the same length and we need not worry about.

Currently Z-Depth only works in DirectX and not in OpenGL. So Windows only for now.

Code: Select all

TransformSprite(0,0,0,   128,32,2,   128,96,2,   0,128,0)

; X = 0, Y = 0, Z = 0  ____
;                      |   ____
;                      |       ____  X = 128, Y = 32, Z = 2
;                   1  |          /|
;                   2  |        /  |  6      ; 128 / 64 = 2
;                   8  |      /    |  4
;                      |    /      |
;                      |  /    ____  X = 128, Y = 96, Z = 2
;                      |/  ____
;                      ____
; X = 0, Y = 128, Z = 0


TransformSprite(0,0,0,   128,16,1.6,   128,96,1.6,   0,128,0)

; X = 0, Y = 0, Z = 0  ______
;                      |     ______  X = 128, Y = 16, Z = 1.6
;                      |           |
;                   1  |         / |
;                   2  |       /   |  8      ; 128 / 80 = 1.6
;                   8  |     /     |  0
;                      |   /       |
;                      | /     ____  X = 128, Y = 96, Z = 1.6
;                      |   ____
;                      ____
; X = 0, Y = 128, Z = 0


TransformSprite(224,0,8,   288,0,8,   512,512,0,   0,512,0)

;                                64         
;        X = 224, Y = 0, Z = 8  ____  X = 288, Y = 0, Z = 8
;                              /   /\
;                             /      \
;                            /  /     \
;                           /          \     ; 512 / 64 = 8
;                          / /          \
;                         /              \   
;                        __________________
; X = 0, Y = 512, Z = 0          512        X = 512, Y = 512, Z = 0

Re: TransformSprite() Z-Depth Basics

Posted: Fri Aug 26, 2016 2:30 am
by electrochrisso
I will put this with my PB Docs, thanks for sharing. :)

Re: TransformSprite() Z-Depth Basics

Posted: Fri Aug 26, 2016 2:39 am
by J. Baker
electrochrisso wrote:I will put this with my PB Docs, thanks for sharing. :)
No problem. ;)

Re: TransformSprite() Z-Depth Basics

Posted: Fri Aug 26, 2016 6:50 am
by STARGÅTE
I think it's a good place to post an example:

Code: Select all

Procedure HyperTransformSprite(Sprite.i, Width.f, Height.f, Depth.f, Roll.f, Yaw.f, Pitch.f, AlignX.f=0.5, AlignY.f=0.5)
   
   Protected CosZ.f = Cos(Radian(Roll)), CosY.f = Cos(Radian(Yaw)), CosX.f = Cos(Radian(Pitch))
   Protected SinZ.f = Sin(Radian(Roll)), SinY.f = Sin(Radian(Yaw)), SinX.f = Sin(Radian(Pitch))
   
   Protected A11.f =  CosY*CosZ,                A12.f = -CosY*SinZ,                A13.f =  SinY
   Protected A21.f =  SinX*SinY*CosZ+CosX*SinZ, A22.f = -SinX*SinY*SinZ+CosX*CosZ, A23.f = -SinX*CosY
   Protected A31.f = -CosX*SinY*CosZ+SinX*SinZ, A32.f =  CosX*SinY*SinZ+SinX*CosZ, A33.f =  CosX*CosY
   
   Protected U0.f = -AlignX*Width,  U1.f = (1-AlignX)*Width
   Protected V0.f = -AlignY*Height, V1.f = (1-AlignY)*Height
   
   Protected Z1.f = U0*A31 + V0*A32 + Depth
   Protected Y1.f = ( U0*A21 + V0*A22 ) * Depth / Z1
   Protected X1.f = ( U0*A11 + V0*A12 ) * Depth / Z1
   Protected Z2.f = U1*A31 + V0*A32 + Depth
   Protected Y2.f = ( U1*A21 + V0*A22 ) * Depth / Z2
   Protected X2.f = ( U1*A11 + V0*A12 ) * Depth / Z2
   Protected Z3.f = U1*A31 + V1*A32 + Depth
   Protected Y3.f = ( U1*A21 + V1*A22 ) * Depth / Z3
   Protected X3.f = ( U1*A11 + V1*A12 ) * Depth / Z3
   Protected Z4.f = U0*A31 + V1*A32 + Depth
   Protected Y4.f = ( U0*A21 + V1*A22 ) * Depth / Z4
   Protected X4.f = ( U0*A11 + V1*A12 ) * Depth / Z4
   
   TransformSprite(Sprite, X1, Y1, Z1, X2, Y2, Z2, X3, Y3, Z3, X4, Y4, Z4)
   
EndProcedure



InitSprite()
;InitNetwork()

UseJPEGImageDecoder()

Enumeration
   #Window
   #Sprite
EndEnumeration

OpenWindow(#Window, 0, 0, 800, 600, "ScreenTitle", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(#Window), 0, 0, WindowWidth(#Window), WindowHeight(#Window), 0, 0, 0)

SpriteQuality(#PB_Sprite_BilinearFiltering)

CatchSprite(#Sprite, ReceiveHTTPMemory("http://data.unionbytes.de/convergence_by_rahll.jpg"))

Repeat
   
   Repeat
      
      Select WindowEvent()
         Case #PB_Event_CloseWindow
            End
         Case #Null
            Break
      EndSelect
      
   ForEver
   
   ClearScreen(0)
   
   HyperTransformSprite(#Sprite, 160, 100, 300, ElapsedMilliseconds()/10, 0, 0)
   DisplaySprite(#Sprite, 200, 150)
   
   HyperTransformSprite(#Sprite, 160, 100, 300, 0, Sin(ElapsedMilliseconds()/1000)*70, 0)
   DisplaySprite(#Sprite, 600, 150)
   
   HyperTransformSprite(#Sprite, 160, 100, 300, 0, 0, Sin(ElapsedMilliseconds()/1000)*70)
   DisplaySprite(#Sprite, 200, 450)
   
   HyperTransformSprite(#Sprite, 160, 100, 300, ElapsedMilliseconds()/10, 0, -40, 1.0, 0.0)
   DisplaySprite(#Sprite, 600, 450)
   
   FlipBuffers()
   
ForEver
But as you have said, it doesn't work in OpenGL correctly.

Re: TransformSprite() Z-Depth Basics

Posted: Fri Aug 26, 2016 7:21 am
by J. Baker
STARGÅTE wrote:I think it's a good place to post an example:
It is. :D

Thanks for your more complex example! ;)