2012-08-16 98 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

相关问题