[SOLVED] Problem Translating Bullet Vector Relative to Player's Angle
Posted: Sun Jul 24, 2022 2:36 am
In my top-down shooter, the player holds a gun in their right hand, and bullets need to spawn from that location regardless of the player's angle. I stumbled across this thread and this thread, however both of these seem vastly overcomplicated for what I'm trying to achieve (I tried both anyway and failed).
LOVE2D's draw function makes it trivial to position sprites this way:
but this is obviously limited to draw functions and can't be depended on for collision/position updates.
Using the bump.lua library to handle collisions, here is the closest I got:
where b.offsX and b.offsY are the same values used to offset the bullet sprites. This results in the following:
Red are the collision boxes, blue-white are the sprites. You can see that this code works fine when the player's angle is exactly up, but fails in all other cases. Intuitively that says I need to be doing some trigonometric transformations to account for the player's angle.
Given that my game is a top-down shooter the concept of local/global coordinates has become more relevant over time and I'd like to gain a better grasp of the math involved. Effectively I want to make whatever calculations are happening under the hood for ox,oy in love.graphics.draw more generic for use across my codebase. Thanks in advance.
LOVE2D's draw function makes it trivial to position sprites this way:
Code: Select all
love.graphics.draw(guns.equipped.sprite, player.x, player.y, player_angle(), 1, 1, guns.equipped.offsX, guns.equipped.offsY)
Using the bump.lua library to handle collisions, here is the closest I got:
Code: Select all
local goalX = b.x + math.cos(b.direction - math.pi/2) * b.speed * dt
local goalY = b.y + math.sin(b.direction - math.pi/2) * b.speed * dt
local actualX, actualY, cols, length = world:move(b, goalX - b.offsX, goalY - b.offsY, bulletFilter)
b.x, b.y = actualX + b.offsX, actualY + b.offsY
Red are the collision boxes, blue-white are the sprites. You can see that this code works fine when the player's angle is exactly up, but fails in all other cases. Intuitively that says I need to be doing some trigonometric transformations to account for the player's angle.
Given that my game is a top-down shooter the concept of local/global coordinates has become more relevant over time and I'd like to gain a better grasp of the math involved. Effectively I want to make whatever calculations are happening under the hood for ox,oy in love.graphics.draw more generic for use across my codebase. Thanks in advance.