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

Author Topic: Sprite to stay in Top Left Corner Help  (Read 2963 times)

0 Members and 1 Guest are viewing this topic.

Canvas

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Sprite to stay in Top Left Corner Help
« 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]

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Sprite to stay in Top Left Corner Help
« Reply #1 on: July 05, 2012, 09:55:13 pm »
I'm not sure if you've seen my tutorial on sf::View, 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));
« Last Edit: July 05, 2012, 09:57:03 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Canvas

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Re: Sprite to stay in Top Left Corner Help
« Reply #2 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
« Last Edit: July 05, 2012, 10:08:19 pm by Canvas »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Sprite to stay in Top Left Corner Help
« Reply #3 on: July 05, 2012, 10:09:14 pm »
Use again convertCoords()! ;)

sf::Vector2f mouseViewPos = window.convertCoords(sf::Mouse::getPosition(window));
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Canvas

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Re: Sprite to stay in Top Left Corner Help
« Reply #4 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();
 
« Last Edit: July 05, 2012, 10:18:55 pm by Canvas »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Sprite to stay in Top Left Corner Help
« Reply #5 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. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Canvas

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Re: Sprite to stay in Top Left Corner Help
« Reply #6 on: July 06, 2012, 12:04:06 am »
I got it, cheers for the help man
« Last Edit: July 06, 2012, 12:19:07 am by Canvas »

 

anything