-
Notifications
You must be signed in to change notification settings - Fork 0
Physics
New, better, improved physics!
Installation
Angle
Vector
PhysicsBody
shwijs#Angle
You can create angles now! It is pretty self explanatory, but:
To create a new angle, just create a new instance with the value of the angle and the unit with it!
const { Angle } = require("shwi-js");
const myAngle = new Angle(10, "deg");
Allowed units are degrees
or radians
.
Degrees: Alias[deg, degree, degrees]
Radians: Alias[rad, radian, radians] \
Rotate this angle to a new angle! Just pass in another angle instance to rotate!
const { Angle } = require("shwi-js");
const myAngle = new Angle(0, "deg");
const angleToAdd = new Angle(10, "deg");
myAngle.rotate(angleToAdd);
//=> value = 10, unit = deg
Yes, you can also pass in different unit angles, it will automatically convert them.
Get the radian value of the angle. It will convert degrees to radian if the unit of angle is degrees, otherwise it will just return the value. (Because if the angle's unit is radian already, there's no need to convert, is there?)
Get the degre value of the angle. Same as Angle#toRadian
(Static) Get all the units allowed for radian.
(Static) Get all the units allowed for degree.
(Static) Check if a unit is in radian.
(Static) Check if a unit is in degree.
shwijs#Vector
Create a new vector (position).
Create a new vector with positions. (Defaults to Vector.ORIGIN
)
const { Vector } = require("shwi-js");
new Vector(0, 0);
Move this vector by a certain amount. It is the same as adding two vectors.
Set this vector position to something else.
Add another vector to this vector.
const { Vector } = require("shwi-js");
const myVector = new Vector(0, 2);
const vectorToAdd = new Vector(1, 5);
myVector.add(vectorToAdd);
//=> 1, 7
Subtract another vector from this vector. Similar to Vector#add()
Add a resultant to this vector.
const { Vector, Angle } = require("shwi-js");
const myVector = new Vector(0, 0);
myVector.addResultant(5, new Angle(53, "deg"));
//=> myVector = 3, 4
Subtract a resultant from this vector. Similar to Vector#addResultant()
Get the vector positions as an array. [x, y]
Get the resultant value of this vector (without angle).
Get the relative distance of the vector from origin.
const { Vector } = require("shwi-js");
const myVector = new Vector(3, 4);
console.log(myVector.distanceFromOrigin);
//=> 5
If the origin is different, say:
const { Vector } = require("shwi-js");
Vector.SetOrigin(1, 1);
const myVector = new Vector(5, 12);
console.log(myVector.distanceFromOrigin);
//=> 13
Get the absolute position of the vector, no matter what the origin. In above example, its value would be 6, 13
(Static) The origin position. (Of all vectors)
(Static) Check if the given vector components x and y are valid numbers.
const { Vector } = require("shwi-js");
Vector.CheckIfValidPos(0, 0, (err) => {
if (err) return err.log();
// do your thing
});
(Static) Calculate the resultant value of a vector.
(Static) Calculate the components of a resultant vector along with angle.
Returns a vector.
shwijs#PhysicsBody
Create a new physics body with mass (in kilograms) and a position (vector) shwijs#Vector
Default mass is 1 (kg).
Default position is Vector.ORIGIN
Move the physics body by some amount.
const { PhysicsBody, Vector } = require("shwi-js");
const myBody = new PhysicsBody(1, new Vector(1, 0));
myBody.move(new Vector(1, 2));
//=> new position becomes (2, 2)
Move the physics body TO a certain position (vector)
Set the velocity of the body to some velocity vector (vector) for some time in second(s).
const { PhysicsBody, Vector } = require("shwi-js");
const myBody = new PhysicsBody();
myBody.setVelocity(new Vector(1, 1), 2);
//=> 1, 1
//=> 2, 2
//---done---
NOTE: The velocity vector is per second.
Set the mass of the body to a new mass.
Get the current velocity of the body. If there is none, returns a vector with zero values.
Returns the absolute position of the body. The vector origin can be anything, but it will return the absolute from (0, 0)
(Static) Check if a certain mass is valid.
That's it!
Copyright 2021 @Shwibi
Feel free to open an Issue if you find anything wrong in the module! Or open a new Discussion if you have any suggestions, or just want to give a feedback!
The latest STABLE version is v1.0.0
with the addition of Physics* (depracated from v1.1.0^
).
: v1.1.1
Fixed README
: v1.1.0
Released new Physics
: v0.0.5-beta.2
Released something
: v0.0.5-beta
Released, countdown docs
: v0.0.4
Released
: v0.0.4-alpha.6
fix parser
: v0.0.4-alpha.5
npm update
: v0.0.4-alpha.4
(No additions except documentation edits)
: v0.0.4-alpha.2
Added tank, props, and functions.
: v0.0.4-alpha.1
has nothing new in it, it is just fixation of documentation on npm