How to offset a body on another body?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
wburton95
Prole
Posts: 5
Joined: Sun Dec 16, 2018 8:13 pm

How to offset a body on another body?

Post by wburton95 »

I'm trying to figure out how to offset a body on another body so that it follows with with its rotation. I cannot find many clear resources on the subject. I'm working on a space shooter and I'm new to game development. I appreciate any help.

main.lua

Code: Select all


function love.load()
    love.graphics.setDefaultFilter('nearest', 'nearest')
    require('player')
    playerbodies()
end

function love.update(dt)
    world:update(dt)
    pshipmath()
    pturretmath()

end

function love.draw()
    playerdraw()
end
player.lua

Code: Select all

function playerbodies()
    
    world = love.physics.newWorld(0,0,true)
    
    --Table for ship object

    redship = {}
    redship['body'] = love.physics.newBody(world, 0, 0, "dynamic")
    redship['shape'] = love.physics.newRectangleShape(5,5)
    redship['image'] = love.graphics.newImage("/ships/red-ship.png")
    redship['fixture'] = love.physics.newFixture(redship.body,redship.shape,10)

    --Table for gun object.

    gun = {}
    gun['body'] = love.physics.newBody(world, 0, 0, "dynamic")
    gun['shape'] = love.physics.newRectangleShape(1,1)
    gun['image'] = love.graphics.newImage('/ships/big gun.png')
    gun['fixture'] = love.physics.newFixture(redship.body,redship.shape,10)

    
end
function pshipmath()
    
    --Setting the damping for movement.
    redship.body:setLinearDamping(0.5)
    redship.body:setAngularDamping(0.5)
    
    --Finding the foward direction and multiplying for ship movement.
    local angle = redship.body:getAngle()
    local y = math.cos(angle) * 10
    local x = math.sin(angle) * 10
    
    --Applying the foward movement.
    if love.keyboard.isDown("w") then
        redship.body:applyForce(-x, y)
    elseif love.keyboard.isDown("s") then
        redship.body:applyForce(x, -y)
    end

    --Applying rotation.
    if love.keyboard.isDown('d') then
        redship.body:applyTorque(0.8)
    elseif love.keyboard.isDown('a') then
        redship.body:applyTorque(-0.8)
    else
        redship.body:applyTorque(0)
    end
    
    
end

function pturretmath()
        --Point the gun at the cursor.
        local gx, gy = redship.body:getTransform()
        local mx, my = love.mouse.getPosition()
        local mx, my = mx / 3, my / 3
        local pos = math.atan2((mx - gx), (my - gy))
        gun.body:setAngle(-pos)
end

function playerdraw()
    love.graphics.push()
    love.graphics.scale(3,3)
    love.graphics.draw(redship.image, redship.body:getX(), redship.body:getY(), redship.body:getAngle(), 1, 1, redship.image:getWidth()/2, redship.image:getHeight()/2)
    love.graphics.draw(gun.image, redship.body:getX(), redship.body:getY(), gun.body:getAngle(), 1, 1, gun.image:getWidth() / 2, gun.image:getHeight()/2)
    love.graphics.pop()
end
User avatar
pgimeno
Party member
Posts: 3549
Joined: Sun Oct 18, 2015 2:58 pm

Re: How to offset a body on another body?

Post by pgimeno »

I'm not sure if I've understood your request properly. From what I've understood, it seems you want a RevoluteJoint but that's going to have side effects that you probably want to avoid.

If the turret is going to be entirely contained within the ship's body, or if you don't care about collisions with the turret, I recommend handling the turret without physics at all. You can transform the local coordinates of the point where the turret is attached, to physics world coordinates, with Body:getWorldPoints, and use the origin and rotation features of love.graphics.draw to make it rotate properly.
Post Reply

Who is online

Users browsing this forum: No registered users and 83 guests