PDA

View Full Version : TBGL Sprites collision sample 2



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

Petr Schreiber
26-10-2009, 00:20
Miiiiike,

it seems the file in the attachement is different from what you describe (no keyboard input).

Lionheart008
26-10-2009, 07:47
same to me :)

found only as texture: "testanim4.bmp"
missing: "platformtest.tga" for your current example.

best regards, frank

Michael Hartlef
26-10-2009, 20:46
Sorry, guys. I uploaded the wrong zip file. But now it should be fine.

Petr Schreiber
26-10-2009, 21:01
Thanks Mike,

it worked perfectly :)

Lionheart008
03-11-2009, 11:27
hello michael, petr.

thanks for this interesting sprite example with collision and landing! I will use it for the future I am sure.

my short question: it's also possible to load *.png or *.jpg as tbgl sprite background pictures ? I know it's probably covered with less quality but for a little scene or testing project or small screen resolution would be nice to have this one. often I see no big different between a good png or bitmap file for loading to a game scene and my eyes are ok ;)

thanks in advance, nice day all, frank

Michael Hartlef
03-11-2009, 14:48
By default TBGL only support BMP and uncompressed TGA images.

I think you can work around this limitation with the help of GDI+ funktionalities which I have no knowledge about. Means you load an image with GDI+ and then create a texture from it. Maybe there is a script here on the forum showing the procedure, but I could be wrong about that.

Petr Schreiber
03-11-2009, 15:51
Mike is right,

PNG and JPG support is planned for TBGL in ThinBASIC 2.x series.
Why? Because 2.x series will take Windows XP as lowest OS supported, which means GDI+ is guaranteed to be present on the target PC.

If you need to use different file formats, you could pick any image library (devIL, FreeImage), extract the RGBA stream and pass it to TBGL_MakeTexture even now. But I do not want to build any dependencies to TBGL now.

So I recommend sticking to BMP and TGA for now, if possible. The support for JPG/PNG will come later and will be integrated to texture handling functions in seamless fashion.