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

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Azzla
Prole
Posts: 43
Joined: Sun Mar 29, 2020 2:23 am

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

Post 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.
Last edited by Azzla on Mon Jul 25, 2022 6:09 am, edited 2 times in total.
libraries: Stalkpile
electronic music stuff: https://soundcloud.com/azzlamusic
User avatar
togFox
Party member
Posts: 835
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

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

Post 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.
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Idle gridiron. Set team orders then idle and watch: https://togfox.itch.io/pad-and-pencil-gridiron
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

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

Post 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.)
Attachments
TopdownShooter.love
(1.92 KiB) Downloaded 135 times
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
User avatar
Azzla
Prole
Posts: 43
Joined: Sun Mar 29, 2020 2:23 am

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

Post 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
libraries: Stalkpile
electronic music stuff: https://soundcloud.com/azzlamusic
Post Reply

Who is online

Users browsing this forum: No registered users and 11 guests