Transformable

sfml.graphics.Transformable
See theTransformable companion object

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

This class is provided for convenience, on top of Transform.

Transform, 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 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 Transform), and keeps it up-to-date.

In addition to the position, rotation and scale, 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 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 Transform directly.

Transformable can be used as a base class. It is often combined with Drawable – that's what SFML's sprites, texts and shapes do.

val window: RenderWindow = ???

class MyEntity extends Transformable with Drawable:
   override def draw(target: RenderTarget, states: RenderStates): Unit =
       states.transform *= transform
       target.draw(???, states)

val entity = MyEntity()
entity.position = (10, 20)
entity.rotation = 45
window.draw(entity)

It can also be used as a member, if you don't want to use its API directly (because you don't need all its functions, or you have different naming conventions for example).

case class MyVector2(x: Float, y: Float)

class MyEntity:
   private val myTransform = Transformable()

   def position_=(v: MyVector2): Unit =
       myTransform.position = (v.x, v.y)

   def draw(target: RenderTarget): Unit =
       target.draw(???, RenderStates(myTransform.transform))

A note on coordinates and undistorted rendering: By default, SFML (or more exactly, OpenGL) may interpolate drawable objects such as sprites or texts when rendering. While this allows transitions like slow movements or rotations to appear smoothly, it can lead to unwanted results in some cases, for example blurred or distorted objects. In order to render a Drawable object pixel-perfectly, make sure the involved coordinates allow a 1:1 mapping of pixels in the window to texels (pixels in the texture). More specifically, this means:

  • The object's position, origin and scale have no fractional part
  • The object's and the view's rotation are a multiple of 90 degrees
  • The view's center and size have no fractional part

Attributes

See also
Companion
object
Graph
Supertypes
class Object
trait Matchable
class Any
Known subtypes
trait Shape
trait CircleShape
trait Sprite
trait Text

Members list

Value members

Concrete methods

get the inverse of the combined transform of the object

get the inverse of the combined transform of the object

Attributes

Returns

Inverse of the combined transformations applied to the object

See also
def move(offsetX: Float, offsetY: Float): Unit

Move the object by a given offset.

Move the object by a given offset.

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

import sfml.system.Vector2

val obj: Transformable = ???
val offsetX, offsetY: Float = ???

val pos: Vector2[Float] = obj.position
obj.position = (pos.x + offsetX, pos.y + offsetY)

Value parameters

offsetX

X offset

offsetY

Y offset

Attributes

See also
def move(offset: Vector2[Float]): Unit

Move the object by a given 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:

import sfml.system.Vector2

val obj: Transformable = ???
val offset: Vector2[Float] = ???

obj.position = obj.position + offset

Value parameters

offset

Offset

Attributes

See also
def origin: Vector2[Float]

get the local origin of the object

get the local origin of the object

Attributes

Returns

Current origin

See also
def origin_=(x: Float, y: Float): Unit

set the local origin of the object

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).

Value parameters

x

X coordinate of the new origin

y

Y coordinate of the new origin

Attributes

See also
def origin_=(origin: Vector2[Float]): Unit

set the local origin of the object

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).

Value parameters

origin

New origin

Attributes

See also
def position: Vector2[Float]

get the position of the object

get the position of the object

Attributes

Returns

Current position

See also
def position_=(x: Float, y: Float): Unit

set the position of the object

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).

Value parameters

x

X coordinate of the new position

y

Y coordinate of the new position

Attributes

See also
def position_=(position: Vector2[Float]): Unit

set the position of the object

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).

Value parameters

position

New position

Attributes

See also
def rotate(angle: Float): Unit

Rotate the object.

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:

val obj: Transformable = ???
val angle: Float = ???

obj.rotation = obj.rotation + angle

Value parameters

angle

Angle of rotation, in degrees

Attributes

def rotation: Float

get the orientation of the object

get the orientation of the object

The rotation is always in the range [0, 360].

Attributes

Returns

Current rotation, in degrees

See also
def rotation_=(angle: Float): Unit

set the orientation of the object

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.

Value parameters

angle

New rotation, in degrees

Attributes

See also
def scale(factorX: Float, factorY: Float): Unit

Scale the object.

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:

import sfml.system.Vector2

val obj: Transformable = ???
val factorX, factorY: Float = ???

val scale: Vector2[Float] = obj.scale
obj.scale = (scale.x + factorX, scale.y + factorY)

Value parameters

factorX

Horizontal scale factor

factorY

Vertical scale factor

Attributes

See also
def scale(factor: Vector2[Float]): Unit

Scale the object.

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:

import sfml.system.Vector2

val obj: Transformable = ???
val factor: Vector2[Float] = ???

val scale: Vector2[Float] = obj.scale
obj.scale = (scale.x * factor.x, scale.y * factor.y)

Value parameters

factor

Scale factors

Attributes

See also
def scale: Vector2[Float]

get the current scale of the object

get the current scale of the object

Attributes

Returns

Current scale factors

See also
def scale_=(x: Float, y: Float): Unit

set the scale factors of the object

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).

Value parameters

factorX

New horizontal scale factor

factorY

New vertical scale factor

Attributes

See also
def scale_=(factors: Vector2[Float]): Unit

set the scale factors of the object

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).

Value parameters

factors

New scale factors

Attributes

See also

get the combined transform of the object

get the combined transform of the object

Attributes

Returns

Transform combining the position/rotation/scale/origin of the object

See also