//外接矩形-事前检测
ballWillHitledge: function(ledge){
var ballRight = ball.left+ball.width,
ledgeRight = ledgt.left + ledge.width,
ballbottom = +ball.height,
nextBallBottomEstimate = ballbottom + ball.velocity/fps,
return ballRight>ledgt.left && ball.left<ledgeRight && ballBottom< && nextBallBottomEstimate>
}
//外接圆形的判断要比矩形精确一些,其算法也相对简单,外接圆是用两直径相加小于两点间最小距离就判断发生碰撞了
isBallInBucket: function() {
var ballCenter = { x: ball.left + BALL_RADIUS,
y: + BALL_RADIUS
},
distance = Math.sqrt(
Math.pow(bucketHitCenter.x - ballCenter.x, 2) +
Math.pow(bucketHitCenter.y - ballCenter.y, 2));
return distance < BALL_RADIUS + bucketHitRadius;
},
//下面是用外接距形做的一个弹跳小球的例子
var canvas = document.getElementById('canvas'),
context = canvas.getContext('2d'),
ball = new Sprite('ball',
{
paint: function (sprite, context) {
context.save();
context.strokeStyle = 'blue';
context.fillStyle = 'yellow';
context.beginPath();
context.arc(sprite.left + sprite.width/2,
sprite.top + sprite.height/2,
10, 0, Math.PI*2, false);
context.stroke();
context.fill();
context.restore();
}
}),
ballMoving = false,
lastTime = undefined,
velocityX = 350,
velocityY = 190,
showInstructions = true;
// Functions.....................................................
function windowToCanvas(e) {
var x = e.x || e.clientX,
y = e.y || e.clientY,
bbox = canvas.getBoundingClientRect();
return { x: x - bbox.left * (canvas.width / bbox.width),
y: y - * (canvas.height / bbox.height)
};
};
function getBoundingBox(ball) {
return { left: ball.left,
top: ,
width: ball.width,
height: ball.height
};
}
function handleEdgeCollisions() {
var bbox = getBoundingBox(ball),
right = bbox.left + bbox.width,
bottom = + bbox.height;
if (right > canvas.width || bbox.left < 0) {
velocityX = -velocityX;
if (right > canvas.width) {
ball.left -= right-canvas.width;
}
if (bbox.left < 0) {
ball.left -= bbox.left;
}
}
if (bottom > canvas.height || < 0) {
velocityY = -velocityY;
if (bottom > canvas.height) {
-= bottom-canvas.height;
}
if ( < 0) {
-= ;
}
}
};
function detectCollisions() {
if (ballMoving) {
handleEdgeCollisions();
}
};
function isPointInBall(x, y) {
return x > ball.left && x < ball.left + ball.width &&
y > && y < + ball.height;
}
// Event Handlers................................................
canvas.onmousedown = function (e) {
var location = windowToCanvas(e);
ballMoving = !ballMoving;
if (showInstructions)
showInstructions = false;
};
// Animation.....................................................
function animate(time) {
var elapsedTime;
if (lastTime === 0) {
lastTime = time;
}
else {
context.clearRect(0,0,canvas.width,canvas.height);
if (ballMoving) {
elapsedTime = parseFloat(time - lastTime) / 1000;
ball.left += velocityX * elapsedTime;
+= velocityY * elapsedTime;
detectCollisions();
}
lastTime = time;
ball.paint(context);
if (showInstructions) {
context.fillStyle = 'rgba(100, 140, 230, 0.7)';
context.font = '24px Arial';
context.fillText('Click anywhere to start or stop the ball', 20, 40);
}
}
window.requestNextAnimationFrame(animate);
};
// Initialization................................................
ball.fillStyle = 'rgba(255,255,0,1.0)';
ball.left = 100;
= 100;
context.shadowColor = 'rgba(100,140,255,0.5)';
context.shadowBlur = 4;
context.shadowOffsetX = 2;
context.shadowOffsetY = 2;
context.font = '38px Arial';
window.requestNextAnimationFrame(animate);外接图形法-矩形&圆形36
原创
©著作权归作者所有:来自51CTO博客作者生而为人我很遗憾的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
opencv 对称图形最小外接矩形 opencv图像矩
1. 定义大小为M×N M × N 的数字图像f(x,y) f ( x , y
opencv 对称图形最小外接矩形 图像的矩 C++ Opencv i++ -
openCV 画外接矩形 opencv最小外接矩形的角度
实验中使用到最小外接矩阵角度的内容,写博客记录。本篇主要参考了如下四个博客: &n
openCV 画外接矩形 opencv cv 旋转角度 顺时针 -
对vue生命周期的理解
例如 create d:()=>this.fetchTodos() )wp:heading。
#vue.js #前端 #javascript 生命周期 数据 -
基于Spring Boot + Vue 3的乡村振兴综合服务平台架构设计与实现
的架构设计与实现方案,重点分析前后端分离架构、微服务设计模式以及关键模块的代码实现。随着乡村振兴战略的深入推进,数字化技术正成为推动农村发展的重要引擎。
#spring boot #vue.js #后端 Vue 代码分析
















