Sign in to follow this  

[SFML] [C++] Bullet angle problem

This topic is 833 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So I have a top down game but, as cool as it looks, when I press and hold the mouse, all the bullets will travel to that new position... Not really sure how else to handle this really so any input would be great. 

	this->mouse = window.mapPixelToCoords(sf::Mouse::getPosition(window));

	float a = player.playerPosition.x - mouse.x;
	float b = player.playerPosition.y - mouse.y;

	// Shoot the projectile
	if (sf::Mouse::isButtonPressed(sf::Mouse::Left)){
		proj.rect.setPosition(player.playerSprite.getPosition());
		mouseAngle = (atan2(b, a)) * 180 / PI;
		projArray.push_back(proj);
	}

	proj.bulletLifetime++;

	if (proj.bulletLifetime >= proj.lifeTime)
	{
		proj.destroy = true;
		// Handle bullet destroy
	}

	projCounter = 0;

	// Iterate over the projectile
	for (iter = projArray.begin(); iter != projArray.end(); iter++){
		projArray[projCounter].rect.move(std::cos(PI * mouseAngle / 180.f) * 8.0f *-1, std::sin(PI * mouseAngle / 180.f) * 8.0f *-1);
		window.draw(projArray[projCounter].rect);
		projCounter++;
	}

gif: https://gyazo.com/5978b9b9e538f1a803a2ae4f883db7c1

Share this post


Link to post
Share on other sites

Create a simple struct to represent each bullet. It should store all the information about the bullet that it represents, including position, velocity, and life timer. When you update the bullets you add the velocity to the position in order to get the new position, and you decrement the life timer. When drawing the scene, just loop through the bullets and draw each one at its current position using the same graphic (this will improve graphics performance, since SFML does not re-bind the texture if it's the same as the previously used one).

 

Example pseudocode:

 

* if mouse button is down

** add a bullet to the vector (may want to add a firing delay or whatever)

** set its position and life timer

** set the velocity to be the direction toward the mouse times the speed it should travel at

* end "if"

 

* for each bullet in the vector

** decrement life timer

** if life timer > 0

*** position = position + velocity

** end "if"

* end "for"

 

* iterate vector in reverse

** if life timer <= 0

*** remove the bullet

** end "if"

* end "loop"

 

// in drawing section

* for each bullet in the vector

** if life timer > 0

*** draw the bullet at its current position

** end "if"

* end "for"

 

Alternatively, you can create an array with a length indicating the maximum number of bullets that can exist at one time, then skip updating and drawing bullets whose life is expired. When creating a bullet in this case you would simply move through the array looking for a position with an expired bullet. Remember to initialize the array such that all of the bullets start out as expired.

Share this post


Link to post
Share on other sites

This topic is 833 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this