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

Author Topic: Drawable.GetPosition()  (Read 5652 times)

0 Members and 1 Guest are viewing this topic.

e_barroga

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Drawable.GetPosition()
« on: April 16, 2009, 11:16:10 pm »
I've created a shape as follows:
Code: [Select]

sf::Shape Ball = sf::Shape::Circle( 400, 300, BallRadius, sf::Color( 255, 255, 255 ) ); //Ball


When I check the Y coordinate (writing to console) I get: -7

Shouldn't it be 300?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Drawable.GetPosition()
« Reply #1 on: April 17, 2009, 12:26:00 am »
It should be zero. The fact that you put the points of your shape around (300, 400) doesn't move its origin from (0, 0). It's just not a good idea because you'll always have this offset.
Laurent Gomila - SFML developer

e_barroga

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Drawable.GetPosition()
« Reply #2 on: April 17, 2009, 12:53:09 am »
Quote from: "Laurent"
It should be zero. The fact that you put the points of your shape around (300, 400) doesn't move its origin from (0, 0). It's just not a good idea because you'll always have this offset.


Oh, okay. I think I get it. This makes perfect sense, thank you. The actual reason it is -7 is because I move the shape -7.

Thanks for the explanation.

e_barroga

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Drawable.GetPosition()
« Reply #3 on: April 17, 2009, 01:08:41 am »
Now, I'm getting the following error after trying to write the shape as a class:
Code: [Select]

C:\Users\BarrogaTravels\Desktop\Pong\Engine.h|6|error: `sf::Shape::Rectangle' cannot appear in a constant-expression|
C:\Users\BarrogaTravels\Desktop\Pong\Engine.h|6|error: a call to a constructor cannot appear in a constant-expression|
C:\Users\BarrogaTravels\Desktop\Pong\Engine.h|6|error: a function call cannot appear in a constant-expression|
C:\Users\BarrogaTravels\Desktop\Pong\Engine.h|6|error: invalid in-class initialization of static data member of non-integral type `const sf::Shape'|
C:\Users\BarrogaTravels\Desktop\Pong\Engine.h||In constructor `Paddle::Paddle(float, float)':|
C:\Users\BarrogaTravels\Desktop\Pong\Engine.h|11|error: `self' was not declared in this scope|
C:\Users\BarrogaTravels\Desktop\Pong\Engine.h|11|warning: unused variable 'self'|
C:\Users\BarrogaTravels\Desktop\Pong\Engine.h||In member function `void Paddle::Move(float, float)':|
C:\Users\BarrogaTravels\Desktop\Pong\Engine.h|15|error: `self' was not declared in this scope|
C:\Users\BarrogaTravels\Desktop\Pong\Engine.h|15|warning: unused variable 'self'|
C:\Users\BarrogaTravels\Desktop\Pong\Engine.h||In member function `void Paddle::SetX(float)':|
C:\Users\BarrogaTravels\Desktop\Pong\Engine.h|19|error: `self' was not declared in this scope|
C:\Users\BarrogaTravels\Desktop\Pong\Engine.h|19|warning: unused variable 'self'|
C:\Users\BarrogaTravels\Desktop\Pong\Engine.h||In member function `void Paddle::SetY(float)':|
C:\Users\BarrogaTravels\Desktop\Pong\Engine.h|23|error: `self' was not declared in this scope|
C:\Users\BarrogaTravels\Desktop\Pong\Engine.h|23|warning: unused variable 'self'|
C:\Users\BarrogaTravels\Desktop\Pong\main.cpp||In function `int main()':|
C:\Users\BarrogaTravels\Desktop\Pong\main.cpp|45|error: 'class Paddle' has no member named 'GetPosition'|
C:\Users\BarrogaTravels\Desktop\Pong\main.cpp|46|error: 'class Paddle' has no member named 'GetPosition'|
C:\Users\BarrogaTravels\Desktop\Pong\main.cpp|48|error: 'class Paddle' has no member named 'GetPosition'|
C:\Users\BarrogaTravels\Desktop\Pong\main.cpp|49|error: 'class Paddle' has no member named 'GetPosition'|
C:\Users\BarrogaTravels\Desktop\Pong\main.cpp|66|error: expected `)' before "App"|
C:\Users\BarrogaTravels\Desktop\Pong\main.cpp|67|error: no matching function for call to `sf::RenderWindow::Draw(Paddle&)'|
C:\Program Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\SFML\Graphics\RenderTarget.hpp|67|note: candidates are: virtual void sf::RenderTarget::Draw(const sf::Drawable&)|
C:\Users\BarrogaTravels\Desktop\Pong\main.cpp|68|error: no matching function for call to `sf::RenderWindow::Draw(Paddle&)'|
C:\Program Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\SFML\Graphics\RenderTarget.hpp|67|note: candidates are: virtual void sf::RenderTarget::Draw(const sf::Drawable&)|
C:\Users\BarrogaTravels\Desktop\Pong\main.cpp|52|warning: unused variable 'Factor'|
||=== Build finished: 15 errors, 5 warnings ===|


Disregard the "GetPosition" error.

Code: [Select]

class Paddle {
    private:
        sf::Shape self = sf::Shape::Rectangle( 0, 10, 100, sf::Color( 255, 255, 255 ) );

    public:
        Paddle();
        Paddle( float X, float Y ) {
            self.Move( X, Y );
        }

        void Move( float X, float Y ) {
            self.Move( X, Y );
        }

        void SetX( float X ) {
            self.SetX( X );
        }

        void SetY( float Y ) {
            self.SetY( Y );
        }
};

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Drawable.GetPosition()
« Reply #4 on: April 17, 2009, 01:52:20 am »
You should probably have a look at a good C++ book or at least a tutorial. ;)

You cannot initialize class members inside the class definition. That's the constructor's task.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

e_barroga

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Drawable.GetPosition()
« Reply #5 on: April 17, 2009, 05:37:39 am »
I've moved the initialization to the constructor, but now I'm not quite sure how to write this:
Code: [Select]

float GetX() {
    return ( self.GetPosition().x );
}


...considering it isn't returning a float type. I checked the source and it seems I should be using const Vector2f but it doesn't work.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Drawable.GetPosition()
« Reply #6 on: April 17, 2009, 02:49:15 pm »
What doesn't work? Could you post the surrounding and relevant code, including the used types and invoked functions?

The error might be helpful, too.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

e_barroga

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Drawable.GetPosition()
« Reply #7 on: April 17, 2009, 04:05:55 pm »
I'm sorry.... I rechecked the code more indepth and I wrote a class member when it should have been a function member:

Code: [Select]

Paddle1.GetY


//Fixed
Code: [Select]

Paddle1.GetY()


Code: [Select]

class Paddle {
    private:
        sf::Shape self;

    public:
        Paddle() {
            self = sf::Shape::Rectangle( 0.f, 0.f, 10.f, 100.f, sf::Color( 255, 255, 255 ) );
        }
        Paddle( float X, float Y ) {
            self.Move( X, Y );
        }

        void Move( float X, float Y ) {
            self.Move( X, Y );
        }

        void SetX( float X ) {
            self.SetX( X );
        }

        void SetY( float Y ) {
            self.SetY( Y );
        }

        float GetX() {
            return ( self.GetPosition().x );
        }

        float GetY() {
            return ( self.GetPosition().y );
        }

        sf::Shape Draw() {
            return ( self );
        }
};


float lengthdir_x( float Length, float Direction ) {
    return ( std::cos( -Direction*( 3.14159265/180 ) ) * Length );
}

float lengthdir_y( float Length, float Direction ) {
    return ( std::sin( -Direction*( 3.14159265/180 ) ) * Length );
}

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Drawable.GetPosition()
« Reply #8 on: April 17, 2009, 05:59:43 pm »
Okay... Now everything works?

By the way, I have some advice for you. First, you should use the constructor's initializer list:
Code: [Select]
Paddle() : self(sf::Shape::Rectangle( 0.f, 0.f, 10.f, 100.f, sf::Color( 255, 255, 255 ) )
{
}

Second, there is mostly no reason not to declare getter functions const:
Code: [Select]
float GetX() const
{
    return self.GetPosition().x;
}
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Drawable.GetPosition()
« Reply #9 on: April 18, 2009, 11:31:20 am »
Quote from: "Nexus"

Second, there is mostly no reason not to declare getter functions const:
Code: [Select]
float GetX() const
{
    return self.GetPosition().x;
}


Yes there is. If the reference to the object is constant then only the constant function works. The const keyword promises that the function does not change the internal state of the object.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Drawable.GetPosition()
« Reply #10 on: April 18, 2009, 12:30:38 pm »
Quote from: "Groogy"
Yes there is. If the reference to the object is constant then only the constant function works. The const keyword promises that the function does not change the internal state of the object.
I wrote "no reason not to declare getter functions const". Sorry if that was misleading... ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Drawable.GetPosition()
« Reply #11 on: April 19, 2009, 12:47:58 pm »
Damn it! I thought I looked smart <_<
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

 

anything