2012-08-16 97 views
3

我有一個受重力支配的玩家實體,並且在屏幕底部有另一個實體可以移動。使實體從ImpactJS中的另一個實體中彈出

當我下降的玩家實體擊中底部的實體時,我希望它反彈離開它。

理想情況下,我想使用玩家實體的.bounciness屬性。

回答

3

您希望您的底部實體包含以下屬性:

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 
} 

此外,如果你願意,你可以處理舵角與碰撞,以及(類似打磚塊遊戲):

如果玩家在中心打,你希望它去的直線上升。如果它擊中右半部分,你希望它右轉;如果它碰到左邊,你想讓它離開。

因此,找到玩家擊中底部實體的位置,並找到相對於實體末端的角度。

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; 
} 
+0

不正是我問,但我不得不懷疑,你所提供的答案很可能是唯一的出路。標記爲正確的。 Upvoted因爲你爲我解決了角度感謝你。 – griegs 2012-08-20 04:51:48

相關問題