Object overlap detection (updated) - with removal on hit

Now when you select any card it will delete another card when they hit. It’s a little sloppy works well. I need to have the height and width of the image dynamic and the next thing to do is to set up a board class that holds all the cards. I should rename the calss to something more generic.

Tiles now generate randomly soon I will have a timer change their texture.

  
   Card = class()

    function Card:init(id,x,y,width,height,cardImage,activeState,callbackFunction,name,debug,touchOffset)
        self.name = name
        self.id = id
        self.x = x
        self.y = y
        self.isCursor = isCursor or 0
        self.width = width
        self.height = height
        self.selected = false
        self.scale = 1 -- the multiplier for  the selected tile size
        self.mesh = mesh()
        self.mesh.texture = imgs[cardImage]
        self.textureCordsList = {} -- the list of vec2 for the uv cordinates and their delay
        -- this delay will be used in the timer class for animating the textures
        self.mesh:addRect(x,y,width,height)
        self.mesh:setRectTex(id,0,0,1,1)
        self.buttonCorner1 = vec2(x-width/2,y-height/2)
        self.buttonCorner2 = vec2(x+width/2, y+height/2)
        --self.hitRect = createBox(x,y,width,height,name)
        --print(self.hitRect.name)
        self.callbackFunction = callbackFunction
        self.hitList = {}
        self.debug=debug
        self.health = health or 10
        self.touchOffset = touchOffset or 0
        self.isHit = 0
    end
   function Card:changePos(x,y)
        self.x=self.x+x
        self.y=self.y+y
        self.buttonCorner1 = vec2(self.x-self.width/2,self.y-self.height/2)
        self.buttonCorner2 = vec2(self.x+self.width/2, self.y+self.height/2) 
    end 
    function Card:draw()
        if self.mesh.shader then
            self.mesh.shader.time=ElapsedTime
       end
        --self.mesh:draw()
        self.mesh:draw()
        -- if debugging mode is on then draw hitrect and 
        if self.debug == 1 and self.selected == true then
            --print("ghghgh")
            noSmooth()
            pushStyle()
            stroke(255)
            strokeWidth(1)
            rectMode(CENTER)
            rect(self.x,self.y,self.width, self.height)
            text("x:"..self.x,self.x,self.y+self.height)
            text("y:"..self.y,self.x,self.y+self.height+16)
            popStyle()
        end

    end

    function Card:touched(touch)
        if touch.state == BEGAN then
            local firstTouch = 0
            bob = "selected"
            -- check if you touched inside of the card
            if touch.x >= self.buttonCorner1.x 
                        and touch.x <= self.buttonCorner2.x 
                        and touch.y >= self.buttonCorner1.y 
                        and touch.y <= self.buttonCorner2.y then
                self.selected = true
                firstTouch = 1
                selectedId=self.id
                self.mesh.shader = shader("Effects:Ripple")
                self.mesh.shader.freq=0.687
                self.scale = 1
                if self.touchOffset == 1 then   
                    self.y = self.y + 50 -- reset the initial touch.
                end 
                self.mesh:setRect(1,self.x*self.scale,self.y*self.scale,self.width*self.scale,self.height*self.scale)
                self.buttonCorner1 = vec2(self.x-self.width/2*self.scale,self.y-self.height/2*self.scale)
                self.buttonCorner2 = vec2(self.x+self.width/2*self.scale, self.y+self.height/2*self.scale)
            end
    elseif touch.state == MOVING then

            -- update the x and y values to reflect the new position
            if self.id == selectedId then
                bob = "moving" 
                --print("moving")
                self.x = self.x + touch.deltaX
                self.y = self.y + touch.deltaY
                self.mesh:setRect(1,self.x,self.y,self.width*self.scale,self.height*self.scale)
                self.buttonCorner1 = vec2(self.x-self.width/2*self.scale,self.y-self.height/2*self.scale)
                self.buttonCorner2 = vec2(self.x+self.width/2*self.scale, self.y+self.height/2*self.scale)
            end    
        elseif touch.state == ENDED then
            bob = "ended"
            self.scale = 1
            self.mesh.shader = nil
            self.hitList = {}
            if self.selected == true then
                -- only affect the selected object
                if self.touchOffset == 1 then   
                    --self.y = self.y - 50 -- reset the initial touch.
                end
                self.mesh:setRect(1,self.x,self.y,self.width*self.scale,self.height*self.scale)
                self.buttonCorner1 = vec2(self.x-self.width/2*self.scale,self.y-self.height/2*self.scale)
                self.buttonCorner2 = vec2(self.x+self.width/2*self.scale, self.y+self.height/2*self.scale)
            else 
                self.mesh:setRect(1,self.x,self.y,self.width*self.scale,self.height*self.scale)
            end
            -- reset the size of the hit bounding box 
            self.buttonCorner1 = vec2(self.x-self.width/2*self.scale,self.y-self.height/2*self.scale)
            self.buttonCorner2 = vec2(self.x+self.width/2*self.scale, self.y+self.height/2*self.scale)
            self.selected = false
            local tmp = cards[selectedId]

         --  print("zzselected id:"..tmp.id)
        --    table.remove(cards,selectedId)
        --    table.insert(cards,#cards+1,tmp)
            selectedId = 0

            --end
        end
    end