Page 1 of 1

[SOLVED] Problem Translating Bullet Vector Relative to Player's Angle

Posted: Sun Jul 24, 2022 2:36 am
by Azzla
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:

Code: Select all

love.graphics.draw(guns.equipped.sprite, player.x, player.y, player_angle(), 1, 1, guns.equipped.offsX, guns.equipped.offsY)
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:

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
where b.offsX and b.offsY are the same values used to offset the bullet sprites. This results in the following:

ImageImage
ImageImage

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.

Re: Problem Translating Bullet Vector Relative to Player's Angle

Posted: Sun Jul 24, 2022 7:17 am
by togFox
It seems to me the gun is always 'd's degrees clockwise of the players facing (f) and always the same distance (x) from the players origin.

Based on those constant truths, you can always find the offset for the gun and the vector the bullet will travel.

Re: Problem Translating Bullet Vector Relative to Player's Angle

Posted: Sun Jul 24, 2022 5:24 pm
by ReFreezed
I think you might not be using the origin parameter in the right way. The origin is just for saying what the "zero" point of the texture being drawn is (i.e. what part of the texture should be drawn at the specified x/y, and what point the texture should rotate around, in texture coordinates).

Anyway, here's a small project that could help you understand how to solve the problem. (See the attachment.)

Re: Problem Translating Bullet Vector Relative to Player's Angle

Posted: Mon Jul 25, 2022 6:08 am
by Azzla
ReFreezed wrote: Sun Jul 24, 2022 5:24 pm Anyway, here's a small project that could help you understand how to solve the problem. (See the attachment.)
It is honestly embarrassing how simple this looks, after I spent an entire day banging my head against it to no avail. The elusive chunk of code I needed:

Code: Select all

local handDistance = math.sqrt(HAND_X^2 + HAND_Y^2) -- From the center of the player.
local handAngle    = player.angle + math.atan2(HAND_Y, HAND_X)
local handOffsetX  = handDistance * math.cos(handAngle)
local handOffsetY  = handDistance * math.sin(handAngle)
I guess I am just not good enough at math, but I don't know how I would have discovered this solution via trial and error on my own. Need to study more trig!

Thanks. :D