gpt4 book ai didi

javascript - 需要 javascript 中的碰撞脚本帮助

转载 作者:行者123 更新时间:2023-11-28 16:26:04 24 4
gpt4 key购买 nike

好吧,所以我只是尝试通过手动将所有内容写入数组来减少代码行数。我的问题是他现在传送并且重力不起作用...首先,我有一个单元对象网格,基本上是一个 32x32 网格“640X480”。这些对象都被传递到一个数组中,如下所示-

var gridcellarray = [750];
gridcellarray[0] = cell0;
gridcellarray[1] = cell1;
gridcellarray[2] = cell2;
and so on for 750 32x32 cells...

现在对于碰撞脚本我有这个......

function collisioncheck(obj) {
obj = obj;
for(var i = 0; i < gridcellarray.length; i++){
//really long if statement// sorry...
if ((gridcellarray[i].solid == true) && ((obj.PosY >= gridcellarray[i].y - obj.maskImg.height) && !(obj.PosY >= gridcellarray[i].y ) && !((obj.PosX > gridcellarray[i].x + solidOriginX + solidImg.width/2-5) || (obj.PosX < gridcellarray[i].x - solidOriginX - solidImg.width/2)))){
if(obj.onground == 0){
obj.PosY = gridcellarray[i].y - obj.maskImg.height;
obj.VelY = 0;
obj.onground = 1;
obj.jump = 0;
}
}
else if (obj.PosY >= canvas.height - obj.maskImg.height){
if(obj.onground == 0){
obj.VelY = 0;
obj.onground = 1;
obj.jump = 0;
obj.PosY = canvas.height - obj.maskImg.height;
}
}
else {
obj.VelY += 1;
obj.onground = 0;
}
}
}

现在这段代码在之前工作得很好如果我为每个单元格手动复制了 750 次问题是,现在我已经将它们作为数组进行了一次迭代,它会变得困惑并将我传送到底部,如果我尝试跳跃,即使不在下方或不在 block 上,它也会将我传送回底部。

//编辑
我认为所有变量都应该通过名称解释其用途,但如果您有任何问题,请询问。

//编辑
所有变量都应用您检查碰撞的对象,例如碰撞检查(玩家)这里是一个正在运行的代码演示的链接... Zack Bloom 的代码就在其中,它非常适用于未发布的水平碰撞脚本,但垂直方向上,它不会重置并确认您站在一个 block 上,即 ongroud = true; Demo link

//编辑
好吧,现在扎克指出将 y 和 x 值重置为有很大帮助,但他仍然无法跳跃,因为地面变量在检测到碰撞时不想重置......哦,传送是由于我的向上脚本-

  //Upwards Collision//     
if ((cell.solid == true) && ((obj.PosY >= cell.y - 32) && !(obj.PosY > cell.y+32) && !((obj.PosX > cell.x + solidOriginX + solidImg.width/2-5) || (obj.PosX < cell.x - solidOriginX - solidImg.width/2)))){
if (obj.onground == 0){
obj.VelY += 1;
obj.onground = 0;
obj.PosY = cell.y + obj.maskImg.height-13;
}
}

关于如何解决上面的这个困惑有什么想法吗?阻止他传送?它只是为了检查碰撞 mask (红色矩形)的顶部是否接触到 block ,就像试图跳过它一样,但它的目的是阻止这种情况发生,这样你就会撞到头并掉下来。提前致谢!

最佳答案

else if/else 实际上根本不属于循环,它们不会评估正在循环的单元格,但每次调用碰撞检查时都会触发多次。

function collisioncheck(obj) {
for(var i = 0; i < gridcellarray.length; i++){
var cell = gridcellarray[i];

if (cell.solid && ((obj.PosY >= cell.y - obj.maskImg.height) && !(obj.PosY >= cell.y ) && !((obj.PosX > cell.x + solidOriginX + solidImg.width/2-5) || (obj.PosX < cell.x - solidOriginX - solidImg.width/2)))){
if(!obj.onground){
obj.PosY = cell.x - obj.maskImg.height;
obj.VelY = 0;
obj.onground = 1;
obj.jump = 0;

break;
}
}
}
if (obj.PosY >= canvas.height - obj.maskImg.height){
if(!obj.onground){
obj.VelY = 0;
obj.onground = 1;
obj.jump = 0;
obj.PosY = canvas.height - obj.maskImg.height;
}
} else {
obj.VelY += 1;
obj.onground = 0;
}
}

但更好的方法是根据每个网格单元所在的位置来存储每个网格单元,因此您只需查找船舶附近的网格单元即可。

// You only have to do this once to build the structure, don't do it every time you
// need to check a collision.
var gridcells = {};
for (var i=0; i < gridcellarray.length; i++){
var cell = gridcellarray[i];

var row_num = Math.floor(cell.PosX / 32);
var col_num = Math.floor(cell.PosY / 32);

if (!gridcells[row_num])
gridcells[row_num] = {};

gridcells[row_num][col_num] = cell;
}

// Then to check a collision:
function collisioncheck(obj){
// I'm not sure exactly what your variables mean, so confirm that this will equal
// the width of the object:
var obj_width = solidImg.width;
var obj_height = obj.maskImg.height;

var collided = false;

var left_col = Math.floor(obj.PosX / 32),
right_col = Math.floor((obj.PosX + obj_width) / 32),
top_row = Math.floor(obj.PosY / 32),
bottom_row = Math.floor((obj.PosY + obj_height) / 32);

for (var row=top_row; row <= bottom_row; row++){
for (var col=left_col; col <= right_col; col++){
var cell = gridcells[row][col];

if (cell.solid){
collided = true;

if (row == top_row){
// We collided into something above us

obj.VelY = 0;
obj.PosY = cell.PosY + 32;
} else if (row == bottom_row && !obj.onground){
// We collided into the ground

obj.PosY = cell.x - obj_height;
obj.VelY = 0;
obj.onground = 1;
obj.jump = 0;
}

if (col == left_col){
// We collided left

obj.VelX = 0;
obj.PosX = cell.PosX + 32;
} else if (col == right_col){
// We collided right

obj.VelX = 0;
obj.PosX = cell.PosX - obj_width;
}
}
}
}

if (obj.PosY >= canvas.height - obj_height){
if (!obj.onground){
obj.VelY = 0;
obj.onground = 1;
obj.jump = 0;
obj.PosY = canvas.height - obj_height;
}
}

if (!collided){
obj.VelY += 1;
obj.onground = 0;
}
}

我们只查看我们知道重叠的单元格,而不是每帧循环 720 个单元格。这比所有位置计算更高效且更容易阅读。

关于javascript - 需要 javascript 中的碰撞脚本帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7974513/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com