Hello,
I am working on a game that so far has:
- A square moving around with a basic velocity system (wasd)
- The Square also rotates based on mouse position
I am now trying to implement bullets that will fire in the direction the square (player) is rotated, I made a very simple system with the last game I made (thanks again Hapax) that only went in one direction.
The bullets fire (yay), just not at the right angle (boo). I debugged and looked at the rotation each time and the numbers matched....it just doesn't come out correctly visually.
Thanks!
This is main.cpp (I chopped a major chunk of code to isolate what the issue may be)
#include "Player.h"
#include "Bullet.h"
#include <iostream>
int main()
{
//declare
float delta, deltaCd, bulletTime, bulletCd, bulletSpeed, bulletAngle;
float& dCd = deltaCd;
sf::Clock gameClock, bulletClock;
Player playerOne;
Player& pO = playerOne;
Bullet playerBullet;
Bullet& pB = playerBullet;
std::vector<sf::RectangleShape> bulletVector;
std::vector<float> bulletAngleVector;
//initialize
delta = .0f;
deltaCd = .25f;
bulletTime = 1.0f;
bulletCd = 2.0f;
bulletSpeed = 1.5f;
bulletAngle = .0f;
playerBullet.bulletShape.setPosition(sf::Vector2f(playerOne.playerShape.getPosition().x,
//window loop
//TODO: Create game loop
while (win.isOpen())
{
window.clear();
delta = gameClock.restart().asSeconds();
playerOne.PlayerRotation(win, pO.playerShape);
playerBullet.BulletAngle(win, pB.bulletShape);
if (sf::Mouse::isButtonPressed(sf::Mouse::Left) && bulletTime > bulletCd)
{
bulletTime = bulletClock.restart().asSeconds();
playerBullet.bulletShape.setPosition(sf::Vector2f(playerOne.playerShape.getPosition().x, playerOne.playerShape.getPosition().y));
bulletAngle = playerBullet.bulletShape.getRotation();
bulletVector.push_back(playerBullet.bulletShape);
bulletAngleVector.push_back(bulletAngle);
}
for (unsigned int i = 0; i < bulletVector.size(); i++)
{
bulletVector[i].move(cos(bulletAngleVector[i]), sin(bulletAngleVector[i]));
window.draw(bulletVector[i]);
}
bulletTime = bulletClock.getElapsedTime().asSeconds();
window.draw(pO.playerShape);
window.display();
}
}
bullet.cpp
#include "Bullet.h"
Bullet::Bullet()
{
bulletSizeX = 5.f;
bulletSizeY = 5.f;
bulletShape.setSize(sf::Vector2f(bulletSizeX, bulletSizeY));
bulletShape.setOrigin(sf::Vector2f(bulletShape.getGlobalBounds().width / 2.f, bulletShape.getGlobalBounds().height / 2.f));
bulletShape.setFillColor(sf::Color::Red);
}
void Bullet::BulletAngle(sf::RenderWindow& win, sf::RectangleShape& bS)
{
dx = 0.f;
dy = 0.f;
mouse = sf::Mouse::getPosition(win);
mouseWorld = win.mapPixelToCoords(mouse);
dx = mouseWorld.x - bS.getPosition().x;
dy = mouseWorld.y - bS.getPosition().y;
rotation = atan2(dy, dx) * 180 / PI;
bS.setRotation(rotation);
}
bullet.h
#pragma once
#include <SFML/Graphics.hpp>
class Bullet
{
public:
float bulletSizeX, bulletSizeY, dx, dy, rotation;
const float PI = 3.14159265f;
sf::Vector2i mouse;
sf::Vector2f mouseWorld;
sf::RectangleShape bulletShape;
void BulletAngle(sf::RenderWindow& win, sf::RectangleShape& bS);
Bullet();
};