SFML community forums

Help => Graphics => Topic started by: Canvas on July 05, 2012, 09:44:47 pm

Title: Sprite to stay in Top Left Corner Help
Post by: Canvas on July 05, 2012, 09:44:47 pm
Hey guys

Im trying to get a sprite to stay in the top left corner, now im using a view like so
mainView.setCenter(512,384);
mainView.setSize(1024,768);
 

Now the red square is the player and the player is the center so i set up my view like so
mainView.setCenter(sf::Vector2f(player.pos.x+15,player.pos.y));
 

but im having some problems, i have a image that i want to go in the top left corner.
so i try this
interBtns.pos.x = v2.x;
interBtns.pos.y = v2.y;
 
now this puts it in the bottom, but how would i work it out so it will go to the top left corner? the screen resoultion is 800 by 600

[attachment deleted by admin]
Title: Re: Sprite to stay in Top Left Corner Help
Post by: eXpl0it3r on July 05, 2012, 09:55:13 pm
I'm not sure if you've seen my tutorial on sf::View (https://github.com/SFML/SFML/wiki/TutorialUsingView), it should give you some insight on how to use views.

To position your sprite you could use convertCoords().

interBtns.pos = window.convertCoords(sf::Vector2i(0, 0));
Title: Re: Sprite to stay in Top Left Corner Help
Post by: Canvas on July 05, 2012, 10:02:03 pm
Ok i will give that a go but also, how will i check collistion detection on the mouse clicking? the mouse x and y will be differnet as in the screen x could be 10 and y 10, but in the actually game the icon is at 756x and 1028y because of where the player is

The icon is in the top left corner but the cords are wrong so i cant check for collision
Mouse
X:54 Y:55
Icon
X:-387  Y:153
Title: Re: Sprite to stay in Top Left Corner Help
Post by: eXpl0it3r on July 05, 2012, 10:09:14 pm
Use again convertCoords()! ;)

sf::Vector2f mouseViewPos = window.convertCoords(sf::Mouse::getPosition(window));
Title: Re: Sprite to stay in Top Left Corner Help
Post by: Canvas on July 05, 2012, 10:13:04 pm
Ok this is what i have, the icon is in the top left corner but its abit delayed from the players movement, I think i have the update in the wrong place but i can sort that out later.
but this is what i have for the position

sf::Vector2f v2 = screen.convertCoords(sf::Vector2i(10, 10));
                        interBtns.pos.x = v2.x;
                        interBtns.pos.y = v2.y;
 

in my collision detection i have this

sf::Vector2f mouseViewPos = screen.convertCoords(sf::Mouse::getPosition(screen));
                                if(math.pointInRect(mouseViewPos.x,mouseViewPos.y,int(interBtns.pos.x),int(interBtns.pos.y),interBtns.pos.w,interBtns.pos.h) == true)
                                {
                                        cout << "correct" << endl;
                                }
 

the math just finds out if a point is inside a rectangle.

I still have a little problem with drawing

Lets say my player is falling, my buttons start to move downwards but they are maybe a few pixels off, why arent they just staying static?

here is a piece of update code

                player.update();
                mainView.setCenter(sf::Vector2f(player.pos.x+15,player.pos.y));
                        screen.draw(interBtns.icon);
                        sf::Vector2f v2 = screen.convertCoords(sf::Vector2i(10, 10));
                        interBtns.pos.x = v2.x+(80);
                        interBtns.pos.y = v2.y;
                        interBtns.update();
 
Title: Re: Sprite to stay in Top Left Corner Help
Post by: eXpl0it3r on July 06, 2012, 12:02:48 am
Okay a better way to draw static objects is to have a second view, that doesn't get changed. So you got one view represents the game and can get moved in all directions and you have a view that is static and you can render to it all your GUI elements etc.

Now before you draw your things you can just change the views:
window.setView(game);
window.draw(object);
//...
window.setView(fixed);
window.draw(gui);

Also PM stands for Personal Message, so as long as you don't have anything to say personally, please don't write me there.
If you need help even faster, then I can only advice you to join the unofficial IRC channel (http://en.sfml-dev.org/forums/index.php?topic=2997.0). ;)
Title: Re: Sprite to stay in Top Left Corner Help
Post by: Canvas on July 06, 2012, 12:04:06 am
I got it, cheers for the help man