【问题标题】:How to apply "air" friction in a Phaser P2 Body?如何在 Phaser P2 Body 中应用“空气”摩擦?
【发布时间】:2016-09-28 20:02:49
【问题描述】:

如何在 Phaser.P2.body 中应用摩擦力? 在基于空气曲棍球相位器的游戏中。 如何从曲棍球台“关闭气流”?,

在本例中: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'); 

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

P2 文档似乎有很多与身体和材料边缘的碰撞和接触有关的东西......但是如何模拟与“空气”的摩擦?或者如果这个身体在游泳的话是“水”……或者是与冰球和桌子的摩擦?

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

【问题讨论】:

  • 请注意,它被称为阻力,通常是一种反作用力。您可能必须为它滚动自己的修饰符。请注意,阻力通常与速度的平方成正比,因此如果速度相对较小,则阻力会很小。此外,如果阻力是恒定的,那么它只会将速度降低一个恒定的量,并且与简单地减慢速度相同。因此,您应该考虑模拟真实物理是否重要。

标签: javascript game-physics phaser-framework p2


【解决方案1】:

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

把它加到你的身体上,冰球很快就停了下来,就好像空气被关掉了一样:

spriteCircle.body.damping = 0.9;

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-28
    • 2016-09-05
    相关资源
    最近更新 更多