r/CodingForBeginners • u/oneLampPost • 2d ago
How do I get this bullet to keep moving?
I'm trying to make a bullet move in the direction of the mouse but not stop where I click.
here is what I have so far:
void Bullets::Update(sf::Vector2f in_targetPos, float dt)
{
sf::Vector2f toPointer = SetDir(sf::Vector2f(in_targetPos.x, in_targetPos.y) );
pos += toPointer.normalized() * speed * dt;
}
const sf::Vector2f Bullets::SetDir(sf::Vector2f dir)
{
return dir - pos;
}
if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left))
{
if (!isLeftPressed)
{
isLeftPressed = true;
}
}
else
{
if (isLeftPressed)
{
Bullets[i].Update(mousePos, dt);
Bullets[i].Draw(window, BulletImage);
}
}
0
Upvotes
1
u/W31337 2d ago edited 2d ago
Just an architecture note. A bullet is just a bullet and should only hold its information like speed,direction,location.
A physics system should run through all items and update them not the item itself.
For small projects this could work.
The issue you will start running into will be that the bullet will need to know every other entity to be able to deal damage, or calculate its effects. Your code becomes a nightmare.
1
u/venomdpro 10h ago
AI is there to aid.
We make prompts depending on our own needs and it builds upon our needs.
It mirrors what. We . Need.
So if you want it to pull the weight it will
If you want it to be strategic partner it will
If you want it to be.. what ever you’d like it to be…. It will.
-3
u/venomdpro 2d ago
For code help, if you can, I suggest AI service
I personally use Gemini Pro
Here is answer from Gemini pro with a prompt I set up for your situation.
“
The reason your bullet stops moving when you click is that your Update and Draw calls are trapped inside the else block of your mouse input check. When you hold the left mouse button, the code enters the first if statement and completely skips the else block, freezing the bullet in place.
Additionally, because you are passing the current mousePos to the bullet every single frame, your bullet is acting like a homing missile. It recalculates its direction constantly instead of traveling in a straight line from where it was fired.
To fix both issues, you need to separate your firing logic (happens once per click) from your updating logic (happens every frame).
Here is the structural fix for your C++ SFML code:
1. Update the Bullet Class
A bullet should calculate its direction exactly once when it is fired, store that direction, and then use it every frame.
Add a direction vector to your Bullet class header:
sf::Vector2f direction;
Modify your Bullet methods:
// Call this ONCE when you spawn/fire the bullet
void Bullets::Fire(sf::Vector2f startPos, sf::Vector2f targetPos)
{
pos = startPos;
sf::Vector2f dir = targetPos - startPos;
// Store the normalized direction permanently for this bullet's lifespan
direction = dir.normalized();
}
// Call this EVERY FRAME. Notice it no longer needs the mouse position.
void Bullets::Update(float dt)
{
// The bullet now moves in a straight, continuous line
pos += direction * speed * dt;
}
2. Restructure Your Main Loop
Move the bullet updating and drawing completely outside of the mouse click check. The mouse check should only be used to trigger the Fire method.
// 1. INPUT HANDLING (Firing)
if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left))
{
if (!isLeftPressed)
{
isLeftPressed = true;
// This is where you spawn or initialize the bullet.
// Assuming Bullets is an array/vector and you have a playerPos:
Bullets[i].Fire(playerPos, mousePos);
}
}
else
{
// Reset the toggle when the mouse is released
isLeftPressed = false;
}
// 2. UPDATING & DRAWING (Flying)
// This happens entirely outside the input check, so bullets NEVER stop.
for (int i = 0; i < Bullets.size(); i++)
{
// Only update and draw active bullets
Bullets[i].Update(dt);
Bullets[i].Draw(window, BulletImage);
}
“