在我製作的遊戲中,槍具有傳播(浮動),我希望給每個子彈的角度範圍內的隨機值[-spread,spread]。爲此,我想我可以使用glm::rotate
,但問題是子彈幾乎散佈在每個方向。用glm旋轉2D矢量
我使用的代碼是:
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);
}
}
(在頂部,我包括vector
和glm/gtx/rotate_vector.hpp
)
從http://glm.g-truc.net/0.9.5/updates.html'最後,這裏是所有可能在GLM 0.9.5.4使用度的功能列表,需要GLM 0.9.6中的弧度:rotate(矩陣和四元數),[etc.] – vaxquis
_randRotate(randomEngine))_部分返回度數,glm :: rotate需要弧度。這就是解決方案!謝謝! :) – Gykonik