Skip to content

Custom pSEngine objects

xam4lor edited this page Jun 10, 2020 · 18 revisions

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.

Vectors

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}');

Drawing

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);

Usual functions

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) or v1.equals(x, y, z): returns true if 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

Usual math functions

  • v1.add(v2) or v.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) - (returns v1 modified)
  • v1.sub(v2) or v.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) - (returns v1 modified)
  • v.mult(c) multiplies each coordinate of v by a certain amount c (a float or integer value) - (returns v1 modified)
  • v.div(c) divides each coordinate of v by a certain amount c (a float or integer value, different from 0) - (returns v1 modified)

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 of v1 and v2
  • Vector.sub(v1, v2): returns the resulting vector from the subtraction of v1 and v2
  • Vector.mult(v1, c): returns the resulting vector from the multiplication of the coordinates of v1 by a float or int value c
  • Vector.div(v1, c): returns the resulting vector from the division of the coordinates of v1 by a float or int value c

Advanced vector operations

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) or v.dot(x, y, z): returns the dot product of v1 and v2 or v1 and (x, y, z) (if not specified, x, y and z are set to 0)
  • v1.cross(v2) or v.cross(x, y, z): returns the cross product of v1 and v2 or v1 and (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 between min and max (that is, if the vector's magnitude is greater than max, then it is set to max, and if it is less than min, it is set to min)
  • v.mag(): returns the vector's magnitude
  • v.setMag(mag): returns the vector with magnitude set to mag
  • v.rotate(angle): returns the vector rotated by angle (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 of v1 and v2
  • Vector.cross(v1, v2): returns the cross product of v1 and v2
  • Vector.normalize(v): returns a copy of the normalized vector
  • Vector.rotate(v, angle): returns a copy of v rotated by the angle angle (in radians)



The pSPoints class

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)

Clone this wiki locally