Physics

Physics lib :)

Properties
NameTypeDescription
Vector2Vector2

Methods

(static) calcAngleBetweenRectangles(rect1, rect2) → {number}

Calculates the angle between the centers of two rectangles, given as instances of Rectangle, in radians.

Parameters:
NameTypeDescription
rect1Object

The first rectangle, represented as an instance of Rectangle.

Properties
NameTypeDescription
xnumber

The x-coordinate of the top-left corner of the first rectangle.

ynumber

The y-coordinate of the top-left corner of the first rectangle.

widthnumber

The width of the first rectangle.

heightnumber

The height of the first rectangle.

rect2Object

The second rectangle, represented as an instance of Rectangle.

Properties
NameTypeDescription
xnumber

The x-coordinate of the top-left corner of the second rectangle.

ynumber

The y-coordinate of the top-left corner of the second rectangle.

widthnumber

The width of the second rectangle.

heightnumber

The height of the second rectangle.

Returns:

The angle between the centers of the two rectangles, in radians.

Type: 
number
Examples
const rect1 = { x: 0, y: 0, width: 2, height: 2 };
const rect2 = { x: 2, y: 2, width: 2, height: 2 };
calcAngleBetweenRectangles(rect1, rect2); // output: 0.7853981633974483
const rect1 = { x: 0, y: 0, width: 2, height: 2 };
const rect2 = { x: 1, y: 2, width: 2, height: 2 };
calcAngleBetweenRectangles(rect1, rect2); // output: 1.1071487177940904

(static) calcAngleBetweenRectanglesByCoordinates(x1, y1, width1, height1, x2, y2, width2, height2) → {number}

Gets the angle between two rectangles defined by their coordinates.

Parameters:
NameTypeDescription
x1number

The x-coordinate of the first rectangle.

y1number

The y-coordinate of the first rectangle.

width1number

The width of the first rectangle.

height1number

The height of the first rectangle.

x2number

The x-coordinate of the second rectangle.

y2number

The y-coordinate of the second rectangle.

width2number

The width of the second rectangle.

height2number

The height of the second rectangle.

Returns:

The angle between the two rectangles.

Type: 
number
Examples
calcAngleBetweenRectanglesByCoordinates(0, 0, 10, 10, 20, 20, 20, 20); // Output: 45
calcAngleBetweenRectanglesByCoordinates(10, 10, 10, 10, 30, 30, 20, 20); // Output: 30

(static) calcAngleBetweenTwoPointsDegrees(x1, y1, x2, y2) → {number}

Calculates the angle between two points in a two-dimensional plane, in degrees.

Parameters:
NameTypeDescription
x1number

The x-coordinate of the first point.

y1number

The y-coordinate of the first point.

x2number

The x-coordinate of the second point.

y2number

The y-coordinate of the second point.

Returns:

The angle between the two points, in degrees.

Type: 
number
Examples
calcAngleBetweenTwoPointsDegrees(0, 0, 1, 1); // output: 45
calcAngleBetweenTwoPointsDegrees(0, 0, 0, 1); // output: 90

(static) calcAngleBetweenTwoPointsVector2(point1, point2) → {number}

Calculates the angle between two points in a two-dimensional plane, given as instances of Vector2.

Parameters:
NameTypeDescription
point1Object

The first point, represented as an instance of Vector2.

Properties
NameTypeDescription
xnumber

The x-coordinate of the first point.

ynumber

The y-coordinate of the first point.

point2Object

The second point, represented as an instance of Vector2.

Properties
NameTypeDescription
xnumber

The x-coordinate of the second point.

ynumber

The y-coordinate of the second point.

Returns:

The angle between the two points, in radians.

Type: 
number
Examples
const point1 = { x: 0, y: 0 };
const point2 = { x: 1, y: 1 };
calcAngleBetweenTwoPointsVector2(point1, point2); // output: 0.7853981633974483 (approx. 45°)
const point1 = { x: 0, y: 0 };
const point2 = { x: 0, y: 1 };
calcAngleBetweenTwoPointsVector2(point1, point2); // output: 1.5707963267948966 (approx. 90°)

(static) calcAngleBetweenTwoPointsVector2Degrees(point1, point2) → {number}

Calculates the angle between two points in a two-dimensional plane, given as instances of Vector2, in degrees.

Parameters:
NameTypeDescription
point1Object

The first point, represented as an instance of Vector2.

Properties
NameTypeDescription
xnumber

The x-coordinate of the first point.

ynumber

The y-coordinate of the first point.

point2Object

The second point, represented as an instance of Vector2.

Properties
NameTypeDescription
xnumber

The x-coordinate of the second point.

ynumber

The y-coordinate of the second point.

Returns:

The angle between the two points, in degrees.

Type: 
number
Examples
const point1 = { x: 0, y: 0 };
const point2 = { x: 1, y: 1 };
calcAngleBetweenTwoPointsVector2Degrees(point1, point2); // output: 45
const point1 = { x: 0, y: 0 };
const point2 = { x: 0, y: 1 };
calcAngleBetweenTwoPointsVector2Degrees(point1, point2); // output: 90

(static) calcCircleArea(radius) → {number}

Gets the area of a circle.

Parameters:
NameTypeDescription
radiusnumber

The radius of the circle.

Returns:

The area of the circle.

Type: 
number
Examples
calcCircleArea(5); // Output: 78.53981633974483
calcCircleArea(10); // Output: 314.1592653589793

(static) calcCirclePerimeter(radius) → {number}

Gets the perimeter of a circle.

Parameters:
NameTypeDescription
radiusnumber

The radius of the circle.

Returns:

The perimeter of the circle.

Type: 
number
Examples
calcCirclePerimeter(5); // Output: 31.41592653589793
calcCirclePerimeter(10); // Output: 62.83185307179586

(static) calcRectangleArea(width, height) → {number}

Gets the area of a rectangle.

Parameters:
NameTypeDescription
widthnumber

The width of the rectangle.

heightnumber

The height of the rectangle.

Returns:

The area of the rectangle.

Type: 
number
Examples
calcRectangleArea(5, 10); // Output: 50
calcRectangleArea(10, 20);

(static) calcRectangleCenter(x, y, width, height) → {object}

Calculates the center coordinates of a rectangle given its x-coordinate, y-coordinate, width, and height.

Parameters:
NameTypeDescription
xnumber

The x-coordinate of the rectangle

ynumber

The y-coordinate of the rectangle

widthnumber

The width of the rectangle

heightnumber

The height of the rectangle

Returns:

An object with x and y properties representing the center coordinates of the rectangle

Type: 
object
Example
calcRectangleCenter(10, 10, 20, 20); // returns { x: 15, y: 15 }

(static) calcRectangleCenterFromBounds(rectangle) → {object}

Returns the center of a rectangle, given the rectangle's bounds.

Parameters:
NameTypeDescription
rectangleobject

The rectangle's bounds, represented as an object with x, y, width, and height properties.

Returns:

The center of the rectangle, represented as an object with x and y properties.

Type: 
object
Example
const rect = { x: 10, y: 10, width: 20, height: 20 };
calcRectangleCenterFromBounds(rect); // { x: 20, y: 20 }

(static) calcRectangleCenterX(x, width) → {number}

Calculates the center x-coordinate of a rectangle given its x-coordinate and width.

Parameters:
NameTypeDescription
xnumber

The x-coordinate of the rectangle

widthnumber

The width of the rectangle

Returns:

The center x-coordinate of the rectangle

Type: 
number
Example
calcRectangleCenterX(10, 20); // returns 15

(static) calcRectangleCenterXFromBounds(rectangle) → {number}

Calculates the center x-coordinate of a rectangle given its bounds.

Parameters:
NameTypeDescription
rectangleobject

An object with x, y, width, and height properties representing the bounds of the rectangle

Returns:

The center x-coordinate of the rectangle

Type: 
number
Example
calcRectangleCenterXFromBounds({ x: 10, y: 10, width: 20, height: 20 }); // returns 15

(static) calcRectangleCenterY(y, height) → {number}

Calculates the center y-coordinate of a rectangle given its y-coordinate and height.

Parameters:
NameTypeDescription
ynumber

The y-coordinate of the rectangle

heightnumber

The height of the rectangle

Returns:

The center y-coordinate of the rectangle

Type: 
number
Example
calcRectangleCenterY(10, 20); // returns 15

(static) calcRectangleCenterYFromBounds(rectangle) → {number}

Returns the Y-coordinate of the center of a rectangle, given the rectangle's bounds.

Parameters:
NameTypeDescription
rectangleobject

The rectangle's bounds, represented as an object with y and height properties.

Returns:

The Y-coordinate of the center of the rectangle.

Type: 
number
Examples
calcRectangleCenterYFromBounds({ x: 10, y: 10, y: 10, height: 20 }); // 20
calcRectangleCenterYFromBounds({ x: 10, y: 10, width: 20, height: 20 }); // returns 15

(static) calcRectanglePerimeter(rectangle) → {number}

Gets the perimeter of a rectangle defined by its bounds.

Parameters:
NameTypeDescription
rectangleObject

The rectangle bounds object.

Properties
NameTypeDescription
xnumber

The x-coordinate of the rectangle.

ynumber

The y-coordinate of the rectangle.

widthnumber

The width of the rectangle.

heightnumber

The height of the rectangle.

Returns:

The perimeter of the rectangle.

Type: 
number
Examples
const rectangle = { x: 0, y: 0, width: 5, height: 10 }; 
calcRectanglePerimeter(rectangle); // Output: 30
const rectangle = { x: 10, y: 20, width: 10, height: 20 }; 
calcRectanglePerimeter(rectangle); // Output: 60

(static) calcRectanglePerimeterByDimensions(width, height) → {number}

Gets the perimeter of a rectangle.

Parameters:
NameTypeDescription
widthnumber

The width of the rectangle.

heightnumber

The height of the rectangle.

Returns:

The perimeter of the rectangle.

Type: 
number
Examples
calcRectanglePerimeterByDimensions(5, 10); // Output: 30
calcRectanglePerimeterByDimensions(10, 20); // Output: 60

(static) calcRectangleVertices(x, y, width, height) → {Array}

Returns an array of objects that represent the vertices of a rectangle.

Parameters:
NameTypeDescription
xnumber

The x-coordinate of the top-left corner of the rectangle.

ynumber

The y-coordinate of the top-left corner of the rectangle.

widthnumber

The width of the rectangle.

heightnumber

The height of the rectangle.

Returns:

An array of objects that represent the vertices of the rectangle, where each object has properties 'x' and 'y'.

Type: 
Array
Example
calcRectangleVertices(0, 0, 2, 3); // [{x: 0, y: 0}, {x: 2, y: 0}, {x: 2, y: 3}, {x: 0, y: 3}]

(static) calcVerticesFromRectangleBounds(rectangle) → {Array}

Returns an array of objects that represent the vertices of a rectangle, given its bounds.

Parameters:
NameTypeDescription
rectangleObject

An object that represents the bounds of the rectangle, with properties 'x', 'y', 'width', and 'height'.

Returns:

An array of objects that represent the vertices of the rectangle, where each object has properties 'x' and 'y'.

Type: 
Array
Example
calcVerticesFromRectangleBounds({ x: 0, y: 0, width: 2, height: 3 }); // [{x: 0, y: 0}, {x: 2, y: 0}, {x: 2, y: 3}, {x: 0, y: 3}]

(static) calculateDistanceBetweenCircles(circle1, circle2) → {number}

Gets the distance between two circles defined by their centers.

Parameters:
NameTypeDescription
circle1Object

The first circle object.

Properties
NameTypeDescription
xnumber

The x-coordinate of the center of the first circle.

ynumber

The y-coordinate of the center of the first circle.

radiusnumber

The radius of the first circle.

circle2Object

The second circle object.

Properties
NameTypeDescription
xnumber

The x-coordinate of the center of the second circle.

ynumber

The y-coordinate of the center of the second circle.

radiusnumber

The radius of the second circle.

Returns:

The distance between the two circles.

Type: 
number
Examples
const circle1 = { x: 0, y: 0, radius: 5 };
const circle2 = { x: 10, y: 0, radius: 5 };
calculateDistanceBetweenCircles(circle1, circle2); // Output: 0
const circle1 = { x: 0, y: 0, radius: 10 };
const circle2 = { x: 20, y: 0, radius: 5 };
calculateDistanceBetweenCircles(circle1, circle2); // Output: 5

(static) calculateDistanceBetweenCirclesByCoordinates(x1, y1, radius1, x2, y2, radius2) → {number}

Gets the distance between two circles defined by their centers and radii.

Parameters:
NameTypeDescription
x1number

The x-coordinate of the center of the first circle.

y1number

The y-coordinate of the center of the first circle.

radius1number

The radius of the first circle.

x2number

The x-coordinate of the center of the second circle.

y2number

The y-coordinate of the center of the second circle.

radius2number

The radius of the second circle.

Returns:

The distance between the two circles.

Type: 
number
Examples
calculateDistanceBetweenCirclesByCoordinates(0, 0, 5, 10, 0, 5); // Output: 0
calculateDistanceBetweenCirclesByCoordinates(0, 0, 10, 20, 0, 5); // Output: 5

(static) calculateOverlapBetweenRectangles(rect1, rect2) → {number}

Calculates the area of overlap between two rectangles defined by their bounds.

Parameters:
NameTypeDescription
rect1Object

The first rectangle.

Properties
NameTypeDescription
xnumber

The x-coordinate of the top-left corner of the first rectangle.

ynumber

The y-coordinate of the top-left corner of the first rectangle.

widthnumber

The width of the first rectangle.

heightnumber

The height of the first rectangle.

rect2Object

The second rectangle.

Properties
NameTypeDescription
xnumber

The x-coordinate of the top-left corner of the second rectangle.

ynumber

The y-coordinate of the top-left corner of the second rectangle.

widthnumber

The width of the second rectangle.

heightnumber

The height of the second rectangle.

Returns:

The area of overlap between the two rectangles.

Type: 
number
Example
const rect1 = { x: 0, y: 0, width: 10, height: 10 };
const rect2 = { x: 5, y: 5, width: 10, height: 10 };
calculateOverlapBetweenRectangles(rect1, rect2); // Returns 25

(static) calculateOverlapBetweenRectanglesByCoordinates(x1, y1, width1, height1, x2, y2, width2, height2) → {number}

Calculates the area of overlap between two rectangles defined by their x, y, width, and height.

Parameters:
NameTypeDescription
x1number

The x-coordinate of the top-left corner of the first rectangle.

y1number

The y-coordinate of the top-left corner of the first rectangle.

width1number

The width of the first rectangle.

height1number

The height of the first rectangle.

x2number

The x-coordinate of the top-left corner of the second rectangle.

y2number

The y-coordinate of the top-left corner of the second rectangle.

width2number

The width of the second rectangle.

height2number

The height of the second rectangle.

Returns:

The area of overlap between the two rectangles.

Type: 
number
Example
calculateOverlapBetweenRectanglesByCoordinates(0, 0, 10, 10, 5, 5, 10, 10); // Returns 25

(static) getAngleBetweenTwoPoints(x1, y1, x2, y2) → {number}

Calculates the angle between two points in a two-dimensional plane.

Parameters:
NameTypeDescription
x1number

The x-coordinate of the first point.

y1number

The y-coordinate of the first point.

x2number

The x-coordinate of the second point.

y2number

The y-coordinate of the second point.

Returns:

The angle between the two points, in radians.

Type: 
number
Examples
calcAngleBetweenTwoPoints(0, 0, 1, 1); // output: 0.7853981633974483 (approx. 45°)
calcAngleBetweenTwoPoints(0, 0, 0, 1); // output: 1.5707963267948966 (approx. 90°)

(static) topDownCarMovimentation(state) → {Object}

Updates the position, rotation, and speed of a car in a top-down view.

The algorithm is based on https://www.youtube.com/watch?v=Rs_rAxEsAvI.

Parameters:
NameTypeDescription
stateObject

the current state of the car

Properties
NameTypeAttributesDefaultDescription
keysObject

the object containing the input keys

Properties
NameTypeAttributesDefaultDescription
forwardboolean<optional>
false

true if the forward key is pressed

leftboolean<optional>
false

true if the left key is pressed

rightboolean<optional>
false

true if the right key is pressed

reverseboolean<optional>
false

true if the reverse key is pressed

xnumber

the current x position of the car

ynumber

the current y position of the car

speednumber

the current car speed

accelerationnumber

the acceleration value per update

maxSpeednumber<optional>
Infinity

the maximum speed limit of the car

frictionnumber<optional>
0

the friction of the car (0 to 1)

rotationnumber

the current car rotation

rotationSpeednumber

the car rotation speed

boundsObject

the maximum position limits on the axes

Properties
NameTypeDescription
xObject

the maximum x axis position limits

Properties
NameTypeAttributesDefaultDescription
minnumber<optional>
-Infinity

the minimum x axis limit

maxnumber<optional>
Infinity

the maximum x axis limit

yObject

the maximum y axis position limits

Properties
NameTypeAttributesDefaultDescription
minnumber<optional>
-Infinity

the minimum y axis limit

maxnumber<optional>
Infinity

the maximum y axis limit

Returns:
Type: 
Object