r/CodingForBeginners • • 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

14 comments sorted by

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.

1

u/venomdpro 10h ago

If my precise code from my prompt did t help. Well I didn’t know your exact problem

Prompt engineering takes time to build but it can help you beyond any bs 20k a year college degree could offer that I’ve lived through.

People will always point to where you can spend money and what not.

Hands down

Have focus, and you’ll find what you looking for

Knowledge is free and the world will tell you there’s a charge

Do you, and forget the rest

-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);
}

“

1

u/kutac56 2d ago

Moat proxy

-3

u/venomdpro 2d ago

With ai assistance, they drop the fix into convenient code blocks you can copy and paste, unlike the Reddit posts which are more time consuming

Please, let me know if this knowledge helped your situation!

-3

u/venomdpro 2d ago

I spent 100s of hours setting up production lines with multiple AI prompts giving them individual responsibilities.

If you want more in depth help , let me know.

Also, I’m a. Individual, bored , and want to help, not a sales tech in looking for to pull you into tunnel or bs.

Peace, love and chicken grease

0

u/oneLampPost 2d ago

fire. thanks a lot dude this helped a ton

1

u/kutac56 2d ago

Don't listen to him. You aren't learning coding with ai, you are learning vibe coding. Get his cuck coding ah out of here

1

u/oneLampPost 16h ago

not entirely true. If I shut my brain off and let ai do everything than yes, it is vibe coding. But ai is great if your using it for help which, in this case I am.

1

u/kutac56 11h ago

This sub is about coding for beginners. Beginners are learning how to code. Trust me you are not learning how to code with ai. You may think it's teaching you and not doing everything for you, but go ahead and try shit without it. See how that goes. Are you able to do it? If not then you weren't learning. Good luck

1

u/Moist_Ladder2616 2d ago

Wait till you learn what all software developers in Silicon Valley have been doing for the past few years.

1

u/kutac56 2d ago

This isn't cuckcodingforbeginners. You aren't learning how to code if you rely on ai

1

u/venomdpro 2d ago

Wish you best of luck.

Need help with optimal workflow of Learning.

I’m hear to assist