javascript - 如何在 Phaser P2 Body 中应用 "air"摩擦力?

标签 javascript game-physics phaser-framework p2

如何在 Phaser.P2.body 中应用摩擦力? 在基于 Air-Hockey 移相器的游戏中。 如何从曲棍球 table 上“关闭气流”?,

在这个例子中: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'); 

如果 table 开着,这是一个非常好的和现实的例子..但是..如果 table 关着? puc 应该移动得更慢并且应该有更短的停留时间。我想模拟那个。 想象黄色圆圈是冰球,黑色背景是那些充气 table 之一。如何设置puc和table之间的摩擦力?

P2 docs 似乎有很多与 body 边缘和 Material 的碰撞和接触有关的东西......但是如何模拟与“空气”的摩擦?还是“水”,如果这个 body 在游泳……或者与冰球和 table 的摩擦?

附言。试图在 update() 中降低 P2.body.velocity.x 和 y 会促进奇怪的重新路由行为。

最佳答案

您正在寻找 Phaser P2 的 damping property ,它向物理体引入了阻力。

将它添加到您的 body 中,冰球很快就会停下来,就好像空气已被关闭一样:

spriteCircle.body.damping = 0.9;

阻尼指定每秒损失的速度比例,有效值在 01 范围内。

更新了带阻尼的 JSFiddle:http://jsfiddle.net/Lucgmptn/

关于javascript - 如何在 Phaser P2 Body 中应用 "air"摩擦力?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37535012/

相关文章:

javascript - 大背景图像交换

javascript - 在一行上对齐 HTML 选择元素

c# - 胶囊对撞机不统一跳跃

java - 平台游戏碰撞(渗透到地面几个像素)

javascript - 移相器 : how to group polygons in layers?

javascript - 如何在我的 typescript 生成的 js 中调用函数

javascript - 如何 : Dynamically generate CSRF-Token in WTForms with Flask

javascript - 为基本的 React 老虎机创建动画

ios - 如何最好地赋予落球真实世界的重力

javascript - 无法更改 Phaser 3 中的场景