-
Notifications
You must be signed in to change notification settings - Fork 30
Vectors and Rectangles
In the following chapters, JSFML's vector and rectangle classes will be extensively and therefore, they should be presented beforehand. These are vectors in a mathematical sense, unlike the java.util.Vector
class, which implements a list.
All primitive type classes in JSFML are immutable, this means that once you created one, you cannot change its components, but have to create a new one. This is a design decision contrary to the original SFML design, but the lack of a const
modifier in Java makes it necessary.
Since Java does not allow operator overloading, mathematical operations are represented by static methods that take the respective amount of parameters. The semantics may seem a little odd at first, but they are the most realistic representation of n-ary operators and are more straightforward than instance methods that return new objects (rather than modifying the callees, which is not possible, since they are immutable).
A quick example of what this means: let us assume that we have vectors a
and b
and we want c
to contain their sum. In SFML (C++), we would write:
sf::Vector2f c = a + b;
In JSFML, it looks like this:
Vector2f c = Vector2f.add(a, b);
These design aspects are also true for the Color class (which can be interpreted as a four-dimensional vector), the IntRect and FloatRect classes and finally the Time class that will be covered later in the documentation.
JSFML provides three different vector classes, each serving different purposes. A generic vector class has been considered in the past, but the benefit did not seem worth the effort at all.
The Vector2f class represents a two-dimensional vector using the floating point components x
and y
. These are mostly used in JSFML to describe a position on the screen, but they can as well be interpreted as a direction or a translation offset.
Vector2i is basically the same as Vector2f, but uses integer components. Vector2i is commonly used for sizes (e.g. a texture's dimensions) or rounded screen coordinates (e.g. mouse coordinates).
Vector3i is the integral version of Vector3f and serves merely as a utility class.
Vector3f can be considered the extension of Vector2f by an additional component z
to represent a three-dimensional vector. These are used for sound spatialization (3D sound), which will be covered in a later section of the documentation.
All vector classes provide four basic arithmetic operations:
- [add](http://jsfml.org/javadoc/org/jsfml/system/Vector2f.html#add(org.jsfml.system.Vector2f, org.jsfml.system.Vector2f)) — Addition
- [sub](http://jsfml.org/javadoc/org/jsfml/system/Vector2f.html#sub(org.jsfml.system.Vector2f, org.jsfml.system.Vector2f)) — Subtraction
- [mul](http://jsfml.org/javadoc/org/jsfml/system/Vector2f.html#mul(float\)) — Multiplication by a scalar
- [div](http://jsfml.org/javadoc/org/jsfml/system/Vector2f.html#div(float\)) — Division by a scalar
- [componentwiseMul](http://jsfml.org/javadoc/org/jsfml/system/Vector2f.html#componentwiseMul(org.jsfml.system.Vector2f, org.jsfml.system.Vector2f)) — Component-wise multiplication by a vector
- [componentwiseDiv](http://jsfml.org/javadoc/org/jsfml/system/Vector2f.html#componentwiseDiv(org.jsfml.system.Vector2f, org.jsfml.system.Vector2f)) — Component-wise division by a vector
To represent two-dimensional areas on the screen, JSFML provides two rectangle classes.
Each of them has four components: left
and top
, which describe the position of the top left corner of the rectangle in the scene, and width
and height
, which describe the dimensions of the rectangle.
The FloatRect has floating point precision components and is typically used for AABBs (axis-aligned bounding boxes) of transformable objects
The IntRect is used for integer based rectangles like texture portions.
The [intersection](http://jsfml.org/javadoc/org/jsfml/graphics/FloatRect.html#intersection(org.jsfml.graphics.FloatRect\)) method checks whether a rectangle intersects another given rectangle and if so, it returns the area of intersection as another rectangle. This method can be used for rectangle collision detection.