【问题标题】:Make Entity bounce off another entity in ImpactJS使实体从 ImpactJS 中的另一个实体反弹
【发布时间】:2012-08-12 08:25:45
【问题描述】:

我有一个受重力控制的玩家实体,我在屏幕底部有另一个可以移动的实体。

当我坠落的玩家实体撞到底部的实体时,我希望它反弹。

理想情况下,我想使用玩家实体的 .bounciness 属性。

【问题讨论】:

    标签: javascript impactjs


    【解决方案1】:

    您希望底部实体包含以下属性:

    checkAgainst: ig.Entity.TYPE.A, // player entity type
    collides: ig.Entity.COLLIDES.ACTIVE
    

    然后你希望你的bottomEntity的check()方法在它与它碰撞时反转玩家实体的速度。

    check: function(other) {
      other.vel.y -= 100; // you could also use other.accel.y
    }
    

    此外,如果您愿意,也可以处理带有碰撞的偏转角(类似于 Arkanoid 游戏):

    如果玩家击中中心,您希望它直接向上。如果它击中右半边,你希望它向右;如果它撞到左边,你希望它向左走。

    所以找到玩家击中底部实体的位置,并找到它相对于实体末端的角度。

    var playerPos = player.pos.x - bottomEntity.pos.x;
    var relativePos = ( bottomEntity.size.x - playerPos);
    var angle = relativePos * ( Math.PI / bottomEntity.size.x ); // translate to radians - this finds the number of radians per bottom entity pixel
    

    一旦你得到了角度,就拿它的cos来抓住方向。将方向乘以底部实体的速度,得到底部实体的新速度。

    var newVel = Math.cos( angle ) * bottomEntity.vel.x;
    

    那么您的check() 方法将如下所示:

    check: function(other) {
      var playerPos = other.pos.x - bottomEntity.pos.x;
      var relativePos = ( bottomEntity.size.x - playerPos);
      var angle = relativePos * ( Math.PI / bottomEntity.size.x ); // translate to radians - this finds the number of radians per bottom entity pixel
      other.vel.y -= newVel;
    }
    

    【讨论】:

    • 不完全是我问的,但我怀疑你提供的答案可能是唯一的方法。为此标记为正确。赞成,因为你为我解决了角度问题,谢谢。
    猜你喜欢
    • 2013-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-19
    • 2022-01-09
    • 1970-01-01
    相关资源
    最近更新 更多