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

Author Topic: How do i get the position of the center of my sprite?  (Read 11111 times)

0 Members and 1 Guest are viewing this topic.

gl0w

  • Newbie
  • *
  • Posts: 20
    • View Profile
How do i get the position of the center of my sprite?
« on: August 26, 2011, 08:59:55 pm »
Hello,

I want to get the position of the center of my sprite, so i have tried the following:

pac.SetCenter(0.5*BLOCKSIZE, 0.5*BLOCKSIZE);

But then when i use the pac.GetPosition().x/y it doesn't give me the center of the sprite, instead it gives me the upper left corner of the sprite.

How can i change this to work?

thanks

MorleyDev

  • Full Member
  • ***
  • Posts: 219
  • "It is not enough for code to work."
    • View Profile
    • http://www.morleydev.co.uk/
How do i get the position of the center of my sprite?
« Reply #1 on: August 26, 2011, 10:01:27 pm »
pac.GetPosition().x + pac.GetCenter().x?
UnitTest11 - A unit testing library in C++ written to take advantage of C++11.

All code is guilty until proven innocent, unworthy until tested, and pointless without singular and well-defined purpose.

gl0w

  • Newbie
  • *
  • Posts: 20
    • View Profile
How do i get the position of the center of my sprite?
« Reply #2 on: August 26, 2011, 10:58:49 pm »
Quote from: "MorleyDev"
pac.GetPosition().x + pac.GetCenter().x?



Hm.. well, never though about it. But i can try.

 I already tried using:
pac.TransformToGlobal(pac.GetCenter()).x;
pac.TransformToGlobal(pac.GetCenter()).y;

and then

PacPosX = pac.GetCenter().x
PacPosY = pac.GetCenter().y

but it doesn't work either.

thanks!

Lo-X

  • Hero Member
  • *****
  • Posts: 618
    • View Profile
    • My personal website, with CV, portfolio and projects
How do i get the position of the center of my sprite?
« Reply #3 on: August 27, 2011, 10:25:41 am »
SFML 1.6 I guess :

Code: [Select]
pac.SetCenter(pac.GetSize().x / 2 , pac.GetSize().y / 2);

gl0w

  • Newbie
  • *
  • Posts: 20
    • View Profile
How do i get the position of the center of my sprite?
« Reply #4 on: August 27, 2011, 03:03:11 pm »
Quote from: "Lo-X"
SFML 1.6 I guess :

Code: [Select]
pac.SetCenter(pac.GetSize().x / 2 , pac.GetSize().y / 2);


I tried that, with pac.SetCenter(0.5*BLOCKSIZE, 0.5*BLOCKSIZE);

And it actually set's the Center to 10/10, because i can see it on debug mode.
The problem is when i try to get the global position of the center on the screen with:

pac.TransformToGlobal(pac.GetCenter()).x;
pac.TransformToGlobal(pac.GetCenter()).y;

PacPosX = pac.GetCenter().x
PacPosY = pac.GetCenter().y

or

PacPosX = pac.GetPosition().x
PacPosX = pac.GetPosition().y

it still gives me the position of the upper left corner :(

SFML 1.6 here.

thanks

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
How do i get the position of the center of my sprite?
« Reply #5 on: August 27, 2011, 03:09:24 pm »
GetPosition() returns the position of the center, so it should be ok. However the position doesn't change when you set the center, instead it's the sprite that is moved on screen so that the position matches the new center.
Laurent Gomila - SFML developer

gl0w

  • Newbie
  • *
  • Posts: 20
    • View Profile
How do i get the position of the center of my sprite?
« Reply #6 on: August 28, 2011, 03:39:28 am »
Thanks for both of you. Really appreciated


pac.GetPosition().x + pac.GetCenter().x + pac.GetPosition().y + pac.GetCenter().y  works. I can get the center position of the sprite on the screen.

Not sure, if there is another way around for do that ?