PixelData CurveFever碰撞

用户名

如此,我正在使用HTML5 Canvas重制CurveFever(http://curvefever.com/),但是,从上一步开始,我遇到了碰撞,但碰撞一直发生:

http://jsbin.com/xenujaja/1/edit

反正有做到,能够通过它在过去的时间步碰撞?

游戏炼金术士

您需要先做好正确测试的准备,否则,您将始终得到真正的答案。

只需使用常规的cos / sin代码即可在当前玩家之前获得位置:

// if dist is the distance ahead  
ahead.x = player.x + dist * Math.cos ( angle ) ;
ahead.y = player.y + dist * Math.sin ( angle ) ;  // !! and not - as you did

无需测试整个区域,一个像素测试就足够了,正如您将在此提琴中看到的那样:http : //jsbin.com/xenujaja/2/edit?js,输出

(左上角的方块现在说没有碰撞/碰撞)

function getCIDCol(pl, ctx) {
    next.x = player.x + Math.cos(player.a) * player.r ;
    next.y = player.y + Math.sin(player.a) * player.r ;

    var id = ctx.getImageData(next.x, next.y ,1,1 );

    return (id.data[3]) ; 
}
var next = {x:0, y:0 }; 

function update() {
    RAF(update);

    if (keys[39]) {
        player.a += player.t
    }
    if (keys[37]) {
        player.a -= player.t
    }

    player.x += Math.cos(player.a) * player.s
    player.y += Math.sin(player.a) * player.s        

    if (player.ht == null) {
        if (Math.floor(Math.random() * 100) == 1) {
            player.ht = setTimeout(function() {
                player.ht = null;
            }, player.h)
        }
    }

  // draw collision status
    if (getCIDCol(player, ctx)) {
        ctx.fillStyle='#F00';
    } else ctx.fillStyle='#0F0';
   ctx.fillRect(4,4, 16,16);

    render()        
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章