如何根据玩家的方向偏移子弹 object

How to offset bullet object, based on player's direction

我 运行 lua 5.1 和 love2d 11.3.0

在我的演示中,我有一个自上而下的射击游戏。出于视觉目的,这个角色看起来是这样的......正如你从这个自上而下的角度所看到的,他正举着他的武器。

我的问题是,我很难搞清楚 无论他面向哪个方向,或者他在屏幕上的哪个位置,如何让他的子弹从枪尖发射.目前,子弹总是从这个body的中心射出。我尝试调整下面的 bullet.xbullet.y 属性,但这是一个不尊重的静态更改玩家方向...


function spawnBullet()
    local bullet = {}
    bullet.x = player.x
    bullet.y = player.y
    bullet.speed = 500
    bullet.direction = playerMouseAngle()
    table.insert(bullets, bullet)
end

完整的游戏逻辑:

-- top down shooter game demo 2021

function love.load()

    -- create table to collect sprite assets
    sprites = {}
    sprites.bg = love.graphics.newImage('graphics/bg_grass.jpg')
    sprites.hero = love.graphics.newImage('graphics/hero.png')

    -- create table for our player hero
    player = {}
    player.x = love.graphics.getWidth() / 2
    player.y = love.graphics.getHeight() / 2
    player.speed = 360
    player.injured = false
    player.injuredSpeed = 270

    -- create table for player bullets
    bullets = {}

    -- game state, timer and score globals
    gameState = 1
    maxTime = 2
    timer = maxTime
    score = 0

end

function love.update(dt)

    if gameState == 2 then

        -- player speed will be adjusted below
        local moveSpeed = player.speed

        -- adjust player speed if player was injured
        if player.injured then moveSpeed = player.injuredSpeed end

        -- player moves right
        if love.keyboard.isDown("d") and player.x < love.graphics.getWidth() -
            50 then player.x = player.x + moveSpeed * dt end

        -- player moves left
        if love.keyboard.isDown("a") and player.x > 50 then
            player.x = player.x - moveSpeed * dt
        end

        -- player moves up
        if love.keyboard.isDown("w") and player.y > 50 then
            player.y = player.y - moveSpeed * dt
        end

        -- player moves down
        if love.keyboard.isDown("s") and player.y < love.graphics.getHeight() -
            50 then player.y = player.y + moveSpeed * dt end
    end

    -- make bullets move in desired direction
    for i, b in ipairs(bullets) do
        b.x = b.x + (math.cos(b.direction) * b.speed * dt)
        b.y = b.y + (math.sin(b.direction) * b.speed * dt)
    end

    -- remove the bullets from the game once they go off-screen
    for i = #bullets, 1, -1 do
        local b = bullets[i]
        if b.x < 0 or b.y < 0 or b.x > love.graphics.getWidth() or b.y >
            love.graphics.getHeight() then table.remove(bullets, i) end
    end

    -- manage game state and timer
    if gameState == 2 then
        timer = timer - dt
        if timer <= 0 then
            maxTime = 0.95 * maxTime
            timer = maxTime
        end
    end
end

function love.draw()

    -- setup background
    love.graphics.push()
    love.graphics.scale(0.20, 0.20)
    love.graphics.draw(sprites.bg, 0, 0)
    love.graphics.pop()

    -- click to begin
    if gameState == 1 then
        love.graphics.printf('Click anywhere to begin!', 0, 50, love.graphics.getWidth(), "center")
    end

    -- display score
    love.graphics.printf('Score: ' .. score, 0, love.graphics.getHeight() - 100, love.graphics.getWidth(), "center")

    -- adjust player color if player gets injured
    if player.injured then love.graphics.setColor(1, 0, 0) end
    -- draw player
    love.graphics.draw(sprites.hero, player.x, player.y, playerMouseAngle(),
                       nil, nil, sprites.hero:getWidth() / 2,
                       sprites.hero:getHeight() / 2)

    -- display player bullets
    for i, b in ipairs(bullets) do
        love.graphics.circle("fill", b.x, b.y, 10, 100)
    end
end

-- enable player direction based on mouse movement
function playerMouseAngle()
    return
        math.atan2(player.y - love.mouse.getY(), player.x - love.mouse.getX()) +
            math.pi
end

-- define bullet properties
function spawnBullet()
    local bullet = {}
    bullet.x = player.x
    bullet.y = player.y
    bullet.speed = 500
    bullet.direction = playerMouseAngle()
    table.insert(bullets, bullet)
end

-- game state determins shooting player bullets...
function love.mousepressed(x, y, button)
    if button == 1 and gameState == 2 then
        spawnBullet()
        -- ..or starting the game
    elseif button == 1 and gameState == 1 then
        gameState = 2
        maxTime = 2
        timer = maxTime
        score = 0
    end
end

提前感谢您的任何提示

在他们旅行期间,您已经掌握了所需的数学知识...

-- make bullets move in desired direction
for i, b in ipairs(bullets) do
    b.x = b.x + (math.cos(b.direction) * b.speed * dt)
    b.y = b.y + (math.sin(b.direction) * b.speed * dt)
end

只需稍微调整一下即可创建子弹

local bullet = {}
bullet.speed = 500
bullet.direction = playerMouseAngle()
bullet.x = player.x +(math.cos(bullet.direction + 0.5) * (sprites.hero:getWidth() / 2))
bullet.y = player.y +(math.sin(bullet.direction + 0.5) * (sprites.hero:getWidth() / 2))
table.insert(bullets, bullet)

猜猜规模。您可能需要将 /2 更改为其他内容,例如 *0.75.