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

Author Topic: Getting absolute coordinates  (Read 9877 times)

0 Members and 4 Guests are viewing this topic.

Williamson

  • Newbie
  • *
  • Posts: 16
    • View Profile
Getting absolute coordinates
« on: December 03, 2008, 07:09:11 pm »
Hi everyone!
Thanks to Wizzard, I managed to make a fair amount of progress, but I have hit another snag: I need to get the absolute coordinates of a point when it is clicked (so, not relative to the sf::View, but to the world itself). I tried
Code: [Select]
sf::Vector2f MousePosAbsolute;
                            MousePosAbsolute.x = displayWindow.GetInput().GetMouseX();
                            MousePosAbsolute.y = displayWindow.GetInput().GetMouseY();

But it didnt seem to work.
Have I done something wrong? Or am I barking up the wrong tree?
Thanks!
Williamson

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Getting absolute coordinates
« Reply #1 on: December 03, 2008, 09:04:16 pm »
This gets the mouse position in window coordinates.

In SVN there's a RenderWindow::ConvertCoords function that does exactly what you want.
Laurent Gomila - SFML developer

Williamson

  • Newbie
  • *
  • Posts: 16
    • View Profile
Getting absolute coordinates
« Reply #2 on: December 03, 2008, 09:10:08 pm »
Thanks!
So there isnt one in the current, stable 1.3 version of SFML?
Williamson

Wizzard

  • Full Member
  • ***
  • Posts: 213
    • View Profile
Getting absolute coordinates
« Reply #3 on: December 03, 2008, 09:42:34 pm »
It is in SFML version 1.3 too.
Code: [Select]
sf::Vector2f MousePosAbsolute = displayWindow.ConvertCoords(displayWindow.GetInput().GetMouseX(), displayWindow.GetInput().GetMouseY());

Williamson

  • Newbie
  • *
  • Posts: 16
    • View Profile
Getting absolute coordinates
« Reply #4 on: December 03, 2008, 10:03:46 pm »
I thought that THAT was used to get the position relative to the view.
Williamson

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Getting absolute coordinates
« Reply #5 on: December 03, 2008, 10:19:23 pm »
Quote
It is in SFML version 1.3 too.

Oops :lol:
Laurent Gomila - SFML developer

Wizzard

  • Full Member
  • ***
  • Posts: 213
    • View Profile
Getting absolute coordinates
« Reply #6 on: December 03, 2008, 11:46:58 pm »
Quote from: "Williamson"
I thought that THAT was used to get the position relative to the view.

In the example code I gave you, I was using two completely separate views, one to draw GUI objects and one to draw world objects.
Code: [Select]
// Returns the view that RenderWindow creates automatically
displayWindow.GetDefaultView();

RenderWindow creates a view automatically, that, by default, is perfect to draw GUI objects.
Code: [Select]
// Creates a view that fits the whole window
sf::View worldView = sf::View(sf::Vector2f(playerX, playerY), );

The world view, which describes the world from the player's perspective, needs to be created in order to draw world objects.
Code: [Select]
// Sets the coordinate system and Draws and Renders all Drawables
displayWindow.SetView(worldView);            // Use world coordinate system
displayWindow.Draw(worldMap);                // Draw the Map
displayWindow.SetView(App.GetDefaultView()); // Use window coordinate system
displayWindow.Draw(guiPlayer);               // Draw the Player

This brings us back to the example code I showed you before.
Code: [Select]
sf::Vector2f MousePosAbsolute = displayWindow.ConvertCoords(displayWindow.GetInput().GetMouseX(), displayWindow.GetInput().GetMouseY(), worldView);

To get the position of the mouse from the world's view, simply use the above code.


It's important to realize that views are just cameras for your application, they change the meaning of objects' positions based on how they're viewing it.

Imbue

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Getting absolute coordinates
« Reply #7 on: December 04, 2008, 01:14:19 am »
Quote from: "Wizzard"
It is in SFML version 1.3 too.
I really think that it was buggy in version 1.3.

The SVN version seems quite stable though.

Williamson

  • Newbie
  • *
  • Posts: 16
    • View Profile
Getting absolute coordinates
« Reply #8 on: December 05, 2008, 09:31:56 pm »
Its really weird: no matter which I try, I get the same result: the coordinates that I clicked from the views point of view, rather than those of the world.
I am SURE that shouldnt be happening  :shock:
Should I try the SVN version?
Williamson

Wizzard

  • Full Member
  • ***
  • Posts: 213
    • View Profile
Getting absolute coordinates
« Reply #9 on: December 06, 2008, 11:01:03 am »
Yes, you should try to use the SVN version.

Williamson

  • Newbie
  • *
  • Posts: 16
    • View Profile
Getting absolute coordinates
« Reply #10 on: December 06, 2008, 05:40:59 pm »
In any case, I'm sure that there is a bug. With this code:
Code: [Select]
sf::Vector2f MousePosAbsolute = displayWindow.ConvertCoords(displayWindow.GetInput().GetMouseX(), displayWindow.GetInput().GetMouseY());
                            sf::Vector2f MousePosB;
                            MousePosB.x = displayWindow.GetInput().GetMouseX();
                            MousePosB.y = displayWindow.GetInput().GetMouseY();
                            std::cout << "Type 1:" << ((int)MousePosB.x - (int)MousePosB.x%64)/64 << ":" << ((int)MousePosB.y - (int)MousePosB.y%64)/64 << std::endl;
                            std::cout << "Type 2:" << ((int)MousePosAbsolute.x - (int)MousePosAbsolute.x%64)/64 << ":" << ((int)MousePosAbsolute.y - (int)MousePosAbsolute.y%64)/64 << std::endl;


I get this (and I do move the view around):

Type 1:0:0
Type 2:0:0
Type 1:1:0
Type 2:1:0
Type 1:2:0
Type 2:2:0
Type 1:3:0
Type 2:3:0
Type 1:4:0
Type 2:4:0
Type 1:0:0
Type 2:0:0
Type 1:1:0
Type 2:1:0
Type 1:2:0
Type 2:2:0
Type 1:3:0
Type 2:3:0
Type 1:4:0
Type 2:4:0

I'm gonna try installing the SVN version now.

Williamson

Williamson

  • Newbie
  • *
  • Posts: 16
    • View Profile
Getting absolute coordinates
« Reply #11 on: December 06, 2008, 08:01:51 pm »
Well, as far as I could tell, I only had to copy the new include file to the correct directory. I recompiled, and.... :

InterfaceGraphique.cpp:(.text+0x18b5): undefined reference to `sf::Unicode::Text::Text(char const*)'
InterfaceGraphique.cpp:(.text+0x18d3): undefined reference to `sf::String::String(sf::Unicode::Text const&, sf::Font const&, float)'
InterfaceGraphique.cpp:(.text+0x1901): undefined reference to `sf::Unicode::Text::Text(char const*)'
InterfaceGraphique.cpp:(.text+0x191f): undefined reference to `sf::String::String(sf::Unicode::Text const&, sf::Font const&, float)'
InterfaceGraphique.cpp:(.text+0x1b4b): undefined reference to `sf::Unicode::Text::Text(char const*)'
InterfaceGraphique.cpp:(.text+0x1b5d): undefined reference to `sf::String::SetText(sf::Unicode::Text const&)'
InterfaceGraphique.cpp:(.text+0x1bde): undefined reference to `sf::Unicode::Text::Text(char const*)'
InterfaceGraphique.cpp:(.text+0x1bf0): undefined reference to `sf::String::SetText(sf::Unicode::Text const&)'
InterfaceGraphique.cpp:(.text+0x1cde): undefined reference to `sf::RenderTarget::Draw(sf::Drawable const&)'
InterfaceGraphique.cpp:(.text+0x1cf0): undefined reference to `sf::RenderTarget::Draw(sf::Drawable const&)'
InterfaceGraphique.cpp:(.text+0x1d02): undefined reference to `sf::RenderTarget::Draw(sf::Drawable const&)'
InterfaceGraphique.cpp:(.text+0x1d14): undefined reference to `sf::RenderTarget::Draw(sf::Drawable const&)'
InterfaceGraphique.cpp:(.text+0x1f25): undefined reference to `sf::Unicode::Text::Text(char const*)'
InterfaceGraphique.cpp:(.text+0x1f37): undefined reference to `sf::String::SetText(sf::Unicode::Text const&)'
InterfaceGraphique.cpp:(.text+0x1fb4): undefined reference to `sf::Unicode::Text::Text(char const*)'
InterfaceGraphique.cpp:(.text+0x1fc6): undefined reference to `sf::String::SetText(sf::Unicode::Text const&)'
InterfaceGraphique.o: In function `InterfaceGraphique::InterfaceGraphique()':
InterfaceGraphique.cpp:(.text+0x3897): undefined reference to `sf::Font::ourDefaultCharset'
InterfaceGraphique.cpp:(.text+0x389f): undefined reference to `sf::Unicode::Text::Text(unsigned int const*)'
InterfaceGraphique.cpp:(.text+0x38da): undefined reference to `sf::Font::LoadFromFile(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned int, sf::Unicode::Text const&)'
InterfaceGraphique.o: In function `InterfaceGraphique::InterfaceGraphique()':
InterfaceGraphique.cpp:(.text+0x5cef): undefined reference to `sf::Font::ourDefaultCharset'
InterfaceGraphique.cpp:(.text+0x5cf7): undefined reference to `sf::Unicode::Text::Text(unsigned int const*)'
InterfaceGraphique.cpp:(.text+0x5d32): undefined reference to `sf::Font::LoadFromFile(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned int, sf::Unicode::Text const&)'
collect2: ld returned 1 exit status



 :shock:  ARGH!
Did I forget a step, cos this code worked fine before.
Williamson

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Getting absolute coordinates
« Reply #12 on: December 06, 2008, 09:56:30 pm »
You have to recompile SFML, you can't just use the new headers with the old binaries :roll:
Laurent Gomila - SFML developer

Williamson

  • Newbie
  • *
  • Posts: 16
    • View Profile
Getting absolute coordinates
« Reply #13 on: December 06, 2008, 10:09:31 pm »
Thanks!
Ok.
Errrrrr
This may sound like a stupid question, but...
How?  :oops:
Williamson

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Getting absolute coordinates
« Reply #14 on: December 06, 2008, 10:11:21 pm »
Read the tutorial: "Getting started -- SFML and [your compiler]" :)
Laurent Gomila - SFML developer

 

anything