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

Author Topic: When an sf::Shape::SetPosition is called, what is moved?  (Read 2178 times)

0 Members and 1 Guest are viewing this topic.

JordyD

  • Newbie
  • *
  • Posts: 10
    • View Profile
When an sf::Shape::SetPosition is called, what is moved?
« on: August 04, 2009, 04:46:00 am »
Center? Upper-left corner? I was wondering because I'm trying to get sf::Rect and sf::Shape to play nice, meaning they would both move at the same time. So I've implemented a class like this:
Shape.h
Code: [Select]
/*
 * Shape.h
 *
 *  Created on: Aug 3, 2009
 *      Author: Jordy
 */

#ifndef SHAPE_H_
#define SHAPE_H_

#include <algorithm>
#include <SFML/Graphics/Shape.hpp>
#include <SFML/Graphics/Rect.hpp>
#include <SFML/System/Vector2.hpp>

namespace Solids
{

class Shape : public sf::Shape, public sf::Rect<float>
{
public:
Shape();
virtual ~Shape();

virtual void Move(float OffsetX, float OffsetY);
virtual void Move(const sf::Vector2f &Offset);
virtual void SetPosition(float OffsetX, float OffsetY);
virtual void SetPosition(const sf::Vector2f &Offset);

void SetSpeed(float NewSpeedX, float NewSpeedY);
void SetSpeedX(float NewSpeedX);
void SetSpeedY(float NewSpeedY);

void Update(float FrameTime=1);

static Shape Rectangle(float X, float Y, float Width, float Height);
static Shape Triangle(float P1X, float P1Y, float P2X, float P2Y, float P3X, float P3Y);

private:
float SpeedX, SpeedY;
};

}

#endif /* SHAPE_H_ */

Shape.cpp
Code: [Select]
/*
 * Shape.cpp
 *
 *  Created on: Aug 3, 2009
 *      Author: Jordy
 */

#include "Shape.h"

namespace Solids
{

Shape::Shape()
{
}

Shape::~Shape()
{
}

void Shape::Move(float OffsetX, float OffsetY)
{
sf::Shape::Move(OffsetX, OffsetY);
Offset(OffsetX, OffsetY);
}

void Shape::Move(const sf::Vector2f &Offset)
{
Move(Offset.x, Offset.y);
}


void Shape::SetSpeed(float NewSpeedX, float NewSpeedY)
{
SpeedX = NewSpeedX;
SpeedY = NewSpeedY;
}

void Shape::SetSpeedX(float NewSpeedX)
{
SpeedX = NewSpeedX;
}

void Shape::SetSpeedY(float NewSpeedY)
{
SpeedY = NewSpeedY;
}

void Shape::Update(float FrameTime)
{
Move(SpeedX * FrameTime, SpeedY * FrameTime);
}

Shape Shape::Rectangle(float X, float Y, float Width, float Height)
{
Shape Rect;

Rect.Left   = X;
Rect.Top    = Y;
Rect.Right  = Rect.Left + Width;
Rect.Bottom = Rect.Top + Height;

Rect.AddPoint(Rect.Left,  Rect.Top);
Rect.AddPoint(Rect.Right, Rect.Top);
Rect.AddPoint(Rect.Right, Rect.Bottom);
Rect.AddPoint(Rect.Left,  Rect.Bottom);

return Rect;
}

Shape Shape::Triangle(float P1X, float P1Y, float P2X, float P2Y, float P3X, float P3Y)
{
Shape Tri;

// Find boundaries of enclosing sf::Rect
Tri.Left   = std::min(std::min(P1X, P2X), P3X);
Tri.Right  = std::max(std::max(P1X, P2X), P3X);
Tri.Top    = std::min(std::min(P1Y, P2Y), P3Y);
Tri.Bottom = std::max(std::max(P1Y, P2Y), P3Y);

Tri.AddPoint(P1X, P1Y);
Tri.AddPoint(P2X, P2Y);
Tri.AddPoint(P3X, P3Y);

return Tri;
}

}


As you can see, I've implemented a Shape::Move method, but not the Shape::SetPosition method. This is because I'm not sure whether I should be doing this:
Code: [Select]
void Shape::SetPosition(float PosX, float PosY)
{
sf::Shape::SetPosition(PosX, PosY);

float Width  = GetWidth();
float Height = GetHeight();

Left   = PosX;
Top    = PosY;
Right  = Left + Width;
Bottom = Top + Height;
}

or this:
Code: [Select]
void Shape::SetPosition(float PosX, float PosY)
{
sf::Shape::SetPosition(PosX, PosY);

float Width  = GetWidth();
float Height = GetHeight();

Left   = PosX - Width / 2;
Top    = PosY - Height / 2;
Right  = Left + Width;
Bottom = Top  + Height;
}


Note that there may be other errors in my code. I haven't tested it thoroughly yet.

Thanks,
Jordy

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
When an sf::Shape::SetPosition is called, what is moved?
« Reply #1 on: August 04, 2009, 07:53:38 am »
SetPosition sets the position of the center point of the drawable, which is the local point (0, 0) by default (upper-left corner), but can be changed with SetCenter.
Laurent Gomila - SFML developer

JordyD

  • Newbie
  • *
  • Posts: 10
    • View Profile
When an sf::Shape::SetPosition is called, what is moved?
« Reply #2 on: August 04, 2009, 03:04:30 pm »
Quote from: "Laurent"
SetPosition sets the position of the center point of the drawable, which is the local point (0, 0) by default (upper-left corner), but can be changed with SetCenter.


OK, thank you. I think I'll have to make my own SetCenter in that case to make sure that the center of the sf::Rect is also being changed.