Sharing a vector class

1 favourites
  • 3 posts
From the Asset Store
25 high-quality cars. This asset is perfect for a side scrolling game, or even a turn based RPG type game.
  • Just thought I would share a simple class for using vectors if you want to use them. It doesn't all functionality it could, but I think it works pretty well in the context of 2D games.

    Also the philosophy of the member functions is that they modify the existing vector where as the static methods don't modify the original and create a new vector.

    class RGVector
    {
    	constructor(x=0, y=0){
    		this.x=x;
    		this.y=y;
    	}
    	
    	add(v1){
    		this.x += v1.x;
    		this.y += v1.y;
    		
    		return this;
    	}
    	
    	addPoints(x, y) {
    		this.x += x;
    		this.y += y;
    		
    		return this;
    	}
    	
    	sub(v1) {
    		this.x -= v1.x;
    		this.y -= v1.y;
    		
    		return this;
    	}
    	
    	subPoints(x,y) {
    		this.x -= x;
    		this.y -= y;
    		
    		return this;
    	}
    	
    	mult(scalar) {
    		this.x *= scalar;
    		this.y *= scalar;
    		
    		return this;
    	}
    	
    	multX(scalar) {
    		this.x *= scalar;
    		
    		return this;
    	}
    	
    	multY(scalar) {
    		this.y *= scalar;
    		
    		return this;
    	}
    	
    	div(scalar) {
    		this.x /= scalar;
    		this.y /= scalar;
    		
    		return this;
    	}
    	
    	divX(scalar) {
    		this.x /= scalar;
    		
    		return this;
    	}
    	
    	divY(scalar) {
    		this.y /= scalar;
    		
    		return this;
    	}
    	
    	mag() {
    		return Math.sqrt((this.x*this.x) + (this.y*this.y));
    	}
    	
    	normalize() {
    		const m = this.mag();
    		
    		if (m>0) {
    			return this.div(m);
    		}
    		else
    		{
    			return this;
    		}
    	}
    	
    	clone() {
    		return new RGVector(this.x, this.y);
    	}
    	
    	limitTo(value) {
    		this.normalize().mult(value);
    		
    		return this;
    	}
    	
    	angleFrom(v1) {
    		return Math.acos(this.dotProduct(v1) / (Math.abs(this.mag()) * 
    				Math.abs(v1.mag())));
    	}
    	
    	dotProduct(v1){
    		return (this.x * v1.x) + (this.y * v1.y);
    	}
    	
    	invertX() {
    		this.x *= -1;
    		
    		return this;
    	}
    	
    	invertY() {
    		this.y *= -1;
    		
    		return this;
    	}
    	
    	invert() {
    		this.invertX();
    		this.invertY();
    		
    		return this;
    	}
    	
    	static Add(v1, v2)
    	{
    		return v1.clone().add(v2);
    	}
    	
    	static AddPoints(v1, x, y)
    	{
    		return v1.clone().addPoints(x,y);
    	}
    	
    	static Sub(v1, v2) {
    		return v1.clone().sub(v2);
    	}
    	
    	static SubPoints(v1, x, y) {
    		return v1.clone().subPoints(x,y);
    	}
    	
    	static Mult(v1, scalar) {
    		return v1.clone().mult(scalar);
    	}
    	
    	static MultX(v1, scalar) {
    		return v1.clone().multX(scalar);
    	}
    	
    	static MultY(v1, scalar) {
    		return v1.clone().multY(scalar);
    	}
    	
    	static Div(v1, scalar) {
    		return v1.clone().div(scalar);
    	}
    	
    	static DivX(v1, scalar) {
    		return v1.clone().divX(scalar);
    	}
    	
    	static DivY(v1, scalar) {
    		return v1.clone().divY(scalar);
    	}
    	
    	static Mag(v1) {
    		return v1.mag();
    	}
    	
    	static Normalize(v1) {
    		return v1.clone().normalize();
    	}
    	
    	static Clone(v1) {
    		return v1.clone();
    	}
    	
    	static LimitTo(v1, value) {
    		return v1.clone().limitTo(value);
    	}
    	
    	static AngleFrom(v1, v2) {
    		return v1.angleFrom(v2);
    	}
    	
    	static InvertX(v1) {
    		return v1.clone().invertX();
    	}
    	
    	static InvertY(v1) {
    		return v1.clone().invertY();
    	}
    	
    	static Invert(v1) {
    		return v1.clone().invertX().invertY();
    	}
    }
    

    Tagged:

  • Quick someone go make a request for vector expressions on the suggestions site.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Nice little library for vectors. Frequently you want to see the vector as text (10.3,104.88) including to the parenthesis to a vector to string would be nice. Also, you should be able to round your vector to integers, calculate a right angle unit vector.

    You should be able to put your vector into an array object and retrieve it from an array object

    in case your function returns the UID of an array.

    Sometimes you want to return a vector from a function and you only get one string argument so you need to be able to send your vector to a string and extract it just the x and just the y or the UID of an array that contains x and y.

    Just some suggestions.

    Nice start.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)