How to use the matter-js/src/module/main.js.Body function in matter-js

To help you get started, we’ve selected a few matter-js examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mattgrayisok / wulfram-2d / _assets / js / shared / objects / WorldObject.js View on Github external
WorldObject.prototype.setStateByExtrapolation = function(state1, state2, percent){
	Matter.Body.set(this.body, 'position', global.helpers.extrapolateVector(state1.position, state2.position, percent));
	Matter.Body.set(this.body, 'angle', global.helpers.extrapolate(state1.angle, state2.angle, percent));
	//console.log('Extrapolating', global.helpers.interpolateVector(state1.position, state2.position, percent));
	this.updateSpriteFromBody();
}
github mattgrayisok / wulfram-2d / _assets / js / shared / objects / WorldObject.js View on Github external
WorldObject.prototype.setStateByExtrapolation = function(state1, state2, percent){
	Matter.Body.set(this.body, 'position', global.helpers.extrapolateVector(state1.position, state2.position, percent));
	Matter.Body.set(this.body, 'angle', global.helpers.extrapolate(state1.angle, state2.angle, percent));
	//console.log('Extrapolating', global.helpers.interpolateVector(state1.position, state2.position, percent));
	this.updateSpriteFromBody();
}
github mattgrayisok / wulfram-2d / _assets / js / shared / objects / ControllableTank.js View on Github external
_.each(collisions, function(collision, index){
				
				var differenceVector = Matter.Vector.sub(collision.body.position, self.body.position);
				var distance = Matter.Vector.magnitudeSquared(differenceVector);
				if(collisionToUse == false || distance < collisionDistance){
					collisionToUse = collision;
					collisionDistance = distance;
				}

			});

			if(collisionToUse!==false){
				
				var body = collisionToUse.body;
				var otherObject = body.parentObject;
				Matter.Body.applyForce(otherObject.body, self.body.position, 
					Matter.Vector.rotate({ x: 0, y: global.config.playerMainGunHitForce }, self.body.angle));
		
				//Reduce health on other player
				otherObject.reduceHealth(global.config.playerMainGunDamage);
			};

		}
		
		//Reduce energy on this player
		//Apply a small force to this player pushing them backwards
		Matter.Body.applyForce(this.body, this.body.position, 
			Matter.Vector.rotate({ x: 0, y: -global.config.playerMainGunBackwardForce }, this.body.angle));

		//Update sprite to reflect firing
	}
github mattgrayisok / wulfram-2d / _assets / js / shared / objects / ControllableTank.js View on Github external
ControllableTank.prototype.reconcileTowardsAdjustment = function(){
	if(Math.abs(this.incorrectPositionX) > global.config.minimumPositionAdjustmentOffset 
		|| Math.abs(this.incorrectPositionY) > global.config.minimumPositionAdjustmentOffset 
		|| Math.abs(this.incorrectAngle) > global.config.minimumAngleAdjustmentOffset){

		var changeX = this.incorrectPositionX*global.config.serverAdjustmentReducer;
		var changeY = this.incorrectPositionY*global.config.serverAdjustmentReducer;
		var changeAngle = this.incorrectAngle*global.config.serverAdjustmentReducer;

		var newPos = Matter.Vector.sub(this.body.position, {x: changeX, y:changeY});
		var newAngle = this.body.angle - changeAngle;

		Matter.Body.set(this.body, 'position', newPos);
		Matter.Body.set(this.body, 'angle', newAngle);

		this.incorrectPositionX -= changeX;
		this.incorrectPositionY -= changeY;
		this.incorrectAngle -= changeAngle;
	}
}
github mattgrayisok / wulfram-2d / _assets / js / shared / objects / WorldObject.js View on Github external
this.objectId = UUID();
	this.vertices = vertices || [{x:0,y:0},{x:10,y:0},{x:10,y:10},{x:0,y:10}];
	this.startPosition = position || {x:0, y:0};
	this.isStatic = typeof isStatic == 'undefined' ? true : isStatic; 
	this.renderLayer = renderLayer || 'players';
	this.angle = angle || 0;
	this.parentWorld = parentWorld;

	this.incorrectPositionX = 0;
	this.incorrectPositionY = 0;
	this.incorrectAngle = 0;

	this.previousStates = [];

	this.body = Matter.Body.create({ 	
		frictionAir: global.config.playerAtmosphericFriction, 
		restitution: global.config.playerRestitution, 
		density: global.config.playerDensity,
		angle: this.angle,
		vertices: this.vertices,
		position: this.startPosition,
		isStatic: this.isStatic
	});

	this.body.parentObject = this;

	this.sprite = false;

	if(global.isClient){
		var texture = PIXI.Texture.fromImage("assets/images/tank.png");
		this.sprite = new PIXI.Sprite(texture);
github mattgrayisok / wulfram-2d / _assets / js / shared / objects / ControllableTank.js View on Github external
ControllableTank.prototype.reconcileTowardsAdjustment = function(){
	if(Math.abs(this.incorrectPositionX) > global.config.minimumPositionAdjustmentOffset 
		|| Math.abs(this.incorrectPositionY) > global.config.minimumPositionAdjustmentOffset 
		|| Math.abs(this.incorrectAngle) > global.config.minimumAngleAdjustmentOffset){

		var changeX = this.incorrectPositionX*global.config.serverAdjustmentReducer;
		var changeY = this.incorrectPositionY*global.config.serverAdjustmentReducer;
		var changeAngle = this.incorrectAngle*global.config.serverAdjustmentReducer;

		var newPos = Matter.Vector.sub(this.body.position, {x: changeX, y:changeY});
		var newAngle = this.body.angle - changeAngle;

		Matter.Body.set(this.body, 'position', newPos);
		Matter.Body.set(this.body, 'angle', newAngle);

		this.incorrectPositionX -= changeX;
		this.incorrectPositionY -= changeY;
		this.incorrectAngle -= changeAngle;
	}
}