Skip to content

struct SF::Transform
inherits Struct #

Define a 3x3 transform matrix

A SF::Transform specifies how to translate, rotate, scale, shear, project, whatever things. In mathematical terms, it defines how to transform a coordinate system into another.

For example, if you apply a rotation transform to a sprite, the result will be a rotated sprite. And anything that is transformed by this rotation transform will be rotated the same way, according to its initial position.

Transforms are typically used for drawing. But they can also be used for any computation that requires to transform points between the local and global coordinate systems of an entity (like collision detection).

Example:

# define a translation transform
translation = SF::Transform.new
translation.translate(20, 50)

# define a rotation transform
rotation = SF::Transform.new
rotation.rotate(45)

# combine them
transform = translation * rotation

# use the result to transform stuff...
point = transform.transform_point(10, 20)
rect = transform.transform_rect(SF.float_rect(0, 0, 10, 100))

See also: SF::Transformable, SF::RenderStates

Constants#

Identity = new#

The identity transform (does nothing)

Constructors#

.new(a00 : Number, a01 : Number, a02 : Number, a10 : Number, a11 : Number, a12 : Number, a20 : Number, a21 : Number, a22 : Number)#

Construct a transform from a 3x3 matrix

  • a00 - Element (0, 0) of the matrix
  • a01 - Element (0, 1) of the matrix
  • a02 - Element (0, 2) of the matrix
  • a10 - Element (1, 0) of the matrix
  • a11 - Element (1, 1) of the matrix
  • a12 - Element (1, 2) of the matrix
  • a20 - Element (2, 0) of the matrix
  • a21 - Element (2, 1) of the matrix
  • a22 - Element (2, 2) of the matrix
View source

.new#

Default constructor

Creates an identity transform (a transform that does nothing).

View source

Methods#

#!=(right : Transform) : Bool#

Overload of binary operator != to compare two transforms

This call is equivalent to !(left == right).

  • left - Left operand (the first transform)
  • right - Right operand (the second transform)

Returns: true if the transforms are not equal, false otherwise

View source

#*(right : Transform) : Transform#

Overload of binary operator * to combine two transforms

This call is equivalent to calling Transform(left).combine(right).

  • left - Left operand (the first transform)
  • right - Right operand (the second transform)

Returns: New combined transform

View source

#*(right : Vector2 | Tuple) : Vector2f#

Overload of binary operator * to transform a point

This call is equivalent to calling left.transform_point(right).

  • left - Left operand (the transform)
  • right - Right operand (the point to transform)

Returns: New transformed point

View source

#==(right : Transform) : Bool#

Overload of binary operator == to compare two transforms

Performs an element-wise comparison of the elements of the left transform with the elements of the right transform.

  • left - Left operand (the first transform)
  • right - Right operand (the second transform)

Returns: true if the transforms are equal, false otherwise

View source

#combine(transform : Transform) : Transform#

Combine the current transform with another one

The result is a transform that is equivalent to applying transform followed by self. Mathematically, it is equivalent to a matrix multiplication self * transform.

These two statements are equivalent:

left.combine(right)
left *= right

  • transform - Transform to combine with this transform

Returns: self

View source

#dup : Transform#

Returns a shallow copy of this object.

Because Value is a value type, this method returns self, which already involves a shallow copy of this object because value types are passed by value.

View source

#inspect(io)#

View source

#inverse : Transform#

Return the inverse of the transform

If the inverse cannot be computed, an identity transform is returned.

Returns: A new transform which is the inverse of self

View source

#matrix : ::Pointer(Float32)#

Return the transform as a 4x4 matrix

This function returns a pointer to an array of 16 floats containing the transform elements as a 4x4 matrix, which is directly compatible with OpenGL functions.

transform = (...)
glLoadMatrixf(transform.matrix)

Returns: Pointer to a 4x4 matrix

View source

#rotate(angle : Number, center_x : Number, center_y : Number) : Transform#

Combine the current transform with a rotation

The center of rotation is provided for convenience as a second argument, so that you can build rotations around arbitrary points more easily (and efficiently) than the usual translate(-center).rotate(angle).translate(center).

This function returns self, so that calls can be chained.

transform = SF::Transform.new
transform.rotate(90, 8, 3).translate(50, 20)

  • angle - Rotation angle, in degrees
  • center_x - X coordinate of the center of rotation
  • center_y - Y coordinate of the center of rotation

Returns: self

See also: translate, scale

View source

#rotate(angle : Number, center : Vector2 | Tuple) : Transform#

Combine the current transform with a rotation

The center of rotation is provided for convenience as a second argument, so that you can build rotations around arbitrary points more easily (and efficiently) than the usual translate(-center).rotate(angle).translate(center).

This function returns self, so that calls can be chained.

transform = SF::Transform.new
transform.rotate(90, SF.vector2f(8, 3)).translate(SF.vector2f(50, 20))

  • angle - Rotation angle, in degrees
  • center - Center of rotation

Returns: self

See also: translate, scale

View source

#rotate(angle : Number) : Transform#

Combine the current transform with a rotation

This function returns self, so that calls can be chained.

transform = SF::Transform.new
transform.rotate(90).translate(50, 20)

  • angle - Rotation angle, in degrees

Returns: self

See also: translate, scale

View source

#scale(scale_x : Number, scale_y : Number, center_x : Number, center_y : Number) : Transform#

Combine the current transform with a scaling

The center of scaling is provided for convenience as a second argument, so that you can build scaling around arbitrary points more easily (and efficiently) than the usual translate(-center).scale(factors).translate(center).

This function returns self, so that calls can be chained.

transform = SF::Transform.new
transform.scale(2, 1, 8, 3).rotate(45)

  • scale_x - Scaling factor on X axis
  • scale_y - Scaling factor on Y axis
  • center_x - X coordinate of the center of scaling
  • center_y - Y coordinate of the center of scaling

Returns: self

See also: translate, rotate

View source

#scale(scale_x : Number, scale_y : Number) : Transform#

Combine the current transform with a scaling

This function returns self, so that calls can be chained.

transform = SF::Transform.new
transform.scale(2, 1).rotate(45)

  • scale_x - Scaling factor on the X axis
  • scale_y - Scaling factor on the Y axis

Returns: self

See also: translate, rotate

View source

#scale(factors : Vector2 | Tuple, center : Vector2 | Tuple) : Transform#

Combine the current transform with a scaling

The center of scaling is provided for convenience as a second argument, so that you can build scaling around arbitrary points more easily (and efficiently) than the usual translate(-center).scale(factors).translate(center).

This function returns self, so that calls can be chained.

transform = SF::Transform.new
transform.scale(SF.vector2f(2, 1), SF.vector2f(8, 3)).rotate(45)

  • factors - Scaling factors
  • center - Center of scaling

Returns: self

See also: translate, rotate

View source

#scale(factors : Vector2 | Tuple) : Transform#

Combine the current transform with a scaling

This function returns self, so that calls can be chained.

transform = SF::Transform.new
transform.scale(SF.vector2f(2, 1)).rotate(45)

  • factors - Scaling factors

Returns: self

See also: translate, rotate

View source

#transform_point(x : Number, y : Number) : Vector2f#

Transform a 2D point

These two statements are equivalent:

transformed_point = matrix.transformPoint(x, y)
transformed_point = matrix * SF.vector2f(x, y)

  • x - X coordinate of the point to transform
  • y - Y coordinate of the point to transform

Returns: Transformed point

View source

#transform_point(point : Vector2 | Tuple) : Vector2f#

Transform a 2D point

These two statements are equivalent:

transformed_point = matrix.transformPoint(point)
transformed_point = matrix * point

  • point - Point to transform

Returns: Transformed point

View source

#transform_rect(rectangle : FloatRect) : FloatRect#

Transform a rectangle

Since SFML doesn't provide support for oriented rectangles, the result of this function is always an axis-aligned rectangle. Which means that if the transform contains a rotation, the bounding rectangle of the transformed rectangle is returned.

  • rectangle - Rectangle to transform

Returns: Transformed rectangle

View source

#translate(x : Number, y : Number) : Transform#

Combine the current transform with a translation

This function returns self, so that calls can be chained.

transform = SF::Transform.new
transform.translate(100, 200).rotate(45)

  • x - Offset to apply on X axis
  • y - Offset to apply on Y axis

Returns: self

See also: rotate, scale

View source

#translate(offset : Vector2 | Tuple) : Transform#

Combine the current transform with a translation

This function returns self, so that calls can be chained.

transform = SF::Transform.new
transform.translate(SF.vector2f(100, 200)).rotate(45)

  • offset - Translation offset to apply

Returns: self

See also: rotate, scale

View source