-
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 useful for your simulations. We will talk about two classes : Vector and pSPoints.
pSEngine implements easy to create and manipulate 2D vectors (3D isn't currently supported for all functions). You can create a 2D vector with the following code :
// 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 write the following 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);The Vectorclass implements the following functions :
-
v.set(x, y, z): changes the vector coordinates (returns the vector) -
v.setName(name): changes the vector's name (returns the vector) -
v.copy(): returns a copy of the vector coordinates -
v1.equals(v2)orv1.equals(x, y, z): returnstrueif the coordinates of the two operands are the same -
v.clear(): returns the same vector with coordinates set to(0, 0, 0) -
v.toString(): returns a string representation of the object
-
v1.add(v2)orv.add(x, y, z): add one vector to another or add(x, y, z)to the vector (if not specified, x, y and z are set to 0) - (returnsv1modified) -
v1.sub(v2)orv.sub(x, y, z): substract one vector to another or substract(x, y, z)to the vector (if not specified, x, y and z are set to 0) - (returnsv1modified) -
v.mult(c)multiplies each coordinate ofvby a certain amountc(a float or integer value) - (returnsv1modified) -
v.div(c)divides each coordinate ofvby a certain amountc(a float or integer value, different from 0) - (returnsv1modified)
Please note that each function is also accessible as static methods. You can use them to return a vector without modifying the objects called.
-
Vector.add(v1, v2): returns the resulting vector from the sum ofv1andv2 -
Vector.sub(v1, v2): returns the resulting vector from the subtraction ofv1andv2 -
Vector.mult(v1, c): returns the resulting vector from the multiplication of the coordinates ofv1by a float or int valuec -
Vector.div(v1, c): returns the resulting vector from the division of the coordinates 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 to use them).
-
v1.dot(v2)orv.dot(x, y, z): returns the dot product ofv1andv2orv1and(x, y, z)(if not specified, x, y and z are set to 0) -
v1.cross(v2)orv.cross(x, y, z): returns the cross product ofv1andv2orv1and(x, y, z)(if not specified, x, y, and z are set to 0) -
v.normalize(): returns the normalized vector -
v.limit(min, max): returns the vector with a magnitude limited betweenminandmax(that is, if the vector's magnitude is greater thanmax, then it is set tomax, and if it is less thanmin, it is set tomin) -
v.mag(): returns the vector's magnitude -
v.setMag(mag): returns the vector with magnitude set tomag -
v.rotate(angle): returns the vector rotated byangle(in radians) on the XY axis -
v.getAngle(): returns the angle between this vector and the origin
As before, there is also a few static vector functions:
-
Vector.dist(v1, v2): returns the distance between two vectors -
Vector.dot(v1, v2): returns the dot product ofv1andv2 -
Vector.cross(v1, v2): returns the cross product ofv1andv2 -
Vector.normalize(v): returns a copy of the normalized vector -
Vector.rotate(v, angle): returns a copy ofvrotated by the angleangle(in radians)
pSPoints are generally 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. The points will be drawn with the size pointSize at position (x, y), the color color and name pointName. If vectorName is defined and drawOriginVector set to true, then a vector from the origin to the point will be drawn, with vectorName text attached to it.
Previous (Drawing on the screen) - Back to wiki home - Next (How to write TeX and text content)
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.