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

Author Topic: Cannot move shapes  (Read 3873 times)

0 Members and 1 Guest are viewing this topic.

kintantee

  • Newbie
  • *
  • Posts: 10
    • View Profile
Cannot move shapes
« on: December 28, 2011, 01:37:42 pm »
Hello,
I'm trying to make an Pong clone however I have some problems with constructing and moving static shapes like circle and rectangle.

Let's start with circle,

First of all I have an primitive class called Circle.

Circle class:

Code: [Select]

class Circle
{
public:
    Circle(const sf::Vector2f &position,
      const float         &radius,
           const sf::Color    &fillerColor,
           const float        &outlineThickness,
           const sf::Color    &outlineColor);

sf::Vector2f    getPosition() const;
float           getRadius() const;
const sf::Shape &getDrawableObject() const;

void draw(sf::RenderTarget &renderTarget);
void bounce(const sf::Vector2f &displacement);

private:
    sf::Vector2f position_;
    sf::Shape    shape_;
    float        radius_;
    sf::Color    fillerColor_;
    float        outlineThickness_;
    sf::Color    outlineColor_;
};



I'm constructing my Circle object like fallowing:

Code: [Select]

Circle::Circle(const sf::Vector2f &position,
               const float        &radius,
               const sf::Color    &fillerColor,
               const float        &outlineThickness,
               const sf::Color    &outlineColor)
{
    position_         = position;
    radius_           = radius;
    fillerColor_   = fillerColor;
    outlineThickness_ = outlineThickness;
    outlineColor_     = outlineColor;


    shape_ = sf::Shape::Circle(position_,
                               radius_,  
                               fillerColor_,  
                               outlineThickness_,
                               outlineColor_);
}


and here is my move function:

Code: [Select]

void Circle::bounce(const sf::Vector2f &displacement)
{
position_ += displacement;
shape_.SetPosition(position_);
}


now my question is when I use

Code: [Select]

shape_.SetCenter(position_);


instead of current one it doesn't work. Do you know why?

Also fallowing part fallowing part from constructor is not working as well

Code: [Select]

shape_ = sf::Shape::Circle(position_,
  radius_,
  fillerColor_,
  outlineThickness_,
  outlineColor_);


however if I change position_ with fallowing parameter

Code: [Select]

sf::Vector2f(radius_ / 2, radius_ / 2)

then it's working. I dunno why it's not working, on console the numbers seem right. I mean it's just wierd. By working, I mean it's drawing beucase with position_
parameter it's not showing on screen(or it's on some coordinate out of screen)

now my problem with Rectangle is something smilar

here is my Rectangle class:

Code: [Select]

class Rectangle
{
public:
    Rectangle(const sf::Vector2f &position,
              const float        &width,
              const float        &height,
              const sf::Color    &fillerColor,
              const float        &outlineThickness,
              const sf::Color    &outlineColor);

    sf::Vector2f    getPosition() const;
    float           getWidth() const;
    float           getHeight() const;
    const sf::Shape &getDrawableObject() const;

    void draw(sf::RenderTarget &renderTarget);
    void moveUp(const float &displacement);
    void moveDown(const float &displacement);

private:
    sf::Vector2f position_;
    float        width_;
    float        height_;
    sf::Color    fillerColor_;
    float        outlineThickness_;
    sf::Color    outlineColor_;
    sf::Shape    shape_;
};


I'm constructing my Rectangle object like fallowing:

Code: [Select]

Rectangle::Rectangle(const sf::Vector2f &position,
                     const float        &width,
                     const float        &height,
                     const sf::Color    &fillerColor,
                     const float        &outlineThickness,
                     const sf::Color    &outlineColor)
{
    position_         = position;
    width_            = width;
    height_           = height;
    fillerColor_      = fillerColor;
    outlineThickness_ = outlineThickness;
    outlineColor_     = outlineColor;

    sf::Vector2f lowerRightPoint;
    lowerRightPoint.x = position_.x + width;
    lowerRightPoint.y = position_.y + height;

    shape_ = sf::Shape::Rectangle(position_,
                                  lowerRightPoint,
                                  fillerColor_,
                                  outlineThickness_,
                                  outlineColor_);

}


My Rectangle objectets does not even drawn(or drawn but somewhere else, not in screen) I think there is something wrong with fallowing lines

Code: [Select]

    shape_ = sf::Shape::Rectangle(position_,
                                  lowerRightPoint,
                                  fillerColor_,
                                  outlineThickness_,
                                  outlineColor_);


I think it's position_ again but this time I couldn't find a way to show it on screen

is there any better way to create a static shape?

finally I'm trying to move my rectangle objects like fallowing:

Code: [Select]

void Rectangle::moveUp(const float &displacement)
{
    position_.y -= displacement;
    shape_.s
}

void Rectangle::moveDown(const float &displacement)
{
    position_.y += displacement;
    shape_.SetPosition(position_);
}


even I can't move it right now I can see a problem incoming which in above lines I only move the upper left point of rectangle(I assume).
does SetPosition take care of lower right point as well or do I need to add something else.

I hope I made my points clear this time.

Thank you in advance.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Cannot move shapes
« Reply #1 on: December 28, 2011, 02:16:58 pm »
Quote from: "kintantee"
I will upload full soruce code as soon as possible.
Very bad idea, already the posted source code is far too big. You can't expect us to skim through it and to search for mistakes which you haven't even described. "I have some problems at fundamental parts such as moving an object" is no meaningful problem description.

I suggest to read this thread carefully, and then to post again, considering the points mentioned in the link.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

kintantee

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Cannot move shapes
« Reply #2 on: December 28, 2011, 02:54:46 pm »
Quote from: "Nexus"
Quote from: "kintantee"
I will upload full soruce code as soon as possible.
Very bad idea, already the posted source code is far too big. You can't expect us to skim through it and to search for mistakes which you haven't even described. "I have some problems at fundamental parts such as moving an object" is no meaningful problem description.

I suggest to read this thread carefully, and then to post again, considering the points mentioned in the link.


thank you for tip. I was in kinda rush so I couldn't properly describe the problem. I will edit the topic as soon as possible.

kintantee

  • Newbie
  • *
  • Posts: 10
    • View Profile
Cannot move shapes
« Reply #3 on: December 31, 2011, 02:03:09 am »
I edited my post, made my points as clear as possible without showing bunch of codes. I really need help on this, I stuck on same point for 1,5 weeks. Thank you!

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Cannot move shapes
« Reply #4 on: December 31, 2011, 10:55:52 am »
Quote from: "kintantee"
now my question is when I use
Code: [Select]
shape_.SetCenter(position_);
instead of current one it doesn't work. Do you know why?
You're setting the local center of translation/rotation/scaling, that's not the object's position. The center's coordinates are usually smaller or equal to the object's size. More details can be found in the sf::Drawable documentation...

Quote from: "kintantee"
My Rectangle objectets does not even drawn(or drawn but somewhere else, not in screen)
Check with the debugger if all parameters have meaningful values. Are you sure that you call sf::RenderWindow::Draw()?

Quote from: "kintantee"
does SetPosition take care of lower right point as well
Yes, the position is measured in global coordinates, i.e. relative to the left-upper corner of the window (in the default view). The point's position however is specified in local coordinates, i.e. relative to the origin/center of the shape. In SFML 2, this is simpler. Generally, a lot of things have improved meanwhile.

By the way, some general tips:
  • Use the constructor initializer list to initialize member variables.
  • Don't pass everything as const-reference. Small types like float should be passed by value, I would even pass sf::Vector2f (8 byte) by value.
  • Do you really need to store position, color, thickness etc. a second time? They are already stored in the sf::Shape, you can get the attributes with GetPosition(), GetPointColor() etc.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

kintantee

  • Newbie
  • *
  • Posts: 10
    • View Profile
Cannot move shapes
« Reply #5 on: January 01, 2012, 12:59:57 pm »
Thank you for your tips and help, It really helped a lot. I got two more questions.

first one: I didn't get what do you mean with initializer list.

I made some research and if you mean something like this:

Code: [Select]

class SomeClass
{
public:
    SomeClass(): shape_(sf::Shape(required parameters)) {}
private:
    sf::Shape shape_
};


It didn't seem much different than what I do and I don't know if there is a difference. I would appreciate if you explain me difference.


second: Why I can't create objects with


Code: [Select]

shape_.Circle(required parameters);


it works when I do like fallowing:
Code: [Select]

shape_ = sf::Shape::Circle(req. params.)