1
General / [SOLVED] Setting bullets position to a weapon sprite that changes its rotation
« on: June 19, 2024, 10:36:44 pm »
I'm making my first sfml game, where so far i have a player that can move and shoot from the weapon that changes rotation so it always points towards mouse position.
My weapon sprites dimensions are 32x16 so i set the origin to 0,8 so the weapon rotates from the stock.
There is one problem.
When i set my bullets position to my weapon sprite, naturally it spawns the bullets at its origin which means that the weapon shoots from the stock which i definitely don't want.
I want my bullets to shoot from the barrel instead, but im struggling to put the math together in my mind.
It could be simple, and maybe im just dumb, but basically i want my bullets to spawn at the weapons sprite position with the origin of 32,8 instead of 0,8 without changing the origin, because obviously it would change the point of my rotation which i don't want.
Im thinking that probably something with trigonometry would bring me the desired result, but i can't get the idea how to do that.
Here is the part of the code:
My weapon sprites dimensions are 32x16 so i set the origin to 0,8 so the weapon rotates from the stock.
There is one problem.
When i set my bullets position to my weapon sprite, naturally it spawns the bullets at its origin which means that the weapon shoots from the stock which i definitely don't want.
I want my bullets to shoot from the barrel instead, but im struggling to put the math together in my mind.
It could be simple, and maybe im just dumb, but basically i want my bullets to spawn at the weapons sprite position with the origin of 32,8 instead of 0,8 without changing the origin, because obviously it would change the point of my rotation which i don't want.
Im thinking that probably something with trigonometry would bring me the desired result, but i can't get the idea how to do that.
Here is the part of the code:
[void Weapons::Shoot(float deltaTime, const sf::Vector2f& weaponPos, const sf::Vector2f& target)
{
//updates weapon sprites position to the players position
weaponSpr.setPosition(Player::sprite.getPosition());
//an angle for the weapon rotation
gunAngle = (atan2(target.y - weaponSpr.getPosition().y, target.x - weaponSpr.getPosition().x));
weaponSpr.setRotation(gunAngle * 180 / M_PI);
//flipping the weapon sprite if mouse points to the left side of the player which fixes visual bug
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);
}
//generating bullets
if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left) && clock.getElapsedTime().asSeconds() > fireRate)
{
bullet.setPosition(weaponSpr.getPosition());
bullets.push_back(bullet);
//bullet direction
angles.push_back(atan2(target.y - weaponSpr.getPosition().y, target.x - weaponSpr.getPosition().x));
//rotation for the bullet so it points towards mouse
rotation.push_back(atan2(target.y - bullets.back().getPosition().y, target.x - bullets.back().getPosition().x) * 180 / M_PI)
clock.restart();
}
//moving the bullets
for (int i = 0; i <= bullets.size() - 1 && bullets.empty() == false; i++)
{
bullets[i].move(cos(angles[i]) * bulletSpeed * deltaTime, sin(angles[i]) * bulletSpeed * deltaTime);
bullets[i].setRotation(rotation[i]);
...
]
{
//updates weapon sprites position to the players position
weaponSpr.setPosition(Player::sprite.getPosition());
//an angle for the weapon rotation
gunAngle = (atan2(target.y - weaponSpr.getPosition().y, target.x - weaponSpr.getPosition().x));
weaponSpr.setRotation(gunAngle * 180 / M_PI);
//flipping the weapon sprite if mouse points to the left side of the player which fixes visual bug
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);
}
//generating bullets
if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left) && clock.getElapsedTime().asSeconds() > fireRate)
{
bullet.setPosition(weaponSpr.getPosition());
bullets.push_back(bullet);
//bullet direction
angles.push_back(atan2(target.y - weaponSpr.getPosition().y, target.x - weaponSpr.getPosition().x));
//rotation for the bullet so it points towards mouse
rotation.push_back(atan2(target.y - bullets.back().getPosition().y, target.x - bullets.back().getPosition().x) * 180 / M_PI)
clock.restart();
}
//moving the bullets
for (int i = 0; i <= bullets.size() - 1 && bullets.empty() == false; i++)
{
bullets[i].move(cos(angles[i]) * bulletSpeed * deltaTime, sin(angles[i]) * bulletSpeed * deltaTime);
bullets[i].setRotation(rotation[i]);
...
]