Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [SFML 2.0] Spawn projectiles to mouse pointer problem  (Read 2391 times)

0 Members and 1 Guest are viewing this topic.

AltF4ToWin

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
[SFML 2.0] Spawn projectiles to mouse pointer problem
« on: May 19, 2013, 01:18:39 pm »
Basically, I have a function that has a spaceship follow the mouse pointer, which works perfectly.

        void lookAtMouse(sf::RenderWindow &app)
        {
                sf::Vector2f curPos = sprite.getPosition();
                sf::Vector2i position = sf::Mouse::getPosition(app);

                const float PI = 3.14159265;

                float dX = curPos.x - position.x;
                float dY = curPos.y - position.y;

                float rotation = (atan2(dY, dX)) * 180 / PI;

                sprite.setRotation(rotation - 180);
        }
 

I've tried to modify this to work for the projectiles it shoots, however it doesn't work and causes all kinds of graphical glitches, which I think I know why.

void MoveBullet(int numOfBullets)
{
        for (int i = 0; i < numOfBullets; i++)
        {
                                        sf::Vector2f curPos = bullet[i].getPosition();
                                        sf::Vector2i position = sf::Mouse::getPosition(app.window);

                                        const float PI = 3.14159265;

                                        float dX = curPos.x - position.x;
                                        float dY = curPos.y - position.y;

                                        float rotation = (atan2(dY, dX)) * 180 / PI;

                                    if (bullet[i].exists) bullet[i].Move(dX, dY);
        }
}
 
How would I approach having the projectiles fire to wherever the mouse pointer is? I can provide full source if required.
« Last Edit: May 19, 2013, 01:22:13 pm by AltF4ToWin »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: [SFML 2.0] Spawn projectiles to mouse pointer problem
« Reply #1 on: May 20, 2013, 04:02:09 pm »
Is it intended, that the last Move call will move the particle to opposite position of the mouse instantly?
dX and dY form the mathematical vector from the mouse to the sprite, but if you want to move a bullet from the sprite to the mouse position, you'll need to negate the vector.
If you want it to slowly travel there, you might want to implement the movement differently.

You should make a minimal and complete example, so we can actually test it and see what goes wrong. Currently we're missing quite a bit of information and can mostly guess.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

AltF4ToWin

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: [SFML 2.0] Spawn projectiles to mouse pointer problem
« Reply #2 on: May 23, 2013, 05:07:41 pm »
I don't know how to create a minimal example, but here's a link to the whole VS solution.

https://www.dropbox.com/s/fvp8j3642ob53c5/Asteroids.rar?v=0rc-s

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: [SFML 2.0] Spawn projectiles to mouse pointer problem
« Reply #3 on: May 23, 2013, 08:23:19 pm »
I don't know how to create a minimal example
It is described here.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything