继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

canvas 碰撞检测 javascript

西兰花伟大炮
关注TA
已关注
手记 4
粉丝 88
获赞 107

var ball = {x:600,y:100,r:20,g:1,vx:4,vy:0};

function collision(){
ball.x += ball.vx;
ball.y += ball.vy;
ball.vy += ball.g;
if(ball.y + ball.r >= canvasHeight)
{
ball.y = canvasHeight - ball.r;
ball.vy = -ball.vy
}
if(ball.y -ball.r <=0)
{
ball.y = ball.r;
ball.vy = -ball.vy;
}
if(ball.x - ball.r <= 0)
{
ball.x = ball.r;
ball.vx = -ball.vx;
}
if(ball.x + ball.r >=canvasWidth)
{
ball.x = canvasWidth - ball.r;
ball.vx = -ball.vx;
}}

canvas.width = canvasWidth;
canvas.height = canvasHeight;

打开App,阅读手记
3人推荐
发表评论
随时随地看视频慕课网APP