Javascript 碰撞检测问题;对某些条件的奇怪反应
如果有人认为这个问题过于具体,请随意删除。 我在碰撞检测方面遇到了问题,我看过一些基本的碰撞检测技术,然后尝试调整它来判断碰撞的是哪一侧,但是当角碰撞或角色在碰撞时具有垂直和水平速度时,检测似乎失败 以下是我的碰撞检测功能:Javascript 碰撞检测问题;对某些条件的奇怪反应,javascript,canvas,2d,collision-detection,rectangles,Javascript,Canvas,2d,Collision Detection,Rectangles,如果有人认为这个问题过于具体,请随意删除。 我在碰撞检测方面遇到了问题,我看过一些基本的碰撞检测技术,然后尝试调整它来判断碰撞的是哪一侧,但是当角碰撞或角色在碰撞时具有垂直和水平速度时,检测似乎失败 以下是我的碰撞检测功能: this.checkCollision = function() { var isGround = false; var i; /*Combines Platforms and Chars so Character Coll
this.checkCollision = function() {
var isGround = false;
var i;
/*Combines Platforms and Chars so Character Collide with Eachother*/
var x = platforms[level];
var y = chars[level];
var p = x.concat(y);
/* For Readability */
var top = this.y;
var bottom = this.y + this.h;
var left = this.x;
var right = this.x + this.w;
for (i = 0; i < p.length; i++) {
/* For Readability */
var pTop = p[i].y;
var pBottom = p[i].y + p[i].h;
var pLeft = p[i].x;
var pRight = p[i].x + p[i].w;
if (p[i] !== this) {
if (this.inverted) {
/*Vertical-Top*/
if ((bottom === pTop) && (right > pLeft && left < pRight)) {
this.vy = -gravity;
/*Vertical-Near-Top*/
} else if ((bottom + this.vy >= pTop && bottom <= pTop) && (right > pLeft && left < pRight)) {
this.vy = pTop - bottom;
/*Vertical-Bottom*/
} else if ((top === pBottom) && (right > pLeft && left < pRight) && (this.vy <= 0)) {
isGround = true;
this.onGround = true;
/*Vertical-Near-Bottom*/
} else if ((top + this.vy <= pBottom && top >= pTop) && (right > pLeft && left < pRight)) {
this.vy = pBottom - top + gravity;
}
} else {
/*Vertical-Top*/
if ((bottom === pTop) && (right > pLeft && left < pRight)) {
if (p[i].bouncy) {
this.vy = -(this.jumpVelocity * 1.2);
} else if (p[i] instanceof Char && !p[i].inverted) {
this.vx = this.current ? this.vx : p[i].vx;
if (p[i].onGround) {
isGround = true;
this.onGround = true;
} else {
this.vy = p[i].vy;
}
} else {
this.vx = this.current ? this.vx : 0;
isGround = true;
this.onGround = true;
}
/*Vertical-Near-Top*/
} else if ((bottom + this.vy >= pTop && bottom <= pTop) && (right > pLeft && left < pRight)) {
this.vy = pTop - bottom - gravity;
/*Vertical-Bottom*/
} else if ((top === pBottom) && (right > pLeft && left < pRight)) {
this.vy = gravity;
/*Vertical-Near-Bottom*/
} else if ((top + this.vy <= pBottom && top >= pTop) && (right > pLeft && left < pRight)) {
this.vy = top - pBottom;
}
}
/*Horizontal-Left*/
if ((right === pLeft) && ((top < pTop && bottom > pTop) || (top < pBottom && bottom >= pBottom) || (top >= pTop && bottom <= pBottom)) && (this.vx > 0)) {
this.vx = 0;
/*Horizontal-Left-Near*/
} else if ((right + this.vx >= pLeft && right <= pRight) && ((top < pTop && bottom > pTop) || (top < pBottom && bottom >= pBottom) || (top >= pTop && bottom <= pBottom)) && (this.vx > 0)) {
this.vx = pLeft - right;
/*Horizontal-Right*/
} else if ((left === pRight) && ((top < pTop && bottom > pTop) || (top < pBottom && bottom >= pBottom) || (top >= pTop && bottom <= pBottom)) && (this.vx < 0)) {
this.vx = 0;
/*Horizontal-Right-Near*/
} else if ((left + this.vx <= pRight && left >= pLeft) && ((top < pTop && bottom > pTop) || (top < pBottom && bottom >= pBottom) || (top >= pTop && bottom <= pBottom)) && (this.vx < 0)) {
this.vx = pRight - left;
}
}
}
if (!isGround) {
this.onGround = false;
}
};
this.checkCollision=function(){
var isGround=假;
var i;
/*结合平台和角色,使角色相互碰撞*/
var x=平台[水平];
变量y=字符[级别];
var p=x.concat(y);
/*为了可读性*/
var top=这个.y;
var bottom=this.y+this.h;
var left=这个.x;
var right=此.x+此.w;
对于(i=0;i左侧和左侧<右侧)){
这个.vy=重力;
/*垂直近顶部*/
}else if((底部+此.vy>=pTop&bottom-pLeft&leftpLeft&&left左侧和左侧<右侧)){
if(p[i].弹性){
this.vy=-(this.jumpVelocity*1.2);
}else if(Char&!p[i]instanceof Char&!p[i].倒置){
this.vx=this.current?this.vx:p[i].vx;
if(p[i].onGround){
isGround=真;
this.onGround=true;
}否则{
this.vy=p[i].vy;
}
}否则{
this.vx=this.current?this.vx:0;
isGround=真;
this.onGround=true;
}
/*垂直近顶部*/
}else if((底部+此.vy>=pTop&bottom-pLeft&left左侧<右侧)){
这个.vy=重力;
/*垂直近底*/
}否则如果((顶部+此.vy=pTop)和&(右侧>左侧<右侧)){
this.vy=top-pbotom;
}
}
/*水平左*/
如果((右===pLeft)和((上pTop)| |(上=pbotom)| |(上>=pTop&&bottom 0)){
该值为0.vx=0;
/*水平左近*/
}如果((右+this.vx>=pLeft和右pTop)| |(顶部=PBOTOM)| |(顶部>=pTop和底部0)){
this.vx=pLeft-右侧;
/*水平右*/
}否则如果((左===pRight)和((上pTop)|(上=pBottom)|(上>=pTop&&bottom pTop)|(上=pBottom)|(上>=pTop&&bottom我也在用画布制作游戏。我还是初学者,但已经为我的在线网络游戏做了一些基础知识。
我觉得这里的碰撞几乎就像我的游戏鼠标一样
Xerror = 2
Yerror = 3
monster_width = 20
monster_height = 20
mouseX = evt.clientX-Xerror //My mouse X position in canvas map
mouseY = evt.clientY-Yerror //My mouse Y position in canvas map
for monster in all_monsters //looping trough all monsters
if mouseX > monster.x_position[monster.id]-monster_width/2+Xerror*4 and mouseX<
monster.x_position[monster.id]+monster_width/2+Xerror*4 and mouseY>
monster.y_position[monster.id]-monster_height/2+Yerror*2 and mouseY<
monster.y_position[monster.id]+monster_height/2+Yerror*4
// then my mouse is over one of the monsters
Xerror=2
耶罗=3
宽度=20
怪物_高度=20
mouseX=evt.clientX-Xerror//My mouse X在画布地图中的位置
mouseY=evt.clientY-Yerror//我的鼠标在画布地图中的Y位置
对于所有怪物中的怪物//在所有怪物中循环
如果mouseX>monster.x_位置[monster.id]-monster_宽度/2+Xerror*4和mouseX<
怪物.x_位置[monster.id]+怪物宽度/2+Xerror*4和鼠标>
怪物.y_位置[monster.id]-怪物高度/2+Yerror*2和鼠标<
怪物。你的位置[怪物。id]+怪物的高度/2+耶罗*4
//然后我的鼠标在其中一个怪物身上
这些X-错误可能是因为画布的小边框,不知道,但如果没有这些,我会得到错误的输出。但是我也会从2减去我的怪物的宽度/高度,以获得中心。(我的怪物只是立方体)
我自己的游戏在gamegame.herokuapp.com上,ID testeris,PWD testeris。如果你想联系我,是magimantas@yahoo.com.我热衷于用canvas、Ruby on rails、Backbone.js、coffeescript制作在线游戏。与其他喜欢用canvas制作游戏的人交流会很有趣:)对于《古墓丽影》,我们有一个连接到面或顶点的球体列表。球体和三角形之间的检测非常简单,因此许多小球几乎可以实现完美碰撞。在移动的物体(所有球)和背景之间,只有旗帜(可攀爬的等等)。
当我在这里的时候,C64天的一个诡计——使用一个2D四方形的房间。然后,你只需要考虑在相邻房间中渲染物体,如果它们是在矩形中扩展四方形到矩形/矩形(简单4x4矩阵),所以我想我不太可能简单地看代码,而不是看游戏中发生的事情。