Share My Creation No Internet Connection (Dino Jump)

ilan

Expert
Licensed User
Longtime User
working again on this project and improoving the Collision Detection:


it is build of 3 rects. 1 for the head, 1 for the body and 1 for the tail
 

Sandman

Expert
Licensed User
Longtime User
I realize that three rects most certainly is enough, but I can't help but wonder what the performance hit would be if each pixel in the dino would be its own rect? (Naturally you'd optimize to get as few rects as possible, as long as all pixels in them are filled.)

Are the rects very expensive?
 

ilan

Expert
Licensed User
Longtime User

it would be much simpler to use box2d fo that and create a polygone or chainbody as the dino if you want a very precise collision detection but in my opinion for this kind of game its enough to use 3 rects.
 

ilan

Expert
Licensed User
Longtime User
Oh, I absolutely agree! I was just curious if it would even be possible, or if it was too expensive.

yes it is possible. its acctually really simple. what you do is you create a list and add all rects to it then you run a loop and check if a specific rect (like the cactus) is colliding with one of your dino rect. if true the dino was hit.

here is my dino CLASS

B4X:
Sub Class_Globals
    #if B4J
    Private fx As JFX
    #End If
    Private runningAnim(2), crouchAnim(2), jumpBmp As B4XBitmap
    Private vpW, vpH As Float
    Private xui As XUI
    Public frames, dinoframes, status As Int '0 = isrunning, 1 = isjumping, 2 = iscrouching
    Private cnv As B4XCanvas
    Private pos As vector
    Private dinoframe As B4XRect
    Private yDown, groundY, jumpInt As Float
    Private medead As Boolean
    Private collideFrameList As List
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(senderCanvas As B4XCanvas, width As Float, height As Float)
    runningAnim(0) = xui.LoadBitmap(File.DirAssets,"dino1.png")
    runningAnim(1) = xui.LoadBitmap(File.DirAssets,"dino2.png")
    jumpBmp = xui.LoadBitmap(File.DirAssets,"dino3.png")
    crouchAnim(0) = xui.LoadBitmap(File.DirAssets,"dino4.png")
    crouchAnim(1) = xui.LoadBitmap(File.DirAssets,"dino5.png")
    status = 0
    vpW = width
    vpH = height
    cnv = senderCanvas
    pos.Initialize
    medead = False
    yDown = vpH * 0.00185
    jumpInt = 0
    groundY = vpH * 0.87
    pos.x = vpW * 0.2
    pos.y = groundY
    frames = 0
    dinoframe.Initialize(0,0,0,0)
    collideFrameList.Initialize
End Sub

Public Sub run
    move
    draw
End Sub

Sub move
    jumpInt = jumpInt - yDown
    pos.y = pos.y + yDown - jumpInt
    If pos.y >= groundY Then
        pos.y = groundY
        If status = 2 Then status = 0
    End If
End Sub

Sub draw
    frames = frames + 1
    If frames Mod 6 = 0 Then dinoframes = dinoframes + 1
    If status = 0 Then
        dinoframe.Width = vpW*0.085
        dinoframe.Height = vpH*0.15
        dinoframe.Left = pos.x-dinoframe.Width
        dinoframe.Top = pos.y-(dinoframe.Height/2)
        cnv.DrawBitmap(runningAnim(dinoframes Mod 2),dinoframe)
    else if status = 1 Then
        dinoframe.Width = vpW*0.1
        dinoframe.Height = vpH*0.09
        dinoframe.Left = pos.x-dinoframe.Width
        dinoframe.Top = pos.y-(dinoframe.Height/4)
        cnv.DrawBitmap(crouchAnim(dinoframes Mod 2),dinoframe)
    else if status = 2 Then
        dinoframe.Width = vpW*0.085
        dinoframe.Height = vpH*0.15
        dinoframe.Left = pos.x-dinoframe.Width
        dinoframe.Top = pos.y-(dinoframe.Height/2)
        cnv.DrawBitmap(jumpBmp,dinoframe)
    End If
    If Main.drawCollisionRect Then setCollisionFrameAndDraw(dinoframe,status)
End Sub

Sub setCollisionFrameAndDraw(frame As B4XRect, st As Int)
    collideFrameList.Clear
    Select st
        Case 1
            collideFrameList.Add(frame)
        Case Else
            Dim rectHead As B4XRect   
            rectHead.Initialize(frame.Left+(frame.Width*0.5),frame.Top,frame.Left+(frame.Width*0.95),frame.Top+(frame.Height*0.3))
            Dim rectBody As B4XRect
            rectBody.Initialize(frame.Left+(frame.Width*0.225),frame.Top+(frame.Height*0.35),frame.Left+(frame.Width*0.7),frame.Top+(frame.Height*0.95))
            Dim rectTail As B4XRect
            rectTail.Initialize(frame.Left,frame.Top+(frame.Height*0.4),frame.Left+(frame.Width*0.2),frame.Top+(frame.Height*0.7))
            collideFrameList.AddAll(Array(rectHead,rectBody,rectTail))
    End Select
    
    For Each rect As B4XRect In collideFrameList
        cnv.DrawRect(rect,xui.Color_Red,False,1)
    Next
End Sub

Public Sub dinoJumpNow
    If status = 2 Then Return
    status = 2
    jumpInt = vpH*0.035 ' 10
End Sub

Public Sub dinoCrouchNow(isTrue As Boolean)
    If status = 2 Then Return
    If isTrue Then status = 1 Else status = 0
End Sub

public Sub checkIfHasCollide(enemyFrame As B4XRect) As Boolean
    For Each rect As B4XRect In collideFrameList
        If rect.Bottom > enemyFrame.Top And rect.top < enemyFrame.Bottom Or rect.Bottom < enemyFrame.Top And rect.Top > enemyFrame.Bottom Then
            If rect.Right > enemyFrame.Left And rect.Left < enemyFrame.Right Or rect.Left < enemyFrame.Right And rect.Right > enemyFrame.Left Then
                medead = True
                Return True
            End If
        End If
    Next
    Return False
End Sub
 
Public Sub getStatus As Int
    Return status
End Sub

Public Sub isDinoDead As Boolean
    Return medead
End Sub
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…