- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
您好,当 2 个球对象在 Canvas 上碰撞时,我正在尝试获得响应。
我有一个球对象 ballm 和一个名为 ball 的球对象。
这两个球在 Canvas 上来回弹跳。
如果球碰撞 Canvas 应该说游戏结束。
这是我到目前为止的碰撞代码
function collideWithBall() {
var dx = (ball.x + ball.radius) - (ballm.x + ballm.r);
var dy = (ball.y + ball.radius) - (ballm.y + ballm.r);
var distance = Math.sqrt((dx * dx) + (dy * dy));
if (distance < ball.radius + ballm.r) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = "black";
ctx.font = "18px Arial";
ctx.fillText("Game Over ", 300, 200);
shutdown();
}
}
我唯一的问题是当两个球碰撞时没有任何反应。任何想法都会非常有帮助。
完整代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Canvas</title>
<style type="text/css">
canvas {
border: 1px solid black;
}
</style>
</head>
<body>
<canvas id="tim's-game" width="800px" height="600px"></canvas>
<script type="text/javascript">
// up top. creates the canvas with given dimensions
// declares script type
// Gets a handle to the element with id canvasOne.
var canvas = document.getElementById("tim's-game");
// Get a 2D context for the canvas.
var ctx = canvas.getContext("2d");
// Creates object for the balls
var ball = {
// determines the position where the ball will apear math.random gives a random position on the x ace
position: {x: Math.floor((Math.random() * 780) + 20), y: 10}
// Will determine the size of the ball
, radius: 6
// Will determine the speed of the ball
, velocity: {x: 3, y: 0}
// Will determine if the ball will get faster or slower
, acceleration: {x: 0, y: 0.1}
// Function that draws the ball
,drawBall: function(){
// Collour of the object
ctx.fillStyle = "rgb(25, 100, 100)";
// begins path
ctx.beginPath();
// calls the object it will draw with positions and size
ctx.arc(ball.position.x, ball.position.y, ball.radius, 0, 2 * Math.PI);
// fills the colour
ctx.fill();
// Update the y location.
ball.velocity.y += ball.acceleration.y;
ball.position.x += ball.velocity.x;
ball.position.y += ball.velocity.y;
// Keep the animation going while the ball has not touched the canvas bottom.
// Note there's a bug here.
if ((ball.position.x >= canvas.width - ball.radius) || (ball.position.x <= ball.radius))
ball.velocity.x = -ball.velocity.x;
if ((ball.position.y >= canvas.height - ball.radius) || (ball.position.y <= ball.radius))
ball.velocity.y = -ball.velocity.y;
}
,
}
// Created a second ball object
var ball2 = {
// determines the position where the ball will apear math.random gives a random position on the x ace
position: {x: Math.floor((Math.random() * 780) + 20), y: 10}
// Will determine the size of the ball
, radius: 6
// Will determine the speed of the ball
, velocity: {x: 3, y: 0}
// Will determine if the ball will get faster or slower
, acceleration: {x: 0, y: 0.1}
// Function that draws the ball
,drawBall2: function(){
// Collour of the object
ctx.fillStyle = "rgb(25, 100, 100)";
// begins path
ctx.beginPath();
// calls the object it will draw with positions and size
ctx.arc(ball2.position.x, ball2.position.y, ball2.radius, 0, 2 * Math.PI);
// fills the colour
ctx.fill();
// Update the y location.
ball2.velocity.y += ball2.acceleration.y;
ball2.position.x += ball2.velocity.x;
ball2.position.y += ball2.velocity.y;
// Keep the animation going while the ball has not touched the canvas bottom.
// Note there's a bug here.
if ((ball2.position.x >= canvas.width - ball2.radius) || (ball2.position.x <= ball2.radius))
ball2.velocity.x = -ball2.velocity.x;
if ((ball2.position.y >= canvas.height - ball2.radius) || (ball2.position.y <= ball2.radius))
ball2.velocity.y = -ball2.velocity.y;
}
}
// Creates a new ball object
// This ball will be used for moving around the canvas
var ballm = {
// spawns the ball in the middle of the canvas
position:{ x: canvas.width / 2
, y: canvas.height / 2
}, r: 50
};
// Creates a draw circle function
function drawCircle() {
ctx.fillStyle = "rgb(255, 0, 0)";
ctx.beginPath();
ctx.arc(ballm.position.x, ballm.position.y, ballm.r, 0, 2 * Math.PI);
ctx.fill();
collideWithBall();
}
function collideWithBall() {
var dx = (ball.position.x + ball.radius) - (ballm.position.x + ballm.r);
var dy = (ball.position.y + ball.radius) - (ballm.position.y + ballm.r);
var distance = Math.sqrt((dx * dx) + (dy * dy));
if (distance < ball.radius + ballm.r) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = "black";
ctx.font = "18px Arial";
ctx.fillText("Game Over ", 300, 200);
shutdown();
}
}
// A function to repeat every time the animation loops.
function repeatme() {
collideWithBall();
// clears the screan/canvas i.e. where the ball was previously does not show up.
ctx.clearRect(0, 0, canvas.width, canvas.height);
// calls the function in the ball object
ball.drawBall();
ball2.drawBall2();
//calls the draw circle function
drawCircle();
collideWithBall();
// gets the animation going
window.requestAnimationFrame(repeatme);
}
// Add an event listener to the keypress event.
window.addEventListener("keydown", function(event) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Right
if (event.keyCode == 39 && ballm.position.x < canvas.width - ballm.r)
ballm.position.x += Math.min(10, canvas.width - ballm.position.x - ballm.r);
// Left
else if (event.keyCode == 37 && ballm.position.x > ballm.r)
ballm.position.x -= 10;
// down
else if (event.keyCode == 40 && ballm.position.y < canvas.height - ballm.r)
ballm.position.y += 10;
// For up movement
else if (event.keyCode == 38 && ballm.position.y > ballm.r)
// updates location by int given
ballm.position.y -= 10;
drawCircle();
collideWithBall();
});
// Get the animation going.
window.requestAnimationFrame(repeatme);
</script>
</body>
</html>
编辑:删除了额外的 collideWithBall 函数。
代码中也添加了position
编辑 2:添加了对代码的修复,现在可以正常工作了
最佳答案
我认为你的数学是错误的。你的支票
if (distance < ball.radius + ballm.r) {
是正确的,但暗示距离是从球的中心到中心测量的。因此正确的距离公式是:
var dx = ball.position.x-ballm.x;
var dy = ball.position.y-ballm.y;
var distance = Math.sqrt((dx * dx) + (dy * dy));
如果您按照 if 语句测量中心到中心的距离,则球的半径不会进入计算。
我在您的代码中看到的另一个问题是
collideWithBall
函数被定义了两次!请尝试删除两个看似等效的实现之一。
编辑:另外,您访问的球的位置是错误的。球的位置在 position 属性中。
因此您需要使用它!有关操作方法,请参见上面编辑的代码。
然后工作:http://jsfiddle.net/gQ3hD/2/
Edit2:通过在碰撞函数中遍历两个 baals,更新了 fiddle 以正确处理第二个球。
关于javascript - 球之间的碰撞检测javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33510442/
我正在用 jQuery Collision 编写这个游戏,它使用键盘按键来移动 div,当一个 div 接触另一个 div 时,它应该防止重叠。 我到底该怎么做? HTML ----
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: Java 2D Collision? 嘿,大家好,我有另一篇关于这个问题的帖子刚刚消失了,所以我想我会尝试得到一些关
嘿伙计们,我正在制作一个 2D java 游戏,我正在尝试找出如何制作一个好的碰撞代码。我目前正在使用以下代码: public void checkCollision() { Rect
我的意思是,当我与实体的侧面碰撞并想要跳跃时,我无法向右/向左移动,因为当我与右侧/左侧的实体碰撞时,我有一个标志可以防止这种情况发生,例如所以: 这是我用来检测碰撞的代码: public void
所以我正在运行 collide_mask 检查,以删除与玩家 Sprite 碰撞时的生物实例。它工作得很好。 pygame.sprite.spritecollide(player, mobs, Tru
我正在研究我的砖 block splinter 机,并制作一个适当的碰撞系统,以便使球逻辑地切换方向,我必须检测球与砖 block 的哪一侧碰撞。这是我当前的脚本: int sprite_collid
我做了一个类似颜色切换的游戏。唯一的问题是玩家与每种颜色发生碰撞...... 这是我从github上获取的代码: https://github.com/prometheon/MLNimbleNinja
测试我的游戏,当用户和怪物发生碰撞时,我希望弹出警报但没有成功: function die() { for (var i = 0; i < monster.length; i++) { i
我对 vector 很陌生,这是我第一次真正使用它们进行碰撞检查。这是我的项目,我对如何实现碰撞感到困惑。我目前的碰撞检查和响应代码似乎是……糟糕的设计。 这是我的代码: for(auto it =
我是 javascript 的新手,正在尝试找出如何与球和木板发生碰撞,这将停止游戏并提醒玩家“你输了”。但我只想让红球击中木板,让蓝球不接触地继续前进。这是我正在处理的代码。 (我不介意你是否可以帮
很抱歉提出奇怪的问题,我还是 Android 编程的新手。 我有以下代码: import android.content.DialogInterface.OnClickListener; import
我有 6 个 UIImageView,每个都连接到 UIPanGestureRecognizer,它们都连接到相同的方法。方法是: - (IBAction)handlePan:(UIPanGestur
我想根据某些对象的轴对齐边界框检查视锥体,以粗略检查这些对象是否在视野中。速度不是什么大问题。 最佳答案 我发现构建视锥体的世界空间模型并检查与它的 bbox 碰撞是错误的方法。 一个更简单的方法是以
我项目中的所有这些代码都运行良好,但我遇到了一些问题。当飞机接触到屏幕的边界时,它会在接触后开始旋转。我不知道如何让它在碰到屏幕边界时不旋转。只有在我使用时才会出现这个问题: plane.physic
在应用程序启动时,我在后台线程中删除旧的 CoreData 行,下面是我的代码。我的问题类似于城市街道问题。所以,我有两个实体,Street 和 City,我有一个关系 City > Street,因
我试图不接触穴居人和其他带有碰撞位掩码的图像,但我的穴居人击中了一切。 func addCaveManBitMasks(){ caveManNode.physicsBody?.category
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
如何在 PyGame 中找到字符和图像之间的碰撞?我已经从图像中绘制了一个玩家,并从瓷砖中绘制了墙壁,那么我如何检测这些碰撞? 最佳答案 如果你使用pygame Rect类来表示对象的边界,您可以使用
我正在使用 ftok() 为 C 应用程序使用的共享内存段生成标识符。我有问题,在一个盒子上我与 root 使用的标识符发生冲突。在这种情况下,我可以通过破解代码来修复它,但我想要一个更强大的解决方案
这个问题在这里已经有了答案: JavaScript: Collision detection (10 个回答) 10 个月前关闭。 检测 2 个物体(墙壁)碰撞的好方法。是的,不仅仅是检测,还有进一步
我是一名优秀的程序员,十分优秀!