gpt4 book ai didi

java - 模数问题

转载 作者:行者123 更新时间:2023-12-01 13:51:06 25 4
gpt4 key购买 nike

在此,我正在测试 int 是否value 是一个整数或有一个小数值,如果有一个小数,它会慢慢地对该值进行加或减,使其成为一个整数。第一和第三部分有效,但第二和第四部分无效。

if(ax % tileSize != 0) {
ax -= (ax % tileSize) / 6; // works fine
}
if(ax % tileSize != 0) {
ax += (ax % tileSize) / 6; // doesn't work
}
if(ay % tileSize != 0) {
ay -= (ay % tileSize) / 6; // works fine
}
if(ay % tileSize != 0) {
ay += (ay % tileSize) / 6; // doesn't work
}

有效的数量减少了 48 / 6每次,其他的应该增加 48 / 6 ,但似乎每次增加的数量都在变化。

最佳答案

鉴于该作者的评论:

This is just a Java Game, and all I'm testing for is if the Player's x coordinate, (ax), and the y coordinate, (ay), are in line with the tiles, as this is a tile-based game. If they're not in line with the tiles, then the coordinates are increased or decreased so you are put in line with the tiles.

做到这一点的方法如下:

double tileSize = 10;
double ax = 25;
double vectorX = Math.floor(ax/tileSize + 0.5) - ax/tileSize;

这将为您提供一个范围为 -1..1 的 vector ,您可以将其乘以速度或执行任何您想要决定运动的操作。例如:

ax = ax + Math.ceil(vectorX*speed);

y 轴也是如此。另外,请注意我的公式中有 double ,因此如果需要,请应用适当的转换。

关于java - 模数问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19946586/

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