我正在尝试为爱情中的自上而下射击游戏制作一个基本原型,但是我有一些问题让子弹在旋转时从图像的同一点拍摄。这是我到目前为止的代码工作正常,因为子弹从图像的中心射击,但我希望它从精灵的右手侧射击:
function love.load()
love.graphics.setBackgroundColor(54, 172, 248)
player = love.graphics.newImage('/assets/images/player.png')
playerX = 300
playerY = 300
playerSpeed = 200
bullets = { }
bulletSpeed = 250
cursor = love.mouse.newCursor('assets/images/crosshair.png', 24, 24);
love.mouse.setCursor(cursor);
end
function love.update(dt)
-- Get mouse position to rotate player
local mouseX, mouseY = love.mouse.getPosition()
playerRotation = math.atan2(mouseY - playerY, mouseX - playerX);
-- Keyboard input to move the player
if love.keyboard.isDown('s') then
playerY = playerY + playerSpeed * dt
elseif love.keyboard.isDown('w') then
playerY = playerY - playerSpeed * dt
end
if love.keyboard.isDown('a') then
playerX = playerX - playerSpeed * dt
elseif love.keyboard.isDown('d') then
playerX = playerX + playerSpeed * dt
end
-- update all bullets position
for i, v in ipairs(bullets) do
v.x = v.x + (v.dx * dt)
v.y = v.y + (v.dy * dt)
end
end
function love.draw(dt)
-- Draw player
love.graphics.draw(player, playerX, playerY, playerRotation, 0.5, 0.5, player:getWidth() / 2, player:getHeight() / 2);
-- Draw all bullets
love.graphics.setColor(128, 128, 128)
for i, v in ipairs(bullets) do
love.graphics.circle("fill", v.x, v.y, 3);
end
end
function love.mousepressed(x, y, button)
if button == 1 then
local startX = playerX;
local startY = playerY;
local mouseX = x
local mouseY = y
local angle = math.atan2((mouseY - startY), (mouseX - startX))
local bulletDx = bulletSpeed * math.cos(angle)
local bulletDy = bulletSpeed * math.sin(angle)
table.insert(bullets, {x = startX , y = startY, dx = bulletDx, dy = bulletDy})
end
end
如何从精灵旋转的同一点绘制它?