In a game I'm making, guns have a spread (float) and I want to give each bullet's angle a random value from range [-spread, spread]. For this I thought I could use glm::rotate, but the problem is that the bullets spread in almost every direction.
The code I use is:
void Gun::fire(const glm::vec2& direction, const glm::vec2& position, std::vector<Bullet>& bullets) {
static std::mt19937 randomEngine(time(nullptr));
// For offsetting the accuracy
std::uniform_real_distribution<float> randRotate(-_spread, _spread);
for (int i = 0; i < _bulletsPerShot; i++) {
// Add a new bullet
bullets.emplace_back(position,
glm::rotate(direction, randRotate(randomEngine)),
_bulletDamage,
_bulletSpeed);
}
}
(At the top I included vector and glm/gtx/rotate_vector.hpp)