我有一个由重力控制的播放器实体,我在屏幕底部有另一个实体可以四处移动。
当我的下落的玩家实体击中底部的实体时,我希望它从它身上弹出来。
理想情况下,我希望使用播放器实体的.bounciness属性。
发布于 2012-08-20 12:36:02
您希望底部实体包含以下属性:
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;
}https://stackoverflow.com/questions/11979517
复制相似问题