【发布时间】:2017-04-05 03:37:12
【问题描述】:
我现在有点坚持我的基本体素物理学。它非常非常不稳定,我很确定我的数学在某个地方被打破了,但让我们看看你要说什么:
// SOMEWHERE AT CLASS LEVEL (so not being reinstantiated every frame, but persisted instead!)
glm::vec3 oldPos;
// ACTUAL IMPL
glm::vec3 distanceToGravityCenter =
this->entity->getPosition() -
((this->entity->getPosition() - gravityCenter) * 0.005d); // TODO multiply by time
if (!entity->grounded) {
glm::vec3 entityPosition = entity->getPosition();
if (getBlock(floorf(entityPosition.x), floorf(entityPosition.y), floorf(entityPosition.z))) {
glm::vec3 dir = entityPosition - oldPos; // Actually no need to normalize as we check for lesser, bigger or equal to 0
std::cout << "falling dir: " << glm::to_string(dir) << std::endl;
// Calculate offset (where to put after hit)
int x = dir.x;
int y = dir.y;
int z = dir.z;
if (dir.x >= 0) {
x = -1;
} else if (dir.x < 0) {
x = 1;
}
if (dir.y >= 0) {
y = -1;
} else if (dir.y < 0) {
y = 1;
}
if (dir.z >= 0) {
z = -1;
} else if (dir.z < 0) {
z = 1;
}
glm::vec3 newPos = oldPos + glm::vec3(x, y, z);
this->entity->setPosition(newPos);
entity->grounded = true; // If some update happens, grounded needs to be changed
} else {
oldPos = entity->getPosition();
this->entity->setPosition(distanceToGravityCenter);
}
}
基本想法是确定实体从哪个方向撞击表面,然后将其定位为一个“单元”回到那个方向。但显然我做错了什么,因为这总是会将实体移回它的来源点,有效地将其保持在生成点。
这可能会容易得多,我想多了。
【问题讨论】:
-
if (dir.x >= 0) { x = -1; } if (dir.x < 0) { x = 1; }- 这对我来说似乎毫无用处......x最终将永远是1。你的意思是else if? edit这会导致你描述的行为,所以我会回答。 -
@CompuChip 当然,一个愚蠢的错误消失了,还有多少其他人潜伏在那里?但这不是唯一的问题,它仍然非常非常不稳定。
标签: c++ collision-detection voxel