struct SF::Transform
- SF::Transform
- Struct
- Value
- Object
Overview
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
Defined in:
graphics/graphics.crgraphics/obj.cr
Constant Summary
-
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
-
.new
Default constructor
Instance Method Summary
-
#!=(right : Transform) : Bool
Overload of binary operator != to compare two transforms
-
#*(right : Transform) : Transform
Overload of binary operator * to combine two transforms
-
#*(right : Vector2 | Tuple) : Vector2f
Overload of binary operator * to transform a point
-
#==(right : Transform) : Bool
Overload of binary operator == to compare two transforms
-
#combine(transform : Transform) : Transform
Combine the current transform with another one
-
#dup : Transform
Returns a shallow copy of this object.
- #inspect(io)
-
#inverse : Transform
Return the inverse of the transform
-
#matrix : Pointer(Float32)
Return the transform as a 4x4 matrix
-
#rotate(angle : Number, center_x : Number, center_y : Number) : Transform
Combine the current transform with a rotation
-
#rotate(angle : Number, center : Vector2 | Tuple) : Transform
Combine the current transform with a rotation
-
#rotate(angle : Number) : Transform
Combine the current transform with a rotation
-
#scale(scale_x : Number, scale_y : Number, center_x : Number, center_y : Number) : Transform
Combine the current transform with a scaling
-
#scale(scale_x : Number, scale_y : Number) : Transform
Combine the current transform with a scaling
-
#scale(factors : Vector2 | Tuple, center : Vector2 | Tuple) : Transform
Combine the current transform with a scaling
-
#scale(factors : Vector2 | Tuple) : Transform
Combine the current transform with a scaling
-
#transform_point(x : Number, y : Number) : Vector2f
Transform a 2D point
-
#transform_point(point : Vector2 | Tuple) : Vector2f
Transform a 2D point
-
#transform_rect(rectangle : FloatRect) : FloatRect
Transform a rectangle
-
#translate(x : Number, y : Number) : Transform
Combine the current transform with a translation
-
#translate(offset : Vector2 | Tuple) : Transform
Combine the current transform with a translation
Constructor Detail
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
Instance Method Detail
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
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
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
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
Combine the current transform with another one
The result is a transform that is equivalent to applying
self
followed by transform. Mathematically, it is
equivalent to a matrix multiplication.
- transform - Transform to combine with this transform
Returns: self
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.
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
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
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
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
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
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
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
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
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
Transform a 2D point
- x - X coordinate of the point to transform
- y - Y coordinate of the point to transform
Returns: Transformed point
Transform a 2D point
- point - Point to transform
Returns: Transformed point
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
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
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