SFML community forums
Help => Graphics => Topic started by: Bones 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:
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:
// 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.
-
I'd like to see your code instead of the official doc, especially how you setup your shape and how you call GetPosition().
-
//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
if (player1.GetPosition().y > screenHeight-100)
{
player1.SetPosition(0, screenHeight-100);
}
That's where I'm checking position but if I cout using:
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:
App.Draw(player1);
EDIT: I tried this too but it doesn't seem to be doing anything:
player1.TransformToGlobal(player1.GetPosition());
-
A minimal code would be better.
#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 ;)
-
A minimal code would be better.
#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.
#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.
-
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.
-
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?
-
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.
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?
-
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 ;)