Skip to content

Custom pSEngine objects

xam4lor edited this page Jun 2, 2020 · 18 revisions

On this page, you can find a description of every objects handled by pSEngine that makes things easier. Here, we will talk about two classes : Vector and pSPoints.

Vectors

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

Drawing

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

Usual functions

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


Usual math functions

  • v1.add(v2) or v.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) or v.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) multiplies v by a certain amount c (a float or integer value) - (return the vector)
  • v.div(c) divides v by a certain amount c (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 of v1 and v2
  • Vector.sub(v1, v2) creates a new vector as the subtraction of v1 and v2
  • Vector.mult(v1, c) creates a new vector as the multiplication of v1 by a float or int value c
  • Vector.div(v1, c) creates a new vector as the division of v1 by a float or int value c

Advanced math Vector operations

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) or v.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) or v.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 of v (return the vector)

  • v.rotate(angle) rotates v on 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 of v rotated by an angle (in radians)



The pSPoints class

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.


Back to wiki home - Next page on how to write TeX and Text Strings

Clone this wiki locally