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

Author Topic: Local/global position problem  (Read 5165 times)

0 Members and 1 Guest are viewing this topic.

Bones

  • Newbie
  • *
  • Posts: 24
    • View Profile
Local/global position problem
« on: June 01, 2011, 06:28:47 am »
I drew a static rectangle in the middle of my screen yet when I use GetPosition().y on it, it returns the local position.  The documentation said to specify the points around the origin to get around this(well that's what I think it means) so I did that.  It still returns the local position so I'm not sure what I"m supposed to do.

Documentation:

Quote
So if you plan to change the position / color of your shape after it is created, you'd better create the points around the origin and with white color, and use only the global position / color (SetPosition, SetColor).

Usage example:

 
Code: [Select]
// Create a shape
 sf::Shape shape;

 // Define its points
 shape.AddPoint(10, 10, sf::Color::White, sf::Color::Red);
 shape.AddPoint(50, 10, sf::Color::White, sf::Color::Green);
 shape.AddPoint(10, 50, sf::Color::White, sf::Color::Blue);

 // Enable outline only
 shape.EnableFill(false);
 shape.EnableOutline(true);
 shape.SetOutlineThickness(10);

 // Display it
 window.Draw(shape); // window is a sf::RenderWindow

 // Display static shapes
 window.Draw(sf::Shape::Line(0, 0, 10, 20, sf::Color::Red));
 window.Draw(sf::Shape::Rectangle(100, 1000, 50, 20, sf::Color::Green));
 window.Draw(sf::Shape::Circle(500, 500, 20, sf::Color::Blue, 5, sf::Color::Black));



Thanks.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Local/global position problem
« Reply #1 on: June 01, 2011, 07:54:30 am »
I'd like to see your code instead of the official doc, especially how you setup your shape and how you call GetPosition().
Laurent Gomila - SFML developer

Bones

  • Newbie
  • *
  • Posts: 24
    • View Profile
Local/global position problem
« Reply #2 on: June 01, 2011, 11:16:30 pm »
Code: [Select]
//Main function
const int screenWidth = 800; //Screen variables for renderwindow
const int screenHeight = 600;

//Make instance of the rendering window
//Input stuff

//This is in the middle of the screen, think pong paddle on the left side.
sf::Shape player1;
player1.AddPoint(0, int(screenHeight/2-50), sf::Color::White, sf::Color::White);
player1.AddPoint(20, int(screenHeight/2-50), sf::Color::White, sf::Color::White);
player1.AddPoint(20, int(screenHeight/2+50), sf::Color::White, sf::Color::White);
player1.AddPoint(0, int(screenHeight/2+50), sf::Color::White, sf::Color::White);
player1.SetColor(sf::Color(255,0,0));



That's basically the beginning of my code where I instance the shape

Code: [Select]
if (player1.GetPosition().y > screenHeight-100)
{
player1.SetPosition(0, screenHeight-100);
}


That's where I'm checking position but if I cout using:

Code: [Select]
std::cout << player1.GetPosition().y << std::endl;

It is offset by half the screen(because that's where I set the points to).

I can post the whole thing if you want but hopefully this should be enough. BTW if it matters, I draw it using:

Code: [Select]
App.Draw(player1);

EDIT: I tried this too but it doesn't seem to be doing anything:

   
Code: [Select]
player1.TransformToGlobal(player1.GetPosition());

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Local/global position problem
« Reply #3 on: June 02, 2011, 10:36:44 am »
A minimal code would be better.

Code: [Select]
#include <SFML/Graphics.hpp>
#include <iostream>

int main()
{
    sf::Shape shape;
    shape.AddPoint(200, 200, sf::Color::White, sf::Color::White);
    shape.AddPoint(200, 300, sf::Color::White, sf::Color::White);
    shape.AddPoint(300, 300, sf::Color::White, sf::Color::White);
    shape.AddPoint(300, 200, sf::Color::White, sf::Color::White);

    shape.SetPosition(100, 100);
    sf::Vector2f position = shape.GetPosition();
    std::cout << position.x << " " << position.y << std::endl;

    return 0;
}

This code should display "100 100". And I'm sure it does ;)
Laurent Gomila - SFML developer

Bones

  • Newbie
  • *
  • Posts: 24
    • View Profile
Local/global position problem
« Reply #4 on: June 02, 2011, 11:40:58 pm »
Quote from: "Laurent"
A minimal code would be better.

Code: [Select]
#include <SFML/Graphics.hpp>
#include <iostream>

int main()
{
    sf::Shape shape;
    shape.AddPoint(200, 200, sf::Color::White, sf::Color::White);
    shape.AddPoint(200, 300, sf::Color::White, sf::Color::White);
    shape.AddPoint(300, 300, sf::Color::White, sf::Color::White);
    shape.AddPoint(300, 200, sf::Color::White, sf::Color::White);

    shape.SetPosition(100, 100);
    sf::Vector2f position = shape.GetPosition();
    std::cout << position.x << " " << position.y << std::endl;

    return 0;
}

This code should display "100 100". And I'm sure it does ;)


Yes it does display "100 100" but when I draw it to the screen, it's over half way down.

Code: [Select]
#include <SFML/Graphics.hpp>
#include <iostream>

int main()
{
sf::RenderWindow App(sf::VideoMode(800, 600, 32), "Position Test");

    sf::Shape shape;
    shape.AddPoint(200, 200, sf::Color::White, sf::Color::White);
    shape.AddPoint(200, 300, sf::Color::White, sf::Color::White);
    shape.AddPoint(300, 300, sf::Color::White, sf::Color::White);
    shape.AddPoint(300, 200, sf::Color::White, sf::Color::White);

    shape.SetPosition(100, 100);

    sf::Vector2f position = shape.GetPosition();
    std::cout << position.x << " " << position.y << std::endl;

while (App.IsOpened())
{
sf::Event Event;

while(App.PollEvent(Event))
{
if (Event.Type == sf::Event::Closed)
App.Close();
}
App.Clear();
App.Draw(shape);
App.Display();
}

    return 0;
}


EDIT: ie it's not giving me global coordinates.  If I change setposition to (0,0) then it will be drawn at where I first set the points but it will still cout 0 0.

Lupinius

  • Jr. Member
  • **
  • Posts: 85
    • View Profile
Local/global position problem
« Reply #5 on: June 02, 2011, 11:51:32 pm »
For me the shape appears at (300, 300), which i do believe is correct because the shape itself starts at (200, 200) and is moved by (100, 100) afterwards.

Bones

  • Newbie
  • *
  • Posts: 24
    • View Profile
Local/global position problem
« Reply #6 on: June 03, 2011, 12:01:17 am »
Quote from: "Lupinius"
For me the shape appears at (300, 300), which i do believe is correct because the shape itself starts at (200, 200) and is moved by (100, 100) afterwards.


But isn't that how the move function works?  SetPosition sets the position but it's setting it in local coords or something because it treats the origin of the shape(which is in the middle of the screen pretty much) as (0,0).  I want the top left of the screen to be (0,0).  Perhaps my call to the transFormToGlobal or whatever it is called was incorrect because I should have called it on each point.  I feel like I"m missing something though because I assumed SetPosition and GetPosition would give me the global coordinates.

EDIT: Wait, I guess that means I should create everything with their origin at 0,0 then set their position to something else.  Does it have to be done like this?

Bones

  • Newbie
  • *
  • Posts: 24
    • View Profile
Local/global position problem
« Reply #7 on: June 03, 2011, 04:08:30 am »
Well, I did finish my pong game now.  It just needs a bit of tweaking to avoid some collision glitches.  This thread is solved I guess unless someone has an answer to my edit question from my last post.

Quote
Wait, I guess that means I should create everything with their origin at 0,0 then set their position to something else. Does it have to be done like this?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Local/global position problem
« Reply #8 on: June 03, 2011, 08:37:21 am »
Quote
Wait, I guess that means I should create everything with their origin at 0,0 then set their position to something else. Does it have to be done like this?

Yes. I thought you already understood this, because this is exactly what you said in your first post ;)
Laurent Gomila - SFML developer

 

anything