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

1

5

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.

Gazpacho answered 31/5, 2016 at 0:7 Comment(1)
Note that it's called drag and it usually is a counteractive force. You might have to roll your own modifier for it. Note that drag usually goes with the square of the velocity, so if the velocity is relatively small then the drag will be low. Also, if the drag is constant then it will only reduce the velocity a constant amount and would be the same as simply slowing it down. So, you should consider if it even matters if you simulation of real physics.Priapic
I
7

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/

Infant answered 31/5, 2016 at 0:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.