summaryrefslogtreecommitdiff
path: root/logic/physics.lua
blob: ef27713aed52218517eeed428381186e5778b3c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
local physics = {}

function physics.bodifyObject(world, object, shape, bodyType)
    object.physicsBody = love.physics.newBody(world, object.x, object.y, bodyType or "dynamic")

    if type(shape) == "number" then
        object.physicsShape = love.physics.newCircleShape(shape)
    elseif type(shape) == "table" then
        object.physicsShape = love.physics.newRectangleShape(shape[1], shape[2])
    elseif object.r then
        object.physicsShape = love.physics.newCircleShape(object.r)
    end

    object.fixture = love.physics.newFixture(object.physicsBody, object.physicsShape, 10)

    object.physicsBody:setGravityScale(0)
    object.physicsBody:setLinearDamping(0)
    object.physicsBody:setActive(true)
    object.physicsBody:setMass(object.mass or 1)
    object.physicsBody:setLinearVelocity(
        type(object.speed) == "table" and object.speed.x or 0,
        type(object.speed) == "table" and object.speed.y or 0)
end

function physics.getObjectPosition(object, rect)
    local body = object.physicsBody or object.body or nil
    local data = {}
    if body and not body:isDestroyed() then
        data.x = body:getX()
        data.y = body:getY()
    end
    return data
end

return physics