Skip to content
This repository has been archived by the owner on Jun 12, 2019. It is now read-only.

Vectors and Rectangles

pdinklag edited this page Dec 31, 2012 · 5 revisions

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.

Design aspects

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.

Vectors

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.

Vector2f

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

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

Vector3i is the integral version of Vector3f and serves merely as a utility class.

Vector3f

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.

Arithmetics

All vector classes provide four basic arithmetic operations:

Rectangles

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.

FloatRect

The FloatRect has floating point precision components and is typically used for AABBs (axis-aligned bounding boxes) of transformable objects

IntRect

The IntRect is used for integer based rectangles like texture portions.

Rectangle Collision

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.

Clone this wiki locally