Michael Hartlef
25-10-2009, 23:14
Hi folks,
here is another sample script that shows how you could handle collision detection for sprites.
It features a ship and a platform. If you land on the platform the ship should stop its vertical movement.
You can control the ship via the Up/LEFT/RIGHT Key. Q to quit the script.
I added the newest TBGL module as it contains a new function (TBGL_SpriteGetOldPos) and a bugfix for
the Box2Box collision check.
Have fun
Michael
'=============================================================================
'= 2D Sprites =
'= Collision example #2 =
'= =
'= Michael Hartlef, 2009 =
'=============================================================================
Uses "TBGL"
dim hWnd AS DWORD
dim FrameRate AS DOUBLE
DIM xdiff as single
DIM width, height as long
%textureSheet = 1
Dim sprPlatform, sprShip, sprBlock As Long
'*****************************************************************
FUNCTION TBMAIN()
'*****************************************************************
local dirTextures as string = "Textures\"
' -- Create and show window
hWnd = TBGL_CREATEWINDOWEX("TBGL 2D Sprite sample (collision check) #2", 640, 480, 32, %TBGL_WS_WINDOWED Or %TBGL_WS_CLOSEBOX)
TBGL_GetWindowClient( hWnd, width, height )
TBGL_ShowWindow
' -- Load the sprites
TBGL_LOADTEXTURE(APP_SourcePath + dirTextures + "platformtest.tga", %textureSheet, %TBGL_TEX_NEAREST)
sprPlatform = TBGL_SPRITECREATE(%textureSheet)
TBGL_SPRITESETBASESIZE(sprPlatform,64,64)
TBGL_SPRITESETTEXCOORD(sprPlatform,1,0,0,63,63)
TBGL_SPRITESETCOLLISIONTYPE(sprPlatform,1)
sprShip = TBGL_SPRITECREATE(%textureSheet)
TBGL_SPRITESETBASESIZE(sprShip,64,64)
TBGL_SPRITESETTEXCOORD(sprShip,1,64,0,127,63)
TBGL_SPRITESETCOLLISIONTYPE(sprShip,1)
' -- Set some render states
tbgl_uselighting %FALSE
tbgl_useblend %FALSE
TBGL_UseDepth %FALSE
tbgl_UseTexture %TRUE
TBGL_USEVSYNC %TRUE
tbgl_UseAlphaTest %TRUE
tbgl_AlphaFunc %tbGL_GREATER, 0.1
' -- Resets status of all keys
TBGL_ResetKeyState()
' -- Set the background to a dark blue
TBGL_BACKCOLOR 150, 150 ,200
' -- Now set the sprites positions
TBGL_SPRITESETPOS(sprPlatform,width/2,height-32)
TBGL_SPRITESETPOS(sprShip,40,40)
' -- Give the ship a little speed to begin with
TBGL_SPRITEADDSPEED(sprShip, 10, 90)
'Add some friction to the sprite
TBGL_SPRITESETFRICTION(sprShip,0.5)
' -- Set the render matrix to the window clients resolution
TBGL_RENDERMATRIX2D (0,height,width,0)
TBGL_BINDPERIODICFUNCTION(hWnd, "Do_GameLoopFunction", 15)
TBGL_PROCESSPERIODICFUNCTION(hWnd)
If TBGL_ISWINDOW(hWnd) = %TRUE Then TBGL_DESTROYWINDOW
End Function
Sub Do_GameLoopFunction()
Local xp, yp, oldx, oldy As Single
FrameRate = TBGL_GETFRAMERATE
' -- Clear the background
TBGL_CLEARFRAME
' -- Add some speed to a sprite according the the pressed keys
If TBGL_GETWINDOWKEYSTATE(hWnd, %VK_UP) Then TBGL_SPRITEADDSPEED(sprShip, 35.0/framerate, 0)
If TBGL_GETWINDOWKEYSTATE(hWnd, %VK_RIGHT) Then TBGL_SPRITEADDSPEED(sprShip, 15.0/framerate, 90)
If TBGL_GETWINDOWKEYSTATE(hWnd, %VK_LEFT) Then TBGL_SPRITEADDSPEED(sprShip, 15.0/framerate, 270)
' -- This simulates the gravity
TBGL_SPRITEADDSPEED(sprShip, 10.0/framerate, 180)
' -- Now calculate new positions of all sprites
TBGL_SPRITESUPDATEALL(10/framerate)
' -- Check if the sprite collided with the platform
'If TBGL_SPRITECOLLIDED(sprship,sprplatform) Then
If TBGL_SPRITECOLLIDED(sprplatform,sprship) Then
' -- Retrieve the position before the collision
TBGL_SpriteGetOldPos(sprShip,oldx,oldy)
' -- Read the current position
TBGL_SPRITEGETPOS(sprShip,xp,yp)
' -- Set the position again (old Y, but current X)
TBGL_SPRITESETPOS(sprShip,xp,oldy)
' -- If the speed angle is downwards, eliminate the Y-Speed factor
' -- Because we don't want the sprite to move further down
If Inside(TBGL_SPRITEGETSPEEDANGLE(sprShip),91,269) Then
TBGL_SPRITEGETSPEEDXY(sprShip,oldx,oldy)
TBGL_SPRITESETSPEEDXY(sprShip,oldx,0)
End If
End If
' -- With one command you draw all active sprites, even if it is just one :-)
TBGL_SpritesDrawAll
' -- Flip the buffer so you see something on the screen
TBGL_DrawFrame
' -- ESCAPE key to exit application
If TBGL_GETWINDOWKEYSTATE(hWnd, %VK_Q) Then
TBGL_UNBINDPERIODICFUNCTION(hWnd)
Exit Sub
End If
End Sub
here is another sample script that shows how you could handle collision detection for sprites.
It features a ship and a platform. If you land on the platform the ship should stop its vertical movement.
You can control the ship via the Up/LEFT/RIGHT Key. Q to quit the script.
I added the newest TBGL module as it contains a new function (TBGL_SpriteGetOldPos) and a bugfix for
the Box2Box collision check.
Have fun
Michael
'=============================================================================
'= 2D Sprites =
'= Collision example #2 =
'= =
'= Michael Hartlef, 2009 =
'=============================================================================
Uses "TBGL"
dim hWnd AS DWORD
dim FrameRate AS DOUBLE
DIM xdiff as single
DIM width, height as long
%textureSheet = 1
Dim sprPlatform, sprShip, sprBlock As Long
'*****************************************************************
FUNCTION TBMAIN()
'*****************************************************************
local dirTextures as string = "Textures\"
' -- Create and show window
hWnd = TBGL_CREATEWINDOWEX("TBGL 2D Sprite sample (collision check) #2", 640, 480, 32, %TBGL_WS_WINDOWED Or %TBGL_WS_CLOSEBOX)
TBGL_GetWindowClient( hWnd, width, height )
TBGL_ShowWindow
' -- Load the sprites
TBGL_LOADTEXTURE(APP_SourcePath + dirTextures + "platformtest.tga", %textureSheet, %TBGL_TEX_NEAREST)
sprPlatform = TBGL_SPRITECREATE(%textureSheet)
TBGL_SPRITESETBASESIZE(sprPlatform,64,64)
TBGL_SPRITESETTEXCOORD(sprPlatform,1,0,0,63,63)
TBGL_SPRITESETCOLLISIONTYPE(sprPlatform,1)
sprShip = TBGL_SPRITECREATE(%textureSheet)
TBGL_SPRITESETBASESIZE(sprShip,64,64)
TBGL_SPRITESETTEXCOORD(sprShip,1,64,0,127,63)
TBGL_SPRITESETCOLLISIONTYPE(sprShip,1)
' -- Set some render states
tbgl_uselighting %FALSE
tbgl_useblend %FALSE
TBGL_UseDepth %FALSE
tbgl_UseTexture %TRUE
TBGL_USEVSYNC %TRUE
tbgl_UseAlphaTest %TRUE
tbgl_AlphaFunc %tbGL_GREATER, 0.1
' -- Resets status of all keys
TBGL_ResetKeyState()
' -- Set the background to a dark blue
TBGL_BACKCOLOR 150, 150 ,200
' -- Now set the sprites positions
TBGL_SPRITESETPOS(sprPlatform,width/2,height-32)
TBGL_SPRITESETPOS(sprShip,40,40)
' -- Give the ship a little speed to begin with
TBGL_SPRITEADDSPEED(sprShip, 10, 90)
'Add some friction to the sprite
TBGL_SPRITESETFRICTION(sprShip,0.5)
' -- Set the render matrix to the window clients resolution
TBGL_RENDERMATRIX2D (0,height,width,0)
TBGL_BINDPERIODICFUNCTION(hWnd, "Do_GameLoopFunction", 15)
TBGL_PROCESSPERIODICFUNCTION(hWnd)
If TBGL_ISWINDOW(hWnd) = %TRUE Then TBGL_DESTROYWINDOW
End Function
Sub Do_GameLoopFunction()
Local xp, yp, oldx, oldy As Single
FrameRate = TBGL_GETFRAMERATE
' -- Clear the background
TBGL_CLEARFRAME
' -- Add some speed to a sprite according the the pressed keys
If TBGL_GETWINDOWKEYSTATE(hWnd, %VK_UP) Then TBGL_SPRITEADDSPEED(sprShip, 35.0/framerate, 0)
If TBGL_GETWINDOWKEYSTATE(hWnd, %VK_RIGHT) Then TBGL_SPRITEADDSPEED(sprShip, 15.0/framerate, 90)
If TBGL_GETWINDOWKEYSTATE(hWnd, %VK_LEFT) Then TBGL_SPRITEADDSPEED(sprShip, 15.0/framerate, 270)
' -- This simulates the gravity
TBGL_SPRITEADDSPEED(sprShip, 10.0/framerate, 180)
' -- Now calculate new positions of all sprites
TBGL_SPRITESUPDATEALL(10/framerate)
' -- Check if the sprite collided with the platform
'If TBGL_SPRITECOLLIDED(sprship,sprplatform) Then
If TBGL_SPRITECOLLIDED(sprplatform,sprship) Then
' -- Retrieve the position before the collision
TBGL_SpriteGetOldPos(sprShip,oldx,oldy)
' -- Read the current position
TBGL_SPRITEGETPOS(sprShip,xp,yp)
' -- Set the position again (old Y, but current X)
TBGL_SPRITESETPOS(sprShip,xp,oldy)
' -- If the speed angle is downwards, eliminate the Y-Speed factor
' -- Because we don't want the sprite to move further down
If Inside(TBGL_SPRITEGETSPEEDANGLE(sprShip),91,269) Then
TBGL_SPRITEGETSPEEDXY(sprShip,oldx,oldy)
TBGL_SPRITESETSPEEDXY(sprShip,oldx,0)
End If
End If
' -- With one command you draw all active sprites, even if it is just one :-)
TBGL_SpritesDrawAll
' -- Flip the buffer so you see something on the screen
TBGL_DrawFrame
' -- ESCAPE key to exit application
If TBGL_GETWINDOWKEYSTATE(hWnd, %VK_Q) Then
TBGL_UNBINDPERIODICFUNCTION(hWnd)
Exit Sub
End If
End Sub