1
General / Re: [SOLVED] Setting bullets position to a weapon sprite that changes its rotation
« on: June 24, 2024, 12:54:05 pm »Quote
Now i'm confused. How does this work?
From what i understand, the formula does the following:
adds weapons x axis to 76 * x axis of the angle which is calculated by using cos. Does the same for the y axis.
So basically it adds an offset to the bullets spawning location which in my case is 76.
Since it is a ratio i cant put a different number thats multiplying by sin of the rotation angle which is y.
So what i did is just add 0,-4 to that ratio which doesn't change x, but changes y by -4, so now my bullets spawn on the barrel.
Regarding your problem with shifting of the barrel as the rotation occurs, i have this piece of code in my project that flips the weapon during rotation so it always shoots with its barrel.
Without this code the weapon shoots from its stock when aiming left, and shoots from its barrel when aiming right.
It's a simple fix for this visual bug, but if i understood you wrong let me know.
if (target.x < weaponSpr.getPosition().x && weaponSpr.getScale().y > 0)
{
weaponSpr.setScale(weaponSpr.getScale().x, -weaponSpr.getScale().y);
}
if (target.x > weaponSpr.getPosition().x && weaponSpr.getScale().y < 0)
{
weaponSpr.setScale(weaponSpr.getScale().x, -weaponSpr.getScale().y);
}
{
weaponSpr.setScale(weaponSpr.getScale().x, -weaponSpr.getScale().y);
}
if (target.x > weaponSpr.getPosition().x && weaponSpr.getScale().y < 0)
{
weaponSpr.setScale(weaponSpr.getScale().x, -weaponSpr.getScale().y);
}
One thing that leaves a questionmark in my head, is that in that ratio y is also set to 76 which should offset my bullets spawning location way, way below the barrel, but it offsets only so little that when changing the y axis by -4 it spawns right on the barrel, perfectly.
I could've understood everything wrong and fixed it by accident which would be very weird, so let me know if what i said makes any sense.
PS. why 76?
Because its the changed x origin of my weapon which i wanted to be 32 and since i changed the weapons scale to 2,2 then i had to multiply it by 2. now we have 64 and what is left is to add the bullet x axis of the origin to that number which gives us 76.
EDIT: I shared my weapons texture as an attachment, so you can copy the code, apply it to that specific texture and see for yourself.