How to apply air friction in the Phaser P2?

How to apply friction in Phaser.P2.body? In the game Air-Hockey phaser. How to "turn off the airflow" from the hockey table ?,

In this example: http://jsfiddle.net/ywzmkso3/32/

// Initialize Phaser, and creates a 400x490px game var game = new Phaser.Game(400, 400, Phaser.CANVAS, 'game_div'); var game_state = {}; // Creates a new 'main' state that wil contain the game game_state.main = function() { }; game_state.main.prototype = { preload: function() { // Function called first to load all the assets }, create: function() { game.physics.startSystem(Phaser.Physics.P2JS); game.physics.p2.restitution = 0.7; //start drawing a circle var graphics = game.add.graphics(0, 0); graphics.beginFill(0xFF3300); graphics.lineStyle(0); graphics.beginFill(0xFFFF0B); graphics.drawCircle(100, 100, 40); graphics.endFill(); //creating an sprite from draw var spriteCircle = game.add.sprite(100, 300, graphics.generateTexture()); // And destroy the original graphics object graphics.destroy(); spriteCircle.anchor.set(0.5); game.physics.p2.enable([ spriteCircle ], false); spriteCircle.body.setCircle(20);// 20 radius spriteCircle.body.mass = 1; spriteCircle.body.debug = true; //give some initial velocity spriteCircle.body.velocity.x = 10000 spriteCircle.body.velocity.y = 19999 }, update: function() { }, }; // Add and start the 'main' state to start the game game.state.add('main', game_state.main); game.state.start('main'); 

This is a very good and realistic example if the table is turned on .. but .. if the table is turned off ?? The bunch should move slower and should have a shorter stop. I want to imitate this. Imagining that the yellow circle is an air hockey puck and the black background is one of those aerated tables. How to set the friction force between puc and table?

P2 documents seem to have a lot of things related to collisions and contacts with body edges and Materials ... but how to simulate friction with "air"? or "water" if this body is floating .. or friction with a puck and a table?

Ps. trying to degrade P2.body.velocity.x and y in update () contributes to a weird route change.

+5
source share
1 answer

You are looking for a Phaser P2 damping property that introduces drag and drop into the physical body.

Add this to your body and the puck will stop very quickly, as if the air were off:

 spriteCircle.body.damping = 0.9; 

Damping determines the fraction of the speed lost every second, and the acceptable values ​​are in the range from 0 to 1 .

Updated JSFiddle with attenuation: http://jsfiddle.net/Lucgmptn/

+6
source

All Articles