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

Author Topic: Convert coords, how?  (Read 3148 times)

0 Members and 2 Guests are viewing this topic.

grimmreefer

  • Jr. Member
  • **
  • Posts: 91
    • View Profile
    • Email
Convert coords, how?
« on: March 31, 2013, 01:01:58 am »
How can i convert the coordinates of an object from an rotated view to the standard view? I make some experiments, at this moment i try to create a isometric look.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Convert coords, how?
« Reply #1 on: March 31, 2013, 09:38:17 am »
Which version of SFML?
Laurent Gomila - SFML developer

grimmreefer

  • Jr. Member
  • **
  • Posts: 91
    • View Profile
    • Email
Re: Convert coords, how?
« Reply #2 on: March 31, 2013, 10:24:49 am »
2.

I also uses mapPixleToCoords, but in this case I can not solve my problem whit this function.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Convert coords, how?
« Reply #3 on: March 31, 2013, 10:30:33 am »
You rather need mapCoordsToPixel.
Laurent Gomila - SFML developer

grimmreefer

  • Jr. Member
  • **
  • Posts: 91
    • View Profile
    • Email
Re: Convert coords, how?
« Reply #4 on: March 31, 2013, 10:49:36 am »
 SWall.setPosition(static_cast<sf::Vector2f>(window.mapCoordsToPixel(Fields[i].getPosition())));
 

@Laurent
Thanks for the hint but i also try this. Maybe i'am using it wrong? 

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Convert coords, how?
« Reply #5 on: March 31, 2013, 11:34:07 am »
Make sure that the rotated view is active when you call the function. If it's not, then pass it explicitely as the second argument of the function.
Laurent Gomila - SFML developer

grimmreefer

  • Jr. Member
  • **
  • Posts: 91
    • View Profile
    • Email
Re: Convert coords, how?
« Reply #6 on: March 31, 2013, 11:52:23 am »
The rotated view is active at this moment and it seams that my sprite will go to the correct position but with a offset.

#include <iostream>
#include <SFML/Graphics.hpp>



using namespace std;

sf::RenderWindow window;
sf::RenderTexture _RenderTexture;
sf::RenderTexture   SecRender;

sf::RectangleShape _BACKG;

sf::Event event;
sf::Mouse _Mouse;
sf::Sprite _Image;
sf::Sprite SecImage;
sf::View View;
sf::View StdView;

sf::Texture Wall;
sf::Sprite SWall;

void CreatField();
void DrawField(sf::RenderTexture &render, sf::RenderWindow &window);
void Input();

vector<sf::RectangleShape> Fields;

int main()
{
    Wall.loadFromFile("Images/muster.png");
    SWall.setTexture(Wall);
    //SWall.setRotation(45);

    _BACKG.setFillColor(sf::Color(0,0,0,0));
    _BACKG.setSize(sf::Vector2f(1280,800));
    View.setSize(1280,800);
    View.setRotation(45);


    window.create(sf::VideoMode(1280, 800 ,32), "Mengo", sf::Style::Default);
        window.setKeyRepeatEnabled(true);
        window.setFramerateLimit(30);




        _RenderTexture.create(1280,800, true);
        _RenderTexture.setSmooth(true);

        SecRender.create(1280,800,true);
        SecRender.setSmooth(true);

    _Image.setTexture(_RenderTexture.getTexture());
    SecImage.setTexture(SecRender.getTexture());

    CreatField();

        while(window.isOpen())
        {
        while(window.pollEvent(event))
                {
                        switch(event.type)
                        {
                        case sf::Event::Closed:
                                window.close();

                        default: break;
            }
                }



    if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
        window.close();

    window.clear(sf::Color(sf::Color(0,0,0,0)));
    _RenderTexture.clear(sf::Color(0,0,0,0));
    _RenderTexture.display();
    SecRender.clear(sf::Color(0,0,0,0));
    SecRender.display();

    window.setView(StdView);///Standard View
    window.draw(_BACKG);

    window.setView(View);///Rotated View
    DrawField(_RenderTexture, window);

    SecRender.draw(SWall);

    Input();
    window.draw(_Image);
    window.setView(StdView);///Standard View
    window.draw(SecImage);



    window.display();
        }
    return 0;
}



void Input()
{
///Moving the view
}



void DrawField(sf::RenderTexture &render, sf::RenderWindow &window)
{
    for(unsigned int i = 0; i < Fields.size(); i++)
    {
        render.draw(Fields[i]);

        if(window.mapPixelToCoords(sf::Mouse::getPosition(window)).x > Fields[i].getPosition().x && window.mapPixelToCoords(sf::Mouse::getPosition(window)).x < Fields[i].getPosition().x + 80.f &&
           window.mapPixelToCoords(sf::Mouse::getPosition(window)).y > Fields[i].getPosition().y && window.mapPixelToCoords(sf::Mouse::getPosition(window)).y < Fields[i].getPosition().y + 80.f)
        {
            Fields[i].setFillColor(sf::Color::Cyan);

///HERE I SET THE POSITION FOR THE STANDARD VIEW

            SWall.setPosition(static_cast<sf::Vector2f>(window.mapCoordsToPixel(Fields[i].getPosition())));
        }
        else
        Fields[i].setFillColor(sf::Color::White);
    }


}


void CreatField()
{
    ///Create the field
}
 

Maybe you can see the error?

Please remember, its just a experiment, so please don't rate the code.
« Last Edit: March 31, 2013, 03:26:22 pm by grimmreefer »

 

anything