TransformSprite() Z-Depth Basics

Advanced game related topics
User avatar
J. Baker
Addict
Addict
Posts: 2178
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

TransformSprite() Z-Depth Basics

Post 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
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef

Mac: 10.13.6 / 1.4GHz Core 2 Duo / 2GB DDR3 / Nvidia 320M
PC: Win 7 / AMD 64 4000+ / 3GB DDR / Nvidia 720GT


Even the vine knows it surroundings but the man with eyes does not.
User avatar
electrochrisso
Addict
Addict
Posts: 980
Joined: Mon May 14, 2007 2:13 am
Location: Darling River

Re: TransformSprite() Z-Depth Basics

Post by electrochrisso »

I will put this with my PB Docs, thanks for sharing. :)
PureBasic! Purely one of the best 8)
User avatar
J. Baker
Addict
Addict
Posts: 2178
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: TransformSprite() Z-Depth Basics

Post by J. Baker »

electrochrisso wrote:I will put this with my PB Docs, thanks for sharing. :)
No problem. ;)
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef

Mac: 10.13.6 / 1.4GHz Core 2 Duo / 2GB DDR3 / Nvidia 320M
PC: Win 7 / AMD 64 4000+ / 3GB DDR / Nvidia 720GT


Even the vine knows it surroundings but the man with eyes does not.
User avatar
STARGÅTE
Addict
Addict
Posts: 2067
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: TransformSprite() Z-Depth Basics

Post 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.
Last edited by STARGÅTE on Fri Mar 08, 2024 10:14 pm, edited 1 time in total.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
J. Baker
Addict
Addict
Posts: 2178
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: TransformSprite() Z-Depth Basics

Post 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! ;)
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef

Mac: 10.13.6 / 1.4GHz Core 2 Duo / 2GB DDR3 / Nvidia 320M
PC: Win 7 / AMD 64 4000+ / 3GB DDR / Nvidia 720GT


Even the vine knows it surroundings but the man with eyes does not.
Post Reply