macOS
1.12.3
Trying to have ships thrust appear behind ship but cant for the life of me work out the math to stop it doing weirdness and then clipping aka being hidden by its own sprite on rotation.
Both are polygons I draw in code and then manipulate as per below. When pressing / holding UP the thrust sprite appears
Ship and Thrust code ATM below
function playerShip:update()
-- draw the ship polygon to the sprite's image
local img = gfx.image.new(32, 32)
gfx.pushContext(img)
gfx.setColor(gfx.kColorWhite)
gfx.fillPolygon(playerShip.polygon)
gfx.setColor(gfx.kColorBlack)
gfx.drawPolygon(playerShip.polygon)
gfx.popContext()
playerShip:setImage(img)
-- rotate the transform
playerShip.ship_transform:rotate(playdate.getCrankChange(), 16, 16)
-- playerShip:setRotation(playdate.getCrankChange(), 16, 16)
-- apply the transform to the ship polygon
playerShip.ship_transform:transformPolygon(playerShip.polygon)
-- reset the transform so that crank change doesn’t accumulate over time
playerShip.ship_transform:reset()
local spX, spY = playerShip:getPosition()
local angle = playdate.getCrankPosition()
decelerateShip()
shipDx = thrustSpeed * math.sin(math.rad(angle))
shipDy = thrustSpeed *-1 * math.cos(math.rad(angle)) +gravity
local shipMoveDx = shipDx
local shipMoveDy = shipDy
if (spX < xOffsetmin and shipDx <= 0) then
shipMoveDx = 0
end
if (spX > xOffsetmax and shipDx >= 0) then
shipMoveDx = 0
end
if (spY < yOffsetmin and shipDy <= 0) then
shipMoveDy = 0
end
if (spY > yOffsetmax and shipDy >= 0) then
shipMoveDy = 0
end
newShipX = shipMoveDx +spX
newShipY = shipMoveDy +spY
playerShip:moveWithCollisions(shipMoveDx +spX, shipMoveDy +spY)
end
function thrustShip:update()
local img = gfx.image.new(32, 32)
gfx.pushContext(img)
gfx.setColor(gfx.kColorWhite)
gfx.fillPolygon(thrustShip.polygon)
gfx.setColor(gfx.kColorBlack)
gfx.drawPolygon(thrustShip.polygon)
gfx.popContext()
thrustShip:setImage(img)
-- thrustShip:moveTo(spX+8,spY+36)
local angle = playdate.getCrankPosition()
thrustShip:moveTo(newShipX + (32 * math.sin(math.rad(angle))),newShipY+36) thrustShip:setZIndex(60)
thrustShip.thrust_transform:rotate(playdate.getCrankChange(), 16,-16)
thrustShip.thrust_transform:transformPolygon(thrustShip.polygon)
thrustShip.thrust_transform:reset()
end