-
Notifications
You must be signed in to change notification settings - Fork 4
Custom pSEngine objects
On this page, you can find a description of each one of the objects handled by pSEngine that makes things easier. Here, we will talk about two classes : Vector and pSPoints.
pSEngine implements an easy for you to create and manage 2D Vectors (3D is currently not supported for each functions). You can create a 2D Vector by running the following command :
// pointing 2 meters to the right (X axis) and 3 meters to the top (Y axis) (and 0 on the Z axis)
// Vector will be drawn in red (default color is white) and displays his name (r)
let v = new Vector(2, 3, 0, 'red', '\\vec{r}');To draw a vector to the screen, just use the following command inside your draw object loop :
// Draw the v Vector to the screen (from (0, 0) to (2, 3) meters)
v.draw();
// Draw the v Vector, starting at (3, 3) meters and ending at (3+2, 3+3)=(5, 6) meters
v.draw(new Vector(3, 3));
// Draw the v Vector with a head size of 10 pixels (default 5 px)
// and with a stroke weight of 2 pixels (default 1 px)
v.draw(new Vector(0, 0), 10, 2);Vector allows you to use the following commands :
-
v.set(x, y, z)to change the Vector coordinates (return the vector) -
v.setName(name)to change the name (return the vector) -
v.copy()to get a copy of the Vector (warning : these function only copies coordinates) -
v1.equals(v2)orv1.equals(x, y, z)returntrueif the coordinates between the two parameters are the same -
v.clear()return this Vector with coordinates set to (0, 0, 0) -
v.toString()return A String representation of the object
-
v1.add(v2)orv.add(x, y, z)add one Vector to another or add an X, Y, and Z value to the vector (if not specified, Y or Z = 0) - (return the vector) -
v1.sub(v2)orv.sub(x, y, z)subtract one Vector to another or subtract X, Y, Z value to the vector (if not specified, Y, Z = 0) - (return the vector) -
v.mult(c)multipliesvby a certain amountc(a float or integer value) - (return the vector) -
v.div(c)dividesvby a certain amountc(a float or integer value, not equal to 0) - (return the vector)
Please note that every function is also accessible as static methods. So, v1 and v2 will not be modified, and a new Vector will be created and returned.
-
Vector.add(v1, v2)creates a new vector as the sum ofv1andv2 -
Vector.sub(v1, v2)creates a new vector as the subtraction ofv1andv2 -
Vector.mult(v1, c)creates a new vector as the multiplication ofv1by a float or int valuec -
Vector.div(v1, c)creates a new vector as the division ofv1by a float or int valuec
Some advanced mathematical operations are available (Warning : some of them are 3D functions, so you we'll need 3D Vectors for that).
-
v1.dot(v2)orv.dot(x, y, z)return the dot product from a Vector to another or with an X, Y, and Z value (if not specified, X, Y or Z = 0) -
v1.cross(v2)orv.cross(x, y, z)return the cross product from a Vector to another or with an X, Y, and Z value (if not specified, X, Y or Z = 0) -
v.normalize()normalize a Vector (return the vector) -
v.limit(min, max)limit a Vector magnitude bewteen two values (return the vector) -
v.mag()return the magnitude of the Vector -
v.setMag(mag)sets the magnitude ofv(return the vector) -
v.rotate(angle)rotatesvon the XY axis by an angle (in radians) - (return the vector) -
v.getAngle()return the angle between this vector and the origin
As in the previous category, there is also a few static Vector commands, to add or replace functionnalities with static Vectors.
-
Vector.dist(v1, v2)return the distance between two vectors -
Vector.dot(v1, v2)return the dot product between v1 and v2 -
Vector.cross(v1, v2)return the cross product between v1 and v2 -
Vector.normalize(v)return a copy of the normalized vector -
Vector.rotate(v, angle)return a copy ofvrotated by an angle (in radians)
pSPoints are usually used when you need to draw real points on the screen with a certain name and you don't want to handle the drawing and position of the point. To create a pSPoint, run the following code :
// Set x and y point position, and color (default is white)
// pointName is the name of the point (none by default) and has a size of pointSize (default 6)
// vectorName is the name of the point (none by default),
let point = new pSPoint(x, y, color, pointName, pointSize, vectorName, drawOriginVector);Then, insert the points.draw() method inside your objects draw function.
Previous (Drawing on the screen) - Back to wiki home - Next (How to write TeX and text strings)
pSEngine - Made by MecanicaScience - Official Website : https://pSEngine.mecanicascience.fr/
If you want to help on this wiki and you don't have access to it, feel free to open a new issue or comment one, while describing which page you would like to edit, and what modifications you want to do.