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

Author Topic: Shape not placing properly  (Read 2739 times)

0 Members and 1 Guest are viewing this topic.

J-Gamer

  • Newbie
  • *
  • Posts: 6
    • View Profile
Shape not placing properly
« on: November 25, 2012, 08:44:18 pm »
I don't really know how to phrase it otherwise. This happens with both rectangle shapes and circular shapes(haven't tested other shapes).


This is a grid in my game, with the player and zombie running across it. The big circle is an obstacle, and should be drawn right in the middle of the screen.
Here's the piece of code that draws it:
sf::Vector2f pos = circ->getCenter();
        sf::CircleShape c;
        c.setRadius(circ->getRadius());
        c.move(-c.getRadius(),-c.getRadius());
        data->win.draw(c);
circ is an object that represents a circle, with a center and a radius. data->win is a RenderWindow.
I checked in the debugger, nothing wrong with the pos variable, just 400,300. As it should be, because I put the circle right in the middle of the screen, which is 800x600. I tested the value I'm sending setRadius, and it is 200, as to be expected.
But it still is drawn incorrectly, slightly to the left and a tad bit too small. The same happens with my rectangular objects. Anyone got any ideas why this happens? I'm really out of ideas now.

Edit:
If I leave out the c.move statement, it's position is already slightly to the left from where you would excpect it to be(while the position I put it in still is 400,300, so nice in the middle of the screen).
« Last Edit: November 25, 2012, 08:57:37 pm by J-Gamer »

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: Shape not placing properly
« Reply #1 on: November 25, 2012, 09:03:44 pm »
The code doesn't really say much, nor is it understood well what's wrong with the positioning, a bit more of code should help.

And in any case if all you want is to place the circle shape to a point just use setPosition with 400 as x, 300 as y. Unless you want something else it's unbelievably simple.
Programmer, Artist, Composer and Storyline/Script Writer of "Origin of Magic". If all goes well this could turn into a commercial project!

Finally back into the programming world!

J-Gamer

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Shape not placing properly
« Reply #2 on: November 25, 2012, 09:15:47 pm »
setPosition sets the top left corner, not the center.

It seems I need to dig deeper in my own code, since I made a simple program trying to demonstrate the problem that did work perfectly(ie: just drawing a circle with those coordinates, and some lines surrounding it to check correctness).

Edit: It seems like it's my math I use for calculating the coordinates is a bit off, since the circle is actually being drawn correctly. The points on my grid, however...
« Last Edit: November 25, 2012, 09:28:56 pm by J-Gamer »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
AW: Re: Shape not placing properly
« Reply #3 on: November 26, 2012, 11:12:12 am »
setPosition sets the top left corner, not the center.
You can use setOrigin to change how things get drawn.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

J-Gamer

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Shape not placing properly
« Reply #4 on: November 26, 2012, 03:32:05 pm »
Found my error: it was completely unrelated to the drawing code. I'm kinda face-palming for myself right now.  :-[

ichineko

  • Newbie
  • *
  • Posts: 44
    • View Profile
Re: Shape not placing properly
« Reply #5 on: November 28, 2012, 01:32:11 am »
It sounds like you've solved your problem, but just an fyi for my benefit.

I believe it would be better for you to set the origin directly in the center of it (width/2,height/2), otherwise it draws from the top left corner, which is just weird.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
AW: Re: Shape not placing properly
« Reply #6 on: November 28, 2012, 10:13:42 am »
I believe it would be better for you to set the origin directly in the center of it (width/2,height/2), otherwise it draws from the top left corner, which is just weird.
Well generally speaking it really depends on how you handle things. If you're for instance in a grid, it's way easier to have the origin of all object redering to the top left (or at least the same relative position), rather than having to branch and manually calculate the actual position.

In this case though it might be easier to change the origin and place it in the middle of the screen. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/