我正在制作一款平台游戏,但我的碰撞检测有问题。我制作了一个在屏幕/ map 上绘制图 block 的功能。在那个函数中是我的碰撞检测,当只绘制一个瓷砖时它工作正常,但是当我用三个瓷砖创建“楼梯”时,第一个瓷砖不能正常工作。玩家只是被“推”在瓷砖上。侧面检测不起作用。其他磁贴工作正常。
下面是碰撞检测和瓷砖绘制的代码:
//Function that allows you to draw a tile
function drawTile(type, x, y, collision){
var tileImg = new Image();
tileImg.onload = function(){
ctx.drawImage(tileImg, x, y)
};
tileImg.src = "images/" + type + ".png";
if (collision){
//Worst collision detection ever.
if((player_x + player_width == x) && (player_y + player_height > y)){
canMoveRight = false;
}else if((player_x == x + 32) && (player_y + player_height > y)){
canMoveLeft = false;
}else if((player_y + player_height > y) && (player_x + player_width >= x) && (player_x + player_width <= x + 64)){
player_y = y - player_height;
}else{
canMoveRight = true;
canMoveLeft = true;
}
}
}
//Draw the map
function drawMap(){
drawTile("block", 96, 208, true);
drawTile("block", 128, 208, true);
drawTile("block", 128, 176, true);
};
如您所见,碰撞检测代码有点糟糕。因此,如果您能向我展示更好的制作方法,那就太好了。
如果你需要知道什么,就说吧。 :)
最佳答案
我知道这对您来说听起来很可怕,但您应该考虑使用场景图来处理所有碰撞检测、绘制甚至点击屏幕上的事件。
场景图基本上是一种树状数据结构,表示 1-parent 到 n-child 关系(注意:每个 HTML 页面的 DOM 也是一个场景图)
因此,要解决这个问题,您将有一个称为“节点”或其他名称的基本接口(interface)或抽象类,代表您的 sceneGraph 中的每个节点都必须实现的接口(interface)。同样,它就像 dom 中的元素一样,它们都具有 CSS 属性、事件处理方法和位置修饰符。
节点:
{
children: [],
update: function() {
for(var i = 0; i < this.children.length; i++) {
this.children[i].update();
}
},
draw: function() {
for(var i = 0; i < this.children.length; i++) {
this.children[i].draw();
}
}
}
现在,你可能知道,如果你在 DOM 中移动一个元素,每个元素,位置或任何其他元素,所有子元素都会自动转到新位置及其父元素,这种行为是通过转换继承实现的,因为为简单起见,我不会展示 3D 变换矩阵,而只是展示一个平移(x、y 偏移量)。
节点:
{
children: [],
translation: new Translation(),
update: function(worldTranslation) {
worldTranslation.addTranslation(this.translation);
for(var i = 0; i < this.children.length; i++) {
this.children[i].update(worldTranslation);
}
worldTranslation.removeTranslation(this.translation);
},
draw: function() {
for(var i = 0; i < this.children.length; i++) {
this.children[i].draw();
}
}
}
转换:
{
x: 0,
y: 0,
addTranslation: function(translation) {
this.x += translation.x;
this.y += translation.y;
},
removeTranslation: function(translation) {
this.x -= translation.x;
this.y -= translation.y;
}
}
(请注意,我们不会实例化新的翻译对象,因为在全局翻译中添加/删除值会更便宜)
现在,您的 worldTranslation(或 globalTranslation)具有节点可以从其父节点继承的所有偏移量。
在开始碰撞检测之前,我将展示如何使用此技术绘制 Sprite 。 基本上,您将使用位置图像对填充绘制循环中的数组
节点:
{
children: [],
translation: new Translation(),
image: null, //assume this value is a valid htmlImage or htmlCanvas element, ready to be drawn onto a canvas
screenPosition: null, //assume this is an object with x and y values like {x: 0, y: 0}
update: function(worldTranslation) {
worldTranslation.addTranslation(this.translation);
this.screenPosition = {x: worldTranslation.x, y: worldTranslation.y};
for(var i = 0; i < this.children.length; i++) {
this.children[i].update(worldTranslation);
}
worldTranslation.removeTranslation(this.translation);
},
draw: function(spriteBatch) {
spriteBatch.push({
x: this.screenPosition.x,
y: this.screenPosition.y,
});
for(var i = 0; i < this.children.length; i++) {
this.children[i].draw(spriteBatch);
}
}
}
渲染函数:
function drawScene(rootNode, context) {
//clear context here
var spriteBatch = [];
rootNode.draw(spriteBatch);
//if you have to, do sorting according to position.x, position.y or some z-value you can set in the draw function
for(var i = 0; i < spriteBatch.length; i++) {
var sprite = spriteBatch[i];
context.drawImage(sprite.image, sprite.position.x, sprite.position.y);
}
}
现在我们对从 sceneGraph 绘制图像有了基本的了解,我们开始进行碰撞检测:
首先我们需要我们的节点有边界框:
盒子:
{
x: 0,
y: 0,
width: 0,
height: 0,
collides: function(box) {
return !(
((this.y + this.height) < (box.y)) ||
(this.y > (box.y + box.height)) ||
((this.x + this.width) < box.x) ||
(this.x > (box.x + box.width))
);
}
}
在 2D 游戏中,我更喜欢边界框不在其所在节点的坐标系中,而是让它知道其绝对 值。 通过 CSS 解释: 在 css 中你可以设置边距和填充,这些值不依赖于页面,而是它们只存在于设置了这些值的元素的“坐标系”中。所以它就像有 position: absolute 和 left: 10px vs margin-left: 10px; 2D 的好处是我们不需要一直冒泡 sceneGraph 来寻找检测,我们也不必从当前坐标系中计算框 -> 进入世界坐标系 -> 返回每个节点进行碰撞检测。
节点:
{
children: [],
translation: new Translation(),
image: null,
screenPosition: null,
box: null, //the boundry box
update: function(worldTranslation) {
worldTranslation.addTranslation(this.translation);
this.screenPosition = {x: worldTranslation.x, y: worldTranslation.y};
this.box.x = worldTranslation.x;
this.box.y = worldTranslation.y;
this.box.width = this.image.width;
this.box.height = this.image.height;
for(var i = 0; i < this.children.length; i++) {
this.children[i].update(worldTranslation);
}
worldTranslation.removeTranslation(this.translation);
},
collide: function(box, collisions) {
if(this.box.collides(box)) {
collisions.push(this);
}
//we will optimize this later, in normal sceneGraphs a boundry box asures that it contains ALL children
//so we only will go further down the tree if this node collides with the box
for(var i = 0; i < this.children.length; i++) {
this.children[i].collide(box, collisions);
}
},
draw: function(spriteBatch) {
spriteBatch.push({
x: this.screenPosition.x,
y: this.screenPosition.y,
image: this.image,
});
for(var i = 0; i < this.children.length; i++) {
this.children[i].draw(spriteBatch);
}
}
}
碰撞函数:
function collideScene(rootNode, box) {
var hits = [];
rootNode.collide(box, hits);
for(var i = 0; i < hits.length; i++) {
var hit = hits[i];
//your code for every hit
}
}
注意: 并非每个节点都必须代表一个图像,您可以创建节点以仅向其子节点添加一个翻译,例如制作一个没有视觉效果的 DIV 容器来排列一堆对象,而只需要编辑一个对象的位置。一个字符可以包括:
CharacterRoot (Translation Only)
CharacterSprite (Image)
Weapon (Image)
Shield (Image)
现在这些只是游戏中使用的场景管理的一些基础知识,您可以谷歌我在这里使用的许多热量,进行一些进一步的优化,并随时提出任何问题。
关于javascript - HTML5 Canvas 中的碰撞检测。优化太,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13099845/