0

Since I have a collision check function in Lua:

    function onCollision(obj1,obj2)
     obj1x = obj1.left
     obj1y = obj1.top
     obj1w = obj1.width
     obj1h = obj1.height
     obj2x = obj2.left
     obj2y = obj2.top
     obj2w = obj2.width
     obj2h = obj2.height
     if obj2x + obj2w >= obj1x and obj2y + obj2h >= obj1y and obj2y <= obj1y + obj1h and obj2x <= obj1x + obj1w then
    return true
    end
   end

And I did make some panels as bricks using array table on my form, with this function, I did collision check between ball with each form sides without problems.

function createBricks()
 for row = 1, brickRows do
   bricks[row] = {}
    for col = 1, brickColumns do
     local x = (col - 1) * (width + gap) -- x offset
     local y = (row - 1) * (height + gap) -- y offset
     local newBrick = createPanel(gamePanel)
     newBrick.width = brickWidth
     newBrick.height = brickHeight
     newBrick.top = y + 15
     newBrick.left = x + 15
     newBrick.BorderStyle = 'bsNone'
     if level == 1 then newBrick.color = '65407' -- green
     elseif level == 2 then newBrick.color = '858083' -- red
     elseif level == 3 then newBrick.color = '9125192' -- brown
     elseif level == 4 then newBrick.color = math.random(8,65255) end
     bricks[row][col] = newBrick
    end
 end
end

Next how to detect if the ball collided with the bricks?. So far I did:

for row = 1, brickRows do
       bricks[row] = {}
        for col = 1, brickColumns do
         dBrick = bricks[row][col]
         if onCollision(gameBall,dBrick) then
          dBrick.destroy()  -- destroy the collided brick
         end
        end
      end

I want to learn how to implement this collision logic in VB Net script which VB script easier for me, I did the whole game project using VB Net, now I try to re-write the project using CE Lua.

Private brickArray(brickRows, brickColumns) As Rectangle
Private isBrickEnabled(brickRows, brickColumns) As Boolean

For rows As Integer = 0 To brickRows
 For columns As Integer = 0 To brickColumns
  If Not isBrickEnabled(rows, columns) Then Continue For
   If gameBall.IntersectsWith(brickArray(rows, columns)) Then
      isBrickEnabled(rows, columns) = False

      If gameBall.X + 10 < brickArray(rows, columns).X Or _
        gameBall.X > brickArray(rows, columns).X + brickArray(rows, columns).Width _
        Then
         xVel = -xVel
     Else
        yVel = -yVel
     End If
    End If
   Next
  Next

And also this private sub, how to write it CE Lua?

Sub loadBricks()
        Dim xOffset As Integer = 75, yOffset As Integer = 100
        For row As Integer = 0 To brickRows
            For column As Integer = 0 To brickColumns
                brickArray(row, column) = New Rectangle(xOffset, yOffset, brickWidth, brickHeight)
                xOffset += brickWidth + 10
                isBrickEnabled(row, column) = True
            Next
            yOffset += brickHeight + 10
            xOffset = 75
        Next
    End Sub

    Function getBrickCount() As Integer
        Dim Count As Integer = 0
        For Each brick As Boolean In isBrickEnabled
            If brick = True Then Count += 1
        Next
        Return Count
    End Function
JoeFern
  • 117
  • 1
  • 8

1 Answers1

0
function onCollision(obj1,obj2)
     obj1x = obj1.left
     obj1y = obj1.top
     obj1w = obj1.width
     obj1h = obj1.height
     obj2x = obj2.left
     obj2y = obj2.top
     obj2w = obj2.width
     obj2h = obj2.height
     if obj2x + obj2w >= obj1x and obj2y + obj2h >= obj1y and obj2y <= obj1y + obj1h and obj2x <= obj1x + obj1w then
    return true
    end
   end

Then to detect collision the collision and remove from the table:

-- Drawback table
local function tcount( t )
  local c = 0
  for k,v in pairs(t) do
   c = c + 1
  end
  return c
end

 local count = #brickArray
       for x = 1, count do
        if onCollision(gameBall, brickArray[x]) then
         if gameBall.Left + 10 < brickArray[x].Left or gameBall.Left > brickArray[x].Left + brickArray[x].Width then
            xVel = -xVel else yVel = -yVel
          end
           playSound(findTableFile('strikeball.wav'))
           brickArray[x] = brickArray[count]
           brickArray[x] = x
           brickArray[count] = nil
           brickArray[x].Visible = false
           tcount(brickArray)
        end
       end

The code above detected the collision and remove the object from the table, but that is not removed from display. How to remove the bricks from the table and display, using Cheat Engine Lua script?.

JoeFern
  • 117
  • 1
  • 8