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

Author Topic: Get the Screen Location of a VertexArray  (Read 2944 times)

0 Members and 1 Guest are viewing this topic.

Volume

  • Newbie
  • *
  • Posts: 2
    • View Profile
Get the Screen Location of a VertexArray
« on: April 30, 2021, 12:19:50 pm »
Hi!
I'm currently working on a Asteroids Project and got stuck with my Asteroid class which is created with a VertexArray.

The Problem that I'm facing is that I don't know how to get the current Screen Location of the Asteroid class. The Class is moving across the Screen so I need the position to keep track when the Asteroid is about to leave the visible area (Screen borders). It should appear at the opposite side of the Screen again.

(Btw: When I'm talking about the Screen I mean the window which I have created via sf::RenderWindow. I still figuring out the correct terminology so I apologize for being misleading.)


My PlayerShip class works like this when it's about to leave the Screen:

if (PlayerShip.getPosition().x > Screen.x) {
                                        PlayerShip.setPositionX(0);
                                }
                                else if (PlayerShip.getPosition().x < 0) {
                                        PlayerShip.setPositionX(Screen.x);
                                }
                                else if (PlayerShip.getPosition().y > Screen.y) {
                                        PlayerShip.setPositionY(0);
                                }
                                else if (PlayerShip.getPosition().y < 0) {
                                        PlayerShip.setPositionY(Screen.y);
                                }



I don't understand how I can “get” the Position of my VertexArray to create the same logic.

After a long time of research I came across advices to not draw directly on the sf::RenderWindow class. So I created a sf::View class but experimenting with it didn't solve my Problem.
Also I tried functions like mapPixelToCoords or mapCoordsToPixel but they didn't help me.

The only way of how I keep track of the Asteroid class position is when initializing the class with two arguments:

Asteroid::Asteroid(float startX, float startY)
{

        StartPosition.x = startX;
        StartPosition.y = startY;

        sf::Transform TransformAsteroid.translate(StartPosition);
}

...but this doesn't help me either. The StartPosition is of course always the StartPosition.


I have the feeling that I get something fundamentally wrong about the coordinate system and how I can work with a sf::Transform class and it's translate() function the same way that I can with a sf::Shape class (which my PlayerShip class is derived from).

fallahn

  • Sr. Member
  • ****
  • Posts: 492
  • Buns.
    • View Profile
    • Trederia
Re: Get the Screen Location of a VertexArray
« Reply #1 on: April 30, 2021, 06:23:28 pm »
Rather than use sf::Transform directly you might want to try inheriting sf::Transformable, which is what sf::Shape and other drawables do. It provides the getPosition() etc functions:

https://www.sfml-dev.org/documentation/2.5.1/classsf_1_1Transformable.php

Volume

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Get the Screen Location of a VertexArray
« Reply #2 on: April 30, 2021, 10:59:00 pm »
Rather than use sf::Transform directly you might want to try inheriting sf::Transformable

I've setup my Asteroid class now like this:

class Asteroid : public sf::Transformable
{...

Now I can call every function that derives from sf::Transformable but when I call getPosition() I only get the value "0" (for both axis).





Edit: Also I (think I) have to return sf::Transform of my VertexArray in the window.draw() function:

window.draw(Asteroid.getShape(), Asteroid.getTransform());

...because otherwise the class is always located at Screen position 0,0 (upper left corner).

Besides I should also mention that this class is about to have a collision (a topic I haven't touched yet) and maybe should rotate too. I guess this is important to know as it'll probably have an impact on how the class is created.
« Last Edit: May 01, 2021, 10:05:13 am by Volume »

 

anything