SFML community forums

Help => Graphics => Topic started by: AltF4ToWin on May 19, 2013, 01:18:39 pm

Title: [SFML 2.0] Spawn projectiles to mouse pointer problem
Post by: AltF4ToWin 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.
Title: Re: [SFML 2.0] Spawn projectiles to mouse pointer problem
Post by: eXpl0it3r 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.
Title: Re: [SFML 2.0] Spawn projectiles to mouse pointer problem
Post by: AltF4ToWin 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 (https://www.dropbox.com/s/fvp8j3642ob53c5/Asteroids.rar?v=0rc-s)
Title: Re: [SFML 2.0] Spawn projectiles to mouse pointer problem
Post by: Nexus on May 23, 2013, 08:23:19 pm
I don't know how to create a minimal example
It is described here (http://en.sfml-dev.org/forums/index.php?topic=5559.msg36368#msg36368).