物体卡在地板上
Object getting stuck in the floor
好的,我正在可汗学院使用 PJS 制作拖放程序,这是我的代码:
/* Just a simple drag and drop program */
// TODO:
// * Add realistic bouncing
// * Add particles when bouncing
// Changeable variables
var testShapeWidth = 45;
var testShapeHeight = 45;
var testShapeX = 200;
var testShapeY = 200;
var gravityStrength = 0.8;
// Unchangeable variables
var onGround = false;
var mouseDown = false;
var round2 = function (num) {
num = num / 2;
num = round(num);
num *= 2;
return num;
};
var checkIfClicked = function(x, y, w, h) {
var mx = mouseX;
var my = mouseY;
var rightSide = x + w;
var bottomSide = y + h;
if (mx > x && mx < rightSide && my > y && my < bottomSide) {
return true;
}
return false;
};
var draw = function() {
background(255, 255, 255);
fill(0);
rect(testShapeX, testShapeY, testShapeWidth, testShapeHeight);
testShapeY = round2(testShapeY);
// Check if on the ground or not and update the onGround variable
if (testShapeY + testShapeHeight < 400) { // If the y coorordinate of the bottom edge of the rectangle is less than 400...
onGround = false; // The rectangle is not on the ground
} else if (testShapeY + testShapeHeight >= 400) { // If the y coorordinate of the bottom edge of the rectangle is greater than or equal to 400...
gravityStrength = round2(gravityStrength * -1) / 2;
}
if (!onGround && !mouseDown) {
testShapeY += gravityStrength;
gravityStrength += 0.2;
}
if (mouseDown) {
testShapeX = mouseX - testShapeWidth / 2;
testShapeY = mouseY - testShapeHeight / 2;
gravityStrength = 0;
}
};
var mousePressed = function() {
if (checkIfClicked(testShapeX, testShapeY, testShapeWidth, testShapeHeight)) {
mouseDown = true;
}
};
var mouseReleased = function() {
mouseDown = false;
};
我只是通过替换这些行来添加弹跳:
else if (testShapeY + testShapeHeight >= 400) { // If the y coorordinate of the bottom edge of the rectangle is greater than or equal to 400...
testShapeY = 400 - testShapeHeight;
gravityStrength = 0;
}
有了这个:
else if (testShapeY + testShapeHeight >= 400) { // If the y coorordinate of the bottom edge of the rectangle is greater than or equal to 400...
gravityStrength = round2(gravityStrength * -1) / 2;
}
但现在矩形有时会卡在地板上。这是一个例子:
一开始我以为是偶数和奇数有关,所以加了round2函数(四舍五入到最近的2)没有用。我希望 Whosebug 社区可以帮助我解决这个问题,但我不能。你会发现 link 到我的项目 here.
你是个幸运的人,因为我不必筛选所有这些代码就知道问题是什么(否则我今晚会帮助其他人,真的,这些代码太多了)。你的问题是数学。您的解决方案很简单。
问题
事情是这样的:当物体低到足以穿过地板时,您反转并除以 2 gravityStrength
。这正是出错的地方。
如果对象落在地平面下的像素多于 gravityStrength / 2
,则它不能再次上升,因为添加新的 gravityStrength
时它的位置仍会在地下。然后它将恢复它的方向并再次减半 gravityStrength
,确保它不会再从这个位置移动(除非你用手移动它)。它肯定卡住了。
解决方案
改变这个:
} else if (testShapeY + testShapeHeight >= 400) { // If the y coorordinate of the bottom edge of the rectangle is greater than or equal to 400...
gravityStrength = round2(gravityStrength * -1) / 2;
}
为此:
} else if (testShapeY + testShapeHeight >= 400) { // If the y coorordinate of the bottom edge of the rectangle is greater than or equal to 400...
testShapeY = 400; // now any speed will bounce. Lower this number to make sure that the object will "get stuck" after a while if otherwise it bounces forever
gravityStrength = round2(gravityStrength * -1) / 2;
}
这里的想法是您的算法将保持相同的工作方式,但不会让物体下降到低到 gravityStrength
无法使它弹回的程度。或者,您可以将 testShapeY = 400;
下移一行并将 testShapeY
变量基于新的 gravityStrength
。那就太好了。
玩得开心!
好的,我正在可汗学院使用 PJS 制作拖放程序,这是我的代码:
/* Just a simple drag and drop program */
// TODO:
// * Add realistic bouncing
// * Add particles when bouncing
// Changeable variables
var testShapeWidth = 45;
var testShapeHeight = 45;
var testShapeX = 200;
var testShapeY = 200;
var gravityStrength = 0.8;
// Unchangeable variables
var onGround = false;
var mouseDown = false;
var round2 = function (num) {
num = num / 2;
num = round(num);
num *= 2;
return num;
};
var checkIfClicked = function(x, y, w, h) {
var mx = mouseX;
var my = mouseY;
var rightSide = x + w;
var bottomSide = y + h;
if (mx > x && mx < rightSide && my > y && my < bottomSide) {
return true;
}
return false;
};
var draw = function() {
background(255, 255, 255);
fill(0);
rect(testShapeX, testShapeY, testShapeWidth, testShapeHeight);
testShapeY = round2(testShapeY);
// Check if on the ground or not and update the onGround variable
if (testShapeY + testShapeHeight < 400) { // If the y coorordinate of the bottom edge of the rectangle is less than 400...
onGround = false; // The rectangle is not on the ground
} else if (testShapeY + testShapeHeight >= 400) { // If the y coorordinate of the bottom edge of the rectangle is greater than or equal to 400...
gravityStrength = round2(gravityStrength * -1) / 2;
}
if (!onGround && !mouseDown) {
testShapeY += gravityStrength;
gravityStrength += 0.2;
}
if (mouseDown) {
testShapeX = mouseX - testShapeWidth / 2;
testShapeY = mouseY - testShapeHeight / 2;
gravityStrength = 0;
}
};
var mousePressed = function() {
if (checkIfClicked(testShapeX, testShapeY, testShapeWidth, testShapeHeight)) {
mouseDown = true;
}
};
var mouseReleased = function() {
mouseDown = false;
};
我只是通过替换这些行来添加弹跳:
else if (testShapeY + testShapeHeight >= 400) { // If the y coorordinate of the bottom edge of the rectangle is greater than or equal to 400...
testShapeY = 400 - testShapeHeight;
gravityStrength = 0;
}
有了这个:
else if (testShapeY + testShapeHeight >= 400) { // If the y coorordinate of the bottom edge of the rectangle is greater than or equal to 400...
gravityStrength = round2(gravityStrength * -1) / 2;
}
但现在矩形有时会卡在地板上。这是一个例子:
一开始我以为是偶数和奇数有关,所以加了round2函数(四舍五入到最近的2)没有用。我希望 Whosebug 社区可以帮助我解决这个问题,但我不能。你会发现 link 到我的项目 here.
你是个幸运的人,因为我不必筛选所有这些代码就知道问题是什么(否则我今晚会帮助其他人,真的,这些代码太多了)。你的问题是数学。您的解决方案很简单。
问题
事情是这样的:当物体低到足以穿过地板时,您反转并除以 2 gravityStrength
。这正是出错的地方。
如果对象落在地平面下的像素多于 gravityStrength / 2
,则它不能再次上升,因为添加新的 gravityStrength
时它的位置仍会在地下。然后它将恢复它的方向并再次减半 gravityStrength
,确保它不会再从这个位置移动(除非你用手移动它)。它肯定卡住了。
解决方案
改变这个:
} else if (testShapeY + testShapeHeight >= 400) { // If the y coorordinate of the bottom edge of the rectangle is greater than or equal to 400...
gravityStrength = round2(gravityStrength * -1) / 2;
}
为此:
} else if (testShapeY + testShapeHeight >= 400) { // If the y coorordinate of the bottom edge of the rectangle is greater than or equal to 400...
testShapeY = 400; // now any speed will bounce. Lower this number to make sure that the object will "get stuck" after a while if otherwise it bounces forever
gravityStrength = round2(gravityStrength * -1) / 2;
}
这里的想法是您的算法将保持相同的工作方式,但不会让物体下降到低到 gravityStrength
无法使它弹回的程度。或者,您可以将 testShapeY = 400;
下移一行并将 testShapeY
变量基于新的 gravityStrength
。那就太好了。
玩得开心!