Trigonometry problem C++

I am trying to make a 2D shooter using C++ and SFML 2.1. A machine gun is positioned somewhere and it turns towards the mouse pointer. Now I need to implement shooting. I am stuck. How do I calculate the bullet trajectory?

I think for just showing a bullet I need to just make a bool variable which stores true or false depending on should the bullet be shown on the screen. But for moving the bullet, I have no clue what am I supposed to do? I think that this has something to do with trigonometry, just like rotating the gun towards the mouse pointer, or it is something else?

CODE:

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <SFML\Graphics.hpp>
#include <SFML\Window.hpp>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <vector>
#include <array>

#include "Player.h"
#include "Bullet.h"

using namespace std;

bool init();
void loadContent();
void update();
void draw();
void cleanup();

int main(int argc, char** argv)
{
	sf::ContextSettings settings;
	settings.antialiasingLevel = 8;
	settings.majorVersion = 3;
	settings.minorVersion = 3;
	sf::RenderWindow window(sf::VideoMode(800, 600, 32), "Smiley invasion", sf::Style::Default, settings);
	
	sf::Texture tex;
	tex.loadFromFile("machine_gun_1.PNG");
	tex.setSmooth(true);

	Player player(sf::Vector2f(200, 300), 100, tex);
	player.playerSprite.setScale(sf::Vector2f(0.3f, 0.3f));

	// game loop
	while (window.isOpen())
	{
		sf::Event evt;
		while (window.pollEvent(evt))
		{
			if (evt.type == sf::Event::Closed)
			{
				window.close();
			}
			if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left))
			{
				player.fire();
			}
		}

		// update code here
		player.update(window);

		window.clear(sf::Color::White);
		// drawing code here
		player.draw(window);

		window.display();
	}
}


Player.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include "Player.h"

Player::Player(sf::Vector2f _pos, int _hp, sf::Texture _tex)
{
	pos = _pos;
	hp = _hp;
	ammo = 250;
	texture = _tex;
	playerSprite = sf::Sprite(texture);
	playerSprite.setOrigin(sf::Vector2f(texture.getSize().x / 2, texture.getSize().y / 2));
	playerSprite.setPosition(pos);
}

void Player::fire()
{	
}

void Player::update(sf::RenderWindow& window)
{
	pos = playerSprite.getPosition();
	sf::Vector2i mouse = sf::Mouse::getPosition(window);
	float a = (float) mouse.x - pos.x;
	float b = (float) mouse.y - pos.y;
	float velocity = 0.1f;
	float angle = -atan2(a, b) * 180 / 3.141;
	playerSprite.setRotation(angle + 180);
}

void Player::draw(sf::RenderWindow& window)
{
	window.draw(playerSprite);
}


Player.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef PLAYER_H
#define PLAYER_H

#include <iostream>
#include <SFML\Graphics.hpp>
#include <SFML\System.hpp>
#include "Bullet.h"

class Player
{
public:
	sf::Vector2f pos;
	sf::Sprite playerSprite;
	sf::Texture texture;
	int hp;
	int ammo;
	Player(sf::Vector2f, int, sf::Texture);
	void fire();
	void update(sf::RenderWindow&);
	void draw(sf::RenderWindow&);
};

#endif 


Bullet.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include "Bullet.h"

Bullet::Bullet(sf::Texture _tex, sf::Vector2f _pos, float _rotation)
{
	bulletTex = _tex;
	pos = _pos;
	rotation = _rotation;

	bullet.setTexture(bulletTex);
	bullet.setScale(sf::Vector2f(0.3f, 0.3f));
	bullet.setPosition(pos);
	bullet.setRotation(rotation);
}

Bullet::~Bullet()
{
	
}

void Bullet::update(sf::RenderWindow& window)
{

}

void Bullet::draw(sf::RenderWindow& window)
{
	if (isActive)
	{
		window.draw(bullet);
	}
}


Bullet.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef BULLET_H
#define BULLET_H

#include <iostream>
#include <SFML\Graphics.hpp>
#include <SFML\Window.hpp>

class Bullet
{
public:
	sf::Sprite bullet;
	sf::Texture bulletTex;
	sf::Vector2f pos;
	float rotation;
	bool isActive = false;

	Bullet(sf::Texture _tex, sf::Vector2f _pos, float _rotation);
	~Bullet();
	void update(sf::RenderWindow& window);
	void draw(sf::RenderWindow& window);
};

#endif 


Thanks in advance.
Last edited on
If the turret is in position (Tx,Ty) and the mouse in position (Mx,My) then you can obtain the direction of movement by doing
ux = Tx-Mx
uy = Ty-My


And then normalize
norm = sqrt(ux*ux + uy*uy)
ux = ux/norm
uy = uy/norm


the movement may be described as
Bx = ux |v| t + Tx
By = uy |v| t + Ty


where |v| is speed of the bullet
and t is he time passed
Now the gun shoots with different rates of fire (does this has to do something with not including time in bx=ux*v*t+tx and the same with y) and it goes crazy when I point the gun in a different direction.
What am I doing wrong?

Thanks in advance.
Last edited on
> Now the gun...
> What am I doing wrong?
In order to know what you are doing wrong, we need to know what you are doing.


> does this has to do something with not including time
if you haven't included time, then the bullet would not move


> it goes crazy when I point the gun in a different direction.
I guess that your mouse coordinates refer to the window, not to the model.
There may be differences between scale (e.g., 640x480), origin (upper left corner) and axis orientation (y grows down).

Also the formula was incorrect, it should be
ux = Mx-Tx
uy = My-Ty
so it points towards the mouse.
Topic archived. No new replies allowed.