Phyisics Obstacles

Hello. I made a fun “little” phyisics obsticle class. It allows you to create multiple physics rectangles that all belong to one Obstacle and move along set paths and rotations. It still needs parameters for the rotation and multiple paths but just thought I’d share.

Yes, I know I spelled obstacle wrong.

-- Obsticles
function setup()
    f1 = vec2(0,0)
    f2 = vec2(WIDTH,0)
    floor = physics.body(EDGE,f1,f2)

    rw1 = vec2(WIDTH,0)
    rw2 = vec2(WIDTH,HEIGHT)
    rw = physics.body(EDGE,rw1, rw2)

    lw1 = vec2(0,0)
    lw2 = vec2(0,HEIGHT)
    lw = physics.body(EDGE,lw1, lw2)

    c1 = vec2(WIDTH, HEIGHT)
    c2 = vec2(0,HEIGHT)
    c = physics.body(EDGE,c1, c2)
    
    w = 10
    h = 200
    -- The cords for the points of the rect must be in order
    param = {}
    param[1] = {}
    param[1][POS_INDEX] = vec2(WIDTH/2, HEIGHT/2)
    param[1][POINT_ONE_INDEX] = vec2(-w/2,-h/2)
    param[1][POINT_TWO_INDEX] = vec2(w/2,-h/2)
    param[1][POINT_THREE_INDEX] = vec2(w/2,h/2)
    param[1][POINT_FOUR_INDEX] = vec2(-w/2,h/2)
    
    param[1][PATH_INDEX] = vec4(100,HEIGHT/2,400,HEIGHT/2)
    

    param[2] = {}
    param[2][POS_INDEX] = vec2(WIDTH/2, HEIGHT/2)
    param[2][POINT_ONE_INDEX] = vec2(-w/2,-h/2)
    param[2][POINT_TWO_INDEX] = vec2(w/2,-h/2)
    param[2][POINT_THREE_INDEX] = vec2(w/2,h/2)
    param[2][POINT_FOUR_INDEX] = vec2(-w/2,h/2)
    
    param[2][PATH_INDEX] = vec4(100,HEIGHT - 100,WIDTH - 100,100)


    obsticle = Obsticle(param)
    
    r = 60
    ball = physics.body(CIRCLE, 50)
    ball.x = r * 2
    ball.y = r* 2
    ball.gravityScale = 0
    ball.linearVelocity = vec2(400,800)
    ball.restitution = 1
    ball.type = DYNAMIC
end

function draw()

    background(40, 40, 50)
    obsticle:Update()
    obsticle:draw()
    pushStyle()
    strokeWidth(2)
    stroke(255, 255, 255, 255)
    line(100 - w/2,HEIGHT/2,400 + w/2,HEIGHT/2)
    strokeWidth(0)
    fill(206, 26, 26, 255)
    ellipse(ball.x,ball.y,r *2)
    popStyle()
end

 Obsticle = class()

--[[
A class of bodies and paths that those bodies take when updated


]]

POS_INDEX = 1

POINT_ONE_INDEX = 2
POINT_TWO_INDEX = 3
POINT_THREE_INDEX = 4
POINT_FOUR_INDEX = 5

PATH_INDEX = 6

TO = 7
BACK = 8
function Obsticle:init(barrierArray)
    -- each barrierArray item has the cords for that barrier and its path in a table
    self.w = 3
    
    self.lapDur = 3 -- in seconds
    self.roationDur = 3 -- in seconds
    
    self.barrierArray = barrierArray
    self.barriers = {} 
    self.paths = {}
    self.bMeshes = {}
    self.rectIndexs = {}
    -- #self.barriers must == #self.paths
    for i = 1, #self.barrierArray do
        -- Barrier(vec4(self.barrierArray[i][1].x,self.barrierArray[i][1].y))
        self.barriers[i] = physics.body(POLYGON, self.barrierArray[i][POINT_ONE_INDEX],
                                                self.barrierArray[i][POINT_TWO_INDEX],
                                                self.barrierArray[i][POINT_THREE_INDEX],
                                                self.barrierArray[i][POINT_FOUR_INDEX])
        self.barriers[i].gravityScale = 0
        self.barriers[i].type = KINEMATIC
        
        self.barriers[i].x = self.barrierArray[i][POS_INDEX].x
        self.barriers[i].y = self.barrierArray[i][POS_INDEX].y
        
        self.paths[i] = Path(self.barrierArray[i][PATH_INDEX])
        
        self.bMeshes[i] = mesh()
        local c = color(205, 102, 28, 255)

        self.rectIndexs[i] = self.bMeshes[i]:addRect(self.barriers[i].x, 
                                                     self.barriers[i].y, w, h)
        self.bMeshes[i]:setRectColor(self.rectIndexs[i], c)
    end
end

function Obsticle:Update()
    for i = 1, #self.barriers do
        -- TO = left to right or top to bottem
        -- BACK = right to left or bottem to top
        if self.paths[i]:GetState() == TO then


            local tar = self.paths[i]:GetPath().z
            local add = (tar - self.paths[i]:GetPath().x) / (60 * self.lapDur)
            local newX = self.barriers[i].x + add 
            
            
            local tar = self.paths[i]:GetPath().w
            local add = (tar - self.paths[i]:GetPath().y) / (60 * self.lapDur)
            local newY = self.barriers[i].y + add
 
            --print("newX = ", newX, "target x = ", self.paths[i]:GetPath().z)
            --print("newY = ", newY, "target y = ", self.paths[i]:GetPath().w)
            
            self.barriers[i].x = newX 
            self.barriers[i].y = newY

            if self.barriers[i].x >= self.paths[i]:GetPath().z and
            self.barriers[i].y  <= self.paths[i]:GetPath().w then
                self.paths[i]:SetState(BACK)
            end
            
        elseif self.paths[i]:GetState() == BACK then
            
            local tar = self.paths[i]:GetPath().x
            local add = (tar - self.paths[i]:GetPath().z) / (60 * self.lapDur)
            local newX = self.barriers[i].x + add 
            
            local tar = self.paths[i]:GetPath().y
            local add = (tar - self.paths[i]:GetPath().w) / (60 * self.lapDur)
            local newY = self.barriers[i].y + add
            
            --print("newX = ", newX, "target x = ", self.paths[i]:GetPath().x)
            --print("newY = ", newY, "target y = ", self.paths[i]:GetPath().y)
            
            self.barriers[i].x = newX 
            self.barriers[i].y = newY
            
            if self.paths[i]:GetPath().x >= self.barriers[i].x and
            self.paths[i]:GetPath().y <= self.barriers[i].y then
                self.paths[i]:SetState(TO)
            end
            
        end
        
        self.barriers[i].angle = self.barriers[i].angle + (360/(self.roationDur * 60))
        
        if self.barriers[i].angle > 360 then
            self.barriers[i].angle = 0
        end
        
    end 
end

function Obsticle:draw()
    pushStyle()

    rectMode(CENTER)
    fill(231, 123, 36, 255)
    for i = 1, #self.barriers do
        -- print(self.barriers[i].x, self.barriers[i].y)
        
        pushMatrix()

        translate(self.barriers[i].x,self.barriers[i].y)
        rotate(self.barriers[i].angle)
        rect(0,0, w, h)
        
        popMatrix()
                
        --[[
        self.bMeshes[i]:setRect(self.rectIndexs[i], self.barriers[i].x, 
                                self.barriers[i].y, math.rad(self.barriers[i].angle))

        self.bMeshes[i]:draw()
        ]]
    end 
    popStyle()
end

Path = class()

function Path:init(path)
    self.path = path
    self.state = TO
end

function Path:SetState(state)

    self.state = state
end

function Path:GetState()
    return self.state
end

function Path:GetPath()
    return self.path
end