Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply "air" friction in a Phaser P2 Body?

How to apply a friction in a Phaser.P2.body? In a Air-Hockey phaser based game. How to "turn-off the air-flow" from 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 on.. but.. if the table is off?? The puc should move slower and should have a shorter stop. I want to simulate that. Being imagining that the yellow circle is the airhockey puck and the black-background is one of those aerated tables. How to set a friction force between puc and table?

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

Ps. trying to degrade P2.body.velocity.x and y in update() promotes weird rerouting behavior.

like image 783
Filipe Tagliacozzi Avatar asked May 31 '16 00:05

Filipe Tagliacozzi


1 Answers

You are looking for Phaser P2's damping property, which introduces drag to a physics body.

Add this to your body and the puck comes to a stop very quickly, as if the air has been turned off:

spriteCircle.body.damping = 0.9;

Damping specifies the proportion of velocity lost each second, and valid values are in the range 0 to 1.

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

like image 112
Maximillian Laumeister Avatar answered Oct 30 '22 13:10

Maximillian Laumeister