View movement while sprites warp boundaries causes bug

Using SFML.

I'm setting up a sort of 'fake' parallax background by using sprites of stars, they warp fine when moving on their own(i.e of their own speed) but because they are the background I need them to warp when they are outside of a moving view which at the moment is just a click/drag-style screen movement.

the problem is that it if I move towards the top-left corner (thus moving all sprites to the bottom-right corner, at which point they activate when they are outside the view and warp to the opposite end, but they don't warp to the top-left, instead they just build up in the bottom-left and top-right corner of the screen and shoot off.

moving to the top-right corner/bottom-left causes the bottom/top half of a triangle (the screen is a square) to contain no stars, with them all building up in the left side of the screen, etc.

its not always like that though unless it is the first time, but it never works right unless I'm only moving in one direction, its a little hard to explain so here's the code and some pictures.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left))
            {
                sf::Vector2f pos = window.mapPixelToCoords(sf::Mouse::getPosition(window), MouseView);
                MouseView.move((pos.x - MouseView.getCenter().x) * 0.01, (pos.y - MouseView.getCenter().y) * 0.01);
                window.setView(MouseView);
            }

            double halfX = tStar.getSize().x / 2;
            double halfY = tStar.getSize().y / 2;

            int xRandUpper = 320 + halfX + static_cast<int>(MouseView.getCenter().x);
            int yRandUpper = 320 + halfY + static_cast<int>(MouseView.getCenter().y);

            std::cout << xRandUpper << "/" << yRandUpper << "\n";
            for (sf::Sprite& star: Stars)
            {
                //star.move(10, 10);

                int xRand = (xRandUpper != 0)? std::rand() % xRandUpper : 0;
                int yRand = (yRandUpper != 0)? std::rand() % yRandUpper : 0;

                if (star.getPosition().x < MouseView.getCenter().x - 320 - halfX) //left
                {
                    star.setPosition(320 + MouseView.getCenter().x + halfX, yRand);
                }
                else if (star.getPosition().y < MouseView.getCenter().y - 320 - halfY) //top
                {
                    star.setPosition(xRand, 320 + MouseView.getCenter().y + halfY);
                }
                else if (star.getPosition().x > MouseView.getCenter().x + 320 + halfX) //right
                {
                    star.setPosition(-320 + MouseView.getCenter().x - halfX, yRand);
                }
                else if (star.getPosition().y > MouseView.getCenter().y + 320 + halfY) //star goes out of the bottom of the screen
                {
                    star.setPosition(xRand, -320 + MouseView.getCenter().y - halfY);
                }
            }


http://imgur.com/uSUzVG3

the red star is the middle of the world, the outer lines of the rectangles are the boundary at which the stars should warp to the other side (note though that length-wise it is infinite, in SFML drawing a simple line isn't simple, so drawing a rectangle was a quicker way of checking the boundaries were right)

http://imgur.com/YhHoW6C

this is what happens when I move to the top-left corner, half the stars push downwards but the code to check if they are past the bottom doesn't seem to be warping them correctly

http://imgur.com/Yudb1Za

the half-triangle issue, moving to the top-right or bottom-left causes this

the only movement which doesn't have any problems initially is moving to the bottom-right, unless you moved in one of the other directions first, then it causes a similar half-triangle problem.

I'm sorry I can't explain any better, I've been trying to fix this for a few hours now but I just can't do it and I don't have any idea what part of the code is causing it, is it the way I move the view? or is it some bug in the warping?

while writing this I realized that when xRandUpper and/or yRandUpper are negative it causes the problem (top-left both are negative, top-right y is negative), rand() negative gives a positive number though, not 0, so not sure if it is coincidence or not.

I fixed it :] I was essentially doing the calculation in the two randUpper variables, which don't even need to exist, not sure why I created them.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
            int xSpeed = 0;
            int ySpeed = 0;

            if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left))
            {
                sf::Vector2f pos = window.mapPixelToCoords(sf::Mouse::getPosition(window), MouseView);
                MouseView.move((pos.x - MouseView.getCenter().x) * 0.01, (pos.y - MouseView.getCenter().y) * 0.01);
                window.setView(MouseView);
                xSpeed = (pos.x - MouseView.getCenter().x) / 640;
                ySpeed = (pos.y - MouseView.getCenter().y) / 640;
            }

            for (sf::Sprite& star: Stars)
            {
                double halfX = tStar.getSize().x / 2;
                double halfY = tStar.getSize().y / 2;

                star.move(xSpeed, ySpeed);

                int xRand = std::rand() % 640;
                int yRand = std::rand() % 640;

                if (star.getPosition().x < MouseView.getCenter().x - 320 - halfX) //left
                {
                    star.setPosition(320 + MouseView.getCenter().x + halfX, MouseView.getCenter().y - 320 + yRand);
                }
                else if (star.getPosition().y < MouseView.getCenter().y - 320 - halfY) //top
                {
                    star.setPosition(MouseView.getCenter().x - 320 + xRand, 320 + MouseView.getCenter().y - halfY);
                }
                else if (star.getPosition().x > MouseView.getCenter().x + 320 + halfX) //right
                {
                    star.setPosition(-320 + MouseView.getCenter().x - halfX, MouseView.getCenter().y - 320 + yRand);
                }
                else if (star.getPosition().y > MouseView.getCenter().y + 320 + halfY) //star goes out of the bottom of the screen
                {
                    star.setPosition(MouseView.getCenter().x - 320 + xRand, -320 + MouseView.getCenter().y - halfY);
                }
            }
Last edited on
Topic archived. No new replies allowed.