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

Author Topic: Pixel Perfect Sprite Position checking?  (Read 2870 times)

0 Members and 1 Guest are viewing this topic.

aNewHobby

  • Jr. Member
  • **
  • Posts: 85
    • View Profile
    • Live 4 Ever Or Die Trying
Pixel Perfect Sprite Position checking?
« on: July 20, 2012, 08:51:53 am »
I am having a problem and I was hoping you guys would have a solution... as I am kinda stuck.

I have a sprite for the main character that is moving around the screen using a velocity and frame time to make a factor to adjust the sprite position. This is a float value. This means that the getPosition(); will return a float and it could be say 300.6348 300.2894 instead of 300,300 for screen cords.

I had planned to do a test on a single pixel to see if the main character was at that pixel. Though I can not get it to work as the sprite seams to just pass directly over it with out trigging the reaction.  I think this is because  the get position of get top/left for the "switch" is a whole number but the testing numbers for the sprite that is moving is in fractions.

I tried using ciel and floor to round the values.. but no real luck there either..

here is what I am trying to do...



Now the coloured squares would not be visible in the end but for now the "switches" can be seen. The idea was for the centre point of each square to test if the pacman centre point is in the same position, and if so do something. Pac-Man and the rects have the transform set to the centre.

I thought this would be trivial with some code something like this....

if (_switch.getPosition() == PacMan.getPosition()
{
//do somthing
}

but it just doesn't seam to work and I am not sure why or how to fix it... Any ideas?

// - - //
Some more info

Each Switch is 40,40px and PacMan is 40x40px. The main idea of this test was so that pacman can not change direction unless he is completely inside the switch. I can test is a point is in the switch or is a edge of pacmans bounding box hits the switch.. but I am unsure about how to do the center === center test if it is possible at all.
Twitter: @Jyinkies
My Own Little Spot on the Net: - Live4everOrDieTrying.info (Blog)

thePyro_13

  • Full Member
  • ***
  • Posts: 156
    • View Profile
Re: Pixel Perfect Sprite Position checking?
« Reply #1 on: July 20, 2012, 09:06:25 am »
Look up floating point equality.

Their are some issue that make using == with floats inaccurate when you get down a few decimal places. Although it's probably just that the positions are so precise that the odds of the pacman being in exactly the same place as the switch, down to a few decimal points is really low.
You should try converting the points to int before comparing them first(since you're probably placing your switches at integer positions).
« Last Edit: July 20, 2012, 09:28:33 am by thePyro_13 »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Pixel Perfect Sprite Position checking?
« Reply #2 on: July 20, 2012, 10:10:04 am »
Another problem coulde be, that your sprite never really gers to that position. I don't know how you calculate the movement, but since you're using a velocity, it's quite possible that the sprite is moving 2 or 3 pixels per frame, thus it can jump over that one pixel you want to check for.
What you could do is; first update the position, the check if the sprite intersects with the square beyond the square with the switch, if so change the position to match the switch position, otherwise move it forward and then draw it finally.
It's a bit more complicated, but it should work fine and not produce any visual ghost movement (getting draw into the wall fot one frame or similar).
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/