Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #1614: add Body:getLocalPoints #1657

Merged
merged 1 commit into from
Dec 30, 2020

Conversation

qaisjp
Copy link
Contributor

@qaisjp qaisjp commented Dec 30, 2020

This is identical to Body:getWorldPoints, except it gets the local points instead.

Fixes #1614.

I've tested this by getting 4 points in each corner around the cursor, converting those points to local points, converting those local points back into world points, and checking that the points are equal. Then using those points to draw a polygon (a square) around the cursor.

Lua script
local world, body

function love.load()
    love.graphics.setBackgroundColor(0, 255, 255)

    love.physics.setMeter(64)

    world = love.physics.newWorld()

    local width, height = love.window.getMode()
    body = love.physics.newBody(world, width/2, height/2)
end

function love.update(dt)
    world:update(dt)
end

function love.draw()
    love.graphics.setLineWidth(12)

    -- Build coordinates for squares around the cursor
    local mx, my = love.mouse.getPosition()
    local points = {
        mx - 30, my - 30, -- top left
        mx + 30, my - 30, -- top right
        mx + 30, my + 30, -- bottom right
        mx - 30, my + 30, -- bottom left
    }

    -- Convert those points local to the body
    local localPoints = {body:getLocalPoints(unpack(points))}

    -- Convert them back to the world
    local worldPoints = {body:getWorldPoints(unpack(localPoints))}

    -- Check that they are equal
    for i, original in ipairs(points) do
        local converted = worldPoints[i]
        assert(original == converted, string.format("Expected %f, got %f", original, converted))
    end

    -- Draw a polgyon
    love.graphics.polygon("line", unpack(worldPoints))
end

@slime73 slime73 merged commit c04b8d1 into love2d:master Dec 30, 2020
@qaisjp qaisjp deleted the feature/get-world-points branch December 30, 2020 23:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Body:getLocalPoints
2 participants