Gamedev Framework (gf)  0.3.0
A C++11 framework for 2D games
Public Member Functions | Protected Member Functions | List of all members
gf::Transformable Class Reference

Decomposed transform defined by a position, a rotation and a scale. More...

#include <gf/Transformable.h>

Inheritance diagram for gf::Transformable:
Inheritance graph
[legend]

Public Member Functions

 Transformable ()
 Default constructor. More...
 
void setOrigin (Vector2f origin)
 Set the local origin of the object. More...
 
Vector2f getOrigin () const
 Get the local origin of the object. More...
 
void setPosition (Vector2f position)
 Set the position of the object. More...
 
Vector2f getPosition () const
 Get the position of the object. More...
 
void move (Vector2f offset)
 Move the object by a given offset. More...
 
void setRotation (float angle)
 Set the orientation of the object. More...
 
float getRotation () const
 Get the orientation of the object. More...
 
void rotate (float angle)
 Rotate the object. More...
 
void setScale (Vector2f factors)
 Set the scale factors of the object. More...
 
void setScale (float factor)
 Set the scale factor of the object. More...
 
Vector2f getScale () const
 Get the current scale of the object. More...
 
void scale (Vector2f factors)
 Scale the object. More...
 
void scale (float factor)
 Scale the object. More...
 
Matrix3f getTransform () const
 Get the combined transform of the object. More...
 
Matrix3f getInverseTransform () const
 Get the inverse of the combined transform of the object. More...
 
- Public Member Functions inherited from gf::Drawable
virtual ~Drawable ()
 Virtual desctructor. More...
 
virtual void draw (RenderTarget &target, RenderStates states)=0
 Draw the object to a render target. More...
 

Protected Member Functions

void setOriginFromAnchorAndBounds (Anchor anchor, const RectF &bounds)
 Set the origin from an anchor and bounds. More...
 

Detailed Description

Decomposed transform defined by a position, a rotation and a scale.

gf::Matrix3f, as a low-level class, offers a great level of flexibility but it is not always convenient to manage. Indeed, one can easily combine any kind of operation, such as a translation followed by a rotation followed by a scaling, but once the result transform is built, there's no way to go backward and, let's say, change only the rotation without modifying the translation and scaling. The entire transform must be recomputed, which means that you need to retrieve the initial translation and scale factors as well, and combine them the same way you did before updating the rotation. This is a tedious operation, and it requires to store all the individual components of the final transform.

That's exactly what gf::Transformable was written for: it hides these variables and the composed transform behind an easy to use interface. You can set or get any of the individual components without worrying about the others. It also provides the composed transform (as a gf::Matrix3f), and keeps it up-to-date.

In addition to the position, rotation and scale, gf::Transformable provides an "origin" component, which represents the local origin of the three other components. Let's take an example with a 10x10 pixels sprite. By default, the sprite is positioned/rotated/scaled relatively to its top-left corner, because it is the local point \((0, 0)\). But if we change the origin to be \((5, 5)\), the sprite will be positioned/rotated/scaled around its center instead. And if we set the origin to \((10, 10)\), it will be transformed around its bottom-right corner.

To keep the gf::Transformable class simple, there's only one origin for all the components. You cannot position the sprite relatively to its top-left corner while rotating it around its center, for example. To do such things, use gf::Matrix3f directly.

gf::Transformable can be used as a base class. That's what gf's sprites, texts, curves and shapes do.

class MyEntity : public gf::Transformable {
virtual void draw(gf::RenderTarget& target, gf::RenderStates states) override {
states.transform *= getTransform();
target.draw(..., states);
}
};
MyEntity entity;
entity.setPosition(10, 20);
entity.setRotation(45);
renderer.draw(entity);
See also
gf::Matrix3f

Constructor & Destructor Documentation

gf::Transformable::Transformable ( )

Default constructor.

By default:

  • the origin is set to \((0,0)\)
  • the position is set to \((0,0)\)
  • the rotation is set to \( 0 \)
  • the scale is set to \((1,1)\)

Member Function Documentation

Matrix3f gf::Transformable::getInverseTransform ( ) const

Get the inverse of the combined transform of the object.

Returns
Inverse of the combined transformations applied to the object
See also
getTransform()
Vector2f gf::Transformable::getOrigin ( ) const
inline

Get the local origin of the object.

Returns
Current origin
See also
setOrigin()
Vector2f gf::Transformable::getPosition ( ) const
inline

Get the position of the object.

Returns
Current position
See also
setPosition
float gf::Transformable::getRotation ( ) const
inline

Get the orientation of the object.

Returns
Current rotation, in radians
See also
setRotation()
Vector2f gf::Transformable::getScale ( ) const
inline

Get the current scale of the object.

Returns
Current scale factors
See also
setScale()
Matrix3f gf::Transformable::getTransform ( ) const

Get the combined transform of the object.

The combined transform of the object is (in this order):

  • a translation of -origin
  • a scaling of the defined scaling factors
  • a rotation of the defined angle
  • a translation of the defined position
Returns
Transform combining the position/rotation/scale/origin of the object
See also
getInverseTransform()
void gf::Transformable::move ( Vector2f  offset)

Move the object by a given offset.

This function adds to the current position of the object, unlike setPosition() which overwrites it. Thus, it is equivalent to the following code:

object.setPosition(object.getPosition() + offset);
Parameters
offsetOffset
See also
setPosition()
void gf::Transformable::rotate ( float  angle)

Rotate the object.

This function adds to the current rotation of the object, unlike setRotation() which overwrites it. Thus, it is equivalent to the following code:

object.setRotation(object.getRotation() + angle);
Parameters
angleAngle of rotation, in radians
void gf::Transformable::scale ( Vector2f  factors)

Scale the object.

This function multiplies the current scale of the object, unlike setScale() which overwrites it. Thus, it is equivalent to the following code:

object.setScale(object.getScale() * factors);
Parameters
factorsScale factors
See also
setScale()
void gf::Transformable::scale ( float  factor)
inline

Scale the object.

This function is a shortcut when the scale factors for \( x \) and \( y \) are the same. It is equivalent to:

object.scale({ factor, factor });
Parameters
factorScale factor
See also
setScale()
void gf::Transformable::setOrigin ( Vector2f  origin)

Set the local origin of the object.

The origin of an object defines the center point for all transformations (position, scale, rotation). The coordinates of this point must be relative to the top-left corner of the object, and ignore all transformations (position, scale, rotation).

The default origin of a transformable object is \((0, 0)\).

Parameters
originNew origin
See also
getOrigin()
void gf::Transformable::setOriginFromAnchorAndBounds ( Anchor  anchor,
const RectF bounds 
)
protected

Set the origin from an anchor and bounds.

This function can be called from derived classes for setting the origin properly thanks to an anchor and the bounds they computed.

Parameters
anchorAn anchor
boundsThe bounds of the entity
void gf::Transformable::setPosition ( Vector2f  position)

Set the position of the object.

This function completely overwrites the previous position. See the move() function to apply an offset based on the previous position instead.

The default position of a transformable object is \((0, 0)\).

Parameters
positionNew position
See also
move(), getPosition()
void gf::Transformable::setRotation ( float  angle)

Set the orientation of the object.

This function completely overwrites the previous rotation. See the rotate() function to add an angle based on the previous rotation instead.

The default rotation of a transformable object is \( 0 \).

Parameters
angleNew rotation, in radians
See also
rotate(), getRotation()
void gf::Transformable::setScale ( Vector2f  factors)

Set the scale factors of the object.

This function completely overwrites the previous scale. See the scale() function to add a factor based on the previous scale instead.

The default scale of a transformable object is \((1, 1)\).

Parameters
factorsNew scale factors
See also
scale(), getScale()
void gf::Transformable::setScale ( float  factor)
inline

Set the scale factor of the object.

This function is a shortcut when the scale factors for \( x \) and \( y \) are the same. It is equivalent to:

object.setScale({ factor, factor });
Parameters
factorNew scale factor
See also
scale(), getScale()