summaryrefslogtreecommitdiff
path: root/logic/physics.lua
blob: feb69a4125bbfe47da2ab875be1dc5a7f9edd9d2 (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
36
37
38
39
40
41
local physics = {}

function physics.init(G)
    love.physics.setMeter(64)
    G.world = love.physics.newWorld(0, 0, true)
    G.world:setCallbacks(onContact)
end

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