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

Author Topic: Sprites not positioning the way I established  (Read 2914 times)

0 Members and 1 Guest are viewing this topic.

devhobby

  • Newbie
  • *
  • Posts: 7
    • View Profile
Sprites not positioning the way I established
« on: December 21, 2017, 11:46:08 pm »
Hello.

I'm making the Dame, a game similar to chess.

This is the board (screenshot): http://prntscr.com/hqr0hg

I'm trying to position the pawns into the Black cells through this algorythm

Quote
sprites[i++].setPosition(Vector2f(sizeEdge + sizeMargin + ((currentSquare - 1)*sizeRectangle), sizeEdge + sizeMargin + sizeRectangle*r));

currentSquare += 2;      // the next black slot can be found after 2 slots   

sizeEdge and sizeMargin are two areas of the board the pawn needs to move away from.

Here is an image that illustrates this: http://prntscr.com/hqr4ru

The edge is not part of the board and the margin is required to center the pawn into the rectangle.

(currentSquare - 1)*sizeRectangle is a simple calculation to get the number of rectangles the pawn needs to move away from (because they're either white - inaccesible - or occupied by other pawns before)

The result of the algorythm is this: http://prntscr.com/hqr61m

As you can see (ignore the white pawn) the more we move to the right, the more the position of the pawns is uncorrect.

And this also repeats for the rows below.

I can't understand where's the issue here. My mathematical formula looks correct... what is causing this issue?

Thanks in advance!

dambal

  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: Sprites not positioning the way I established
« Reply #1 on: December 22, 2017, 12:40:02 am »
float x = sizeEdge + ((currentSquare - 1) * (sizeRectangle + sizeMargin));
float y = sizeEdge + (r * (sizeRectangle + sizeMargin));

sprites[i++].setPosition(Vector2f(x,y));

currentSquare += 2;
 

Try this and tell me if it is working as you wish, I can't really test it so I am not sure if it is right.

devhobby

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Sprites not positioning the way I established
« Reply #2 on: December 22, 2017, 12:52:34 am »
Hi, thanks for the answer.

Unofortunately that didn't solve the problem. Conversely, the problem accentuates: http://prntscr.com/hqrvyg

I'd like to point out that sizeEdge, sizeMargin and sizeRectangle are all integer values

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Sprites not positioning the way I established
« Reply #3 on: December 22, 2017, 08:59:15 am »
Your cells are not 72px wide, on the last screenshot I find only 70px.
Laurent Gomila - SFML developer

devhobby

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Sprites not positioning the way I established
« Reply #4 on: December 22, 2017, 02:52:42 pm »
Changing the sizeRectangle from 72 to 70 made things way better!

Screenshot: http://prntscr.com/hr0681

The problem by the way is not 100% solved: the distance between the Pawn and the Left edge of the rectangle is not the same for every Pawn in the same row

As you can see: http://prntscr.com/hr07lw

The distance between the Pawn and the rectangle gets smaller and smaller

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Sprites not positioning the way I established
« Reply #5 on: December 22, 2017, 02:57:33 pm »
I don't know how this board was produced, but dividing the total width (564) by the total number of cells in a row (8) gives an average cell size of 70.5 pixels. And given the results for 70 and 72, I bet it will give the correct positioning.

Or maybe it's 70 for white cells and 71 for black cells, who knows... ;)
Laurent Gomila - SFML developer

devhobby

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Sprites not positioning the way I established
« Reply #6 on: December 22, 2017, 03:08:32 pm »
Thank you very much, it looks nice now!

So I was adding 1.5px more than required

Last thing though: when changing this calculation I notice a little misprint of the Pawn http://prntscr.com/hr0e9k

By setting setSmooth(true) on the Texture, the problem seems to be solved: http://prntscr.com/hr0g60

My question is: is there a particular reason for this phenomenon to happen only when I changed just a simple variable from const unsigned int sizeRectangle = 72 to const float sizeRectangle = 70.5f ?


Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Sprites not positioning the way I established
« Reply #7 on: December 22, 2017, 04:15:47 pm »
Yes: pixels are whole, so a decimal coordinate has to be rounded and automatically produces visual artifacts.
Laurent Gomila - SFML developer

Tigre Pablito

  • Full Member
  • ***
  • Posts: 226
    • View Profile
    • Email
Re: Sprites not positioning the way I established
« Reply #8 on: December 22, 2017, 09:24:41 pm »
Why don´t you draw an equal size cell checkerboard? I don´t think it would be so hard, and surely mathematical things would be very much easier for you  ;)