完美像素碰撞并翻倍
我正在使用实体
和实体
;它们都是矩形
,我可以调整实体
的速度,例如将速度设置为 2.3
。复杂的问题是 tiles
与实体
之间的冲突。
I am working with tiles
and entities
; they are both rectangles
, and I can adjust the speed of the entities
, such as setting the speed as 2.3
. The problem that complicates is when it comes to collision between the tiles
and the entities
.
我用于碰撞
的方法实际上是在检查下一个图块
的当前速度
,因此如果速度
为 2.3
,但是距离 tile
和 entity
之间的值是 1.4
,它将不会不要让它移动那微小的差异,它将在两者之间留下最小的像素。这不是一件好事。我一直在尝试解决这个问题的方法,以补充微小的差异,但是我只是想不通。
My method for collision
actually adds on to check the next tile
from its current speed
, so if the speed
is 2.3
, but the distance between the tile
and entity
is 1.4
, it won't let it move that tiny difference, and it will leave the tiniest pixel between the two. This isn't a very good thing to have. I have been trying to work ways around this to add on that tiny difference, but I just cannot figure it out..
这是我的碰撞
方法(在 Java
中):
Here is my collision
method (in Java
):
public boolean collision(double xa, double ya) {
for (int c = 0; c < 4; c++) {
int xt = (int) ((this.x + xa) + c % 2 * 15 - 7) >> 4; // (n >> 4) is the same as (n / 16), but faster, if you didn't know.
int yt = (int) ((this.y + ya) + c / 2 * 15 - 8) >> 4;
if (level.getTile(xt, yt).isSolid()) return true;
}
return false;
}
这是我目前用于处理所有运动的方法
And here is my current method that I am using to handle all of the movement of the entity, where the collision is checked:
public void move(double xa, double ya) {
if (xa != 0 && ya != 0) {
move(xa, 0);
move(0, ya);
return;
}
if (collision(xa, ya)) { //---
return; // CONFIGURE DIFFERENCE HERE
} //---
while (xa != 0) {
if (Math.abs(xa) > 1) {
this.x += intValue(xa);
xa -= intValue(xa);
} else {
this.x += xa;
xa = 0;
}
}
while (ya != 0) {
if (Math.abs(ya) > 1) {
this.y += intValue(ya);
ya -= intValue(ya);
} else {
this.y += ya;
ya = 0;
}
}
}
我要合并的是在我放置消息的注释块中找到
。考虑到这可能是一个简单的算法,我可能会失败得很严重,但是我只是想不出找到区别的正确方法。每隔一段时间,我会发现自己离墙一像素。这是我一直试图以某种方式工作的一种方法。 2.3
和 1.4
之间的区别在移动
方法中在此处配置差异
What I want to incorporate is to find the difference between the 2.3
and 1.4
within that comment block where I put the message CONFIGURE DIFFERENCE HERE
in the move
method. I am probably failing pretty badly, considering this is probably an easy algorithm, but I just cannot think of the right way to find the difference. Every once and a while, I will find myself one pixel off from the wall. Here is a method that I have been trying to get to work in some way.
if (collision(xa, ya)) {
while (Math.abs(xa) > 1) {
xa -= intValue(xa); //intValue(int v) returns 1 if v > 0
// returns -1 if v < 0
}
while (Math.abs(ya) > 1) {
ya -= intValue(ya);
}
if (collision(xa, ya)) return;
}
摘要&问题
我有一个问题,如果实体的速度
是大于
距平铺
的距离
,它将离开本身和 tile
之间至少有一个 pixel
,我真的不喜欢这样。我可以使用哪种算法找到实体
和 tile
之间的最小差异?
I have a problem where if the speed
of an entity isgreater than
the distance
from the tile
it is going into, it will leave at least a pixel
in between itself and the tile
, and I really don't like this. What kind of algorithm could I use that will find the tiniest difference between the entity
and the tile
?
如果您需要任何其他信息,我会很乐意添加。
If you need any additional information, I will be glad to add it.
尽管不完全是我在GameDev上问的问题,答案可以在这里使用我得到的:给您的方块移动校正向量。
Although not exactly the question I asked on GameDev, the answer that I received could be used here: Give your tiles movement correction vectors.
当玩家移动到方块上时,将标准化的玩家向量添加到标准化的方块向量中, (重新归一化)向量是玩家实际应该移动的方向。这样可以帮助我绕过角落的角色,但可以帮助您让角色真正接触到墙壁。
When a player moves onto a tile add the normalized player vector to the normalized tile vector and the resulting (re-normalized) vector is the direction the player should actually move. This helped me budge characters around corners but will help you allow a character to actually touch the wall.
如果其他所有方法均失败,则如果速度大于间隙,则仅移动玩家的差距量。在您的示例中,不要移动播放器2.3,也不要移动1.4。
If all else fails, if the speed is bigger than the gap then only move the the player the amount of the gap. In your example, don't move the player 2.3, move them 1.4.