Results 1 to 8 of 8

Thread: TBGL Sprites collision sample 2

  1. #1

    TBGL Sprites collision sample 2

    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


    [code=thinbasic]

    '=============================================================================
    '= 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









    [/code]
    Attached Files Attached Files

  2. #2
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732

    Re: TBGL Sprites collision sample 2

    Miiiiike,

    it seems the file in the attachement is different from what you describe (no keyboard input).
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  3. #3
    Senior Member Lionheart008's Avatar
    Join Date
    Sep 2008
    Location
    Germany, Bad Sooden-Allendorf
    Age
    51
    Posts
    934
    Rep Power
    109

    Re: TBGL Sprites collision sample 2

    same to me

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

    best regards, frank
    you can't always get what you want, but if you try sometimes you might find, you get what you need

  4. #4

    Re: TBGL Sprites collision sample 2

    Sorry, guys. I uploaded the wrong zip file. But now it should be fine.

  5. #5
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732

    Re: TBGL Sprites collision sample 2

    Thanks Mike,

    it worked perfectly
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  6. #6
    Senior Member Lionheart008's Avatar
    Join Date
    Sep 2008
    Location
    Germany, Bad Sooden-Allendorf
    Age
    51
    Posts
    934
    Rep Power
    109

    Re: TBGL Sprites collision sample 2

    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
    you can't always get what you want, but if you try sometimes you might find, you get what you need

  7. #7

    Re: TBGL Sprites collision sample 2

    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.


  8. #8
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732

    Re: TBGL Sprites collision sample 2

    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.
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

Similar Threads

  1. TBGL sprite collision; I want to tune the box2circle collision a bit
    By Michael Hartlef in forum TBGL module by Petr Schreiber
    Replies: 3
    Last Post: 31-07-2009, 23:36

Members who have read this thread: 1

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •