平滑的网格运动

发布于 2024-07-18 02:34:26 字数 269 浏览 7 评论 0原文

我正在使用网格系统开发 Java Bomberman 克隆,但我现在对这个动作并不太满意。 当玩家按下移动键时,角色开始移动(速度为 0.25)。 玩家失去控制,角色继续移动,直到移动完整的方块。 只有当角色到达下一个方块的中心时,玩家才能重新获得控制权。

这使得它太“滞后”。 如果我现在想改变方向,我做不到。

也许我可以使角色的底部小于精灵的大小,这意味着我必须在拐角之前检查以检查它是否是有效的移动。 有什么想法吗?

谢谢!

I'm working on a Java Bomberman clone, using a grid system, and I'm not really satisfied with the movement right now. When the player presses a movement key, the character starts to move (with 0.25 speed). The player loses the control, and the character keeps moving until it has moved a full tile. The player only regains control when the character gets to the center of the next tile.

This makes it's too "laggy". If I want to change direction now, I can't.

Maybe I could make the base of the character smaller than the size of the sprite, meaning I would have to check ahead of the corners to check if it was a valid move. Any thoughts?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

滥情稳全场 2024-07-25 02:34:26

几分钟前我刚刚玩了炸弹人 :)
你可以非常平滑地移动,没有网格计算。
我没怎么用过Java。 我经常使用 Flash。 网格是用来检查碰撞的吗?
它到底是做什么用的?

拥有类似的东西是否有意义(我会尝试画出它的草图):

float vx,vy = 0;//velocity on x and y 
Character bomberman

void keyDownHandler(KeyboardEventSomething event){
 if(key is Left && ! Right ) vx -= .5;
else if(key is Right && !Left ) vx += .5;
//idem for Y axis
}

void keyUpHandler(KeyboardEventSomething event){
vx = vy = 0;
}

void updateLoop(){
  bomberman.x += vx;
  bomberman.y += vy;
}

我可能有点偏离,因为我不确定你是否想克隆炸弹人。 您所描述的网格运动似乎更接近 hopmon

I just played Bomberman a few minutes ago :)
You can move pretty smooth, there no grid calculations.
I haven't used Java that much. I use Flash a lot. Is the Grid for checking collisions ?
What is it for exactly ?

Would it make sense to have something like(I'll try to sketch it):

float vx,vy = 0;//velocity on x and y 
Character bomberman

void keyDownHandler(KeyboardEventSomething event){
 if(key is Left && ! Right ) vx -= .5;
else if(key is Right && !Left ) vx += .5;
//idem for Y axis
}

void keyUpHandler(KeyboardEventSomething event){
vx = vy = 0;
}

void updateLoop(){
  bomberman.x += vx;
  bomberman.y += vy;
}

I might be a bit off, because I'm not sure how much you want to clone Bomberman or not. What you're describing with the grid movement seems closer to hopmon

日久见人心 2024-07-25 02:34:26

不要禁用用户输入。 曾经。 不断地轮询它。

有一个 Tick() 或 OnFrameEnter() 函数执行以下操作:

轮询键盘/操纵杆/其他内容,并缓存用户输入的最后一个方向。

检查角色的位置。

如果角色在网格方块的中心闲置,请将其速度设置为正确的方向并将移动升级一步,除非有墙壁/碰撞物体挡住。 还要清除最后一次按下的某个无效值。

否则,如果角色已经在移动,则更新角色的位置,检查是否与墙壁发生碰撞,或是否接近方向改变节点(交叉点)。 如果它与墙壁相撞,请停止角色。 如果它靠近变向节点并且可以沿缓存方向行驶,则改变方向并清除缓存方向。 否则,如果输入方向与当前方向相反(在走廊中并且玩家想要反转),则反转方向,然后清除按键。 这一切都假设角色应该始终继续移动,直到撞到墙上并停止。 如果您希望角色在任何地方停止,则只需使用反向键即可将其停止到位。

缓存按键允许玩家在角色接近十字路口时抢先改变方向。 这样角色就不必等到停下来才继续前进,玩家也不会感觉游戏没有反应。 不断的轮询让玩家可以随时反转方向,并使十字路口的方向变化看起来更快。

Don't disable user input. Ever. Constantly poll for it.

Have a Tick() or OnFrameEnter() function that does the following:

Poll the keyboard/joystick/whatever, and cache the last direction entered by the user.

Check the position of the character.

If the character is idle in the center of a grid square, set it's velocity in the proper direction and upate the movement one step, unless there's a wall/collision object in the way. Also clear out the last key press to some invalid value.

Else if the character is already moving, update the position of the character, checking for collision with a wall, or proximity to a direction changing node (intersection). If it collides with a wall, stop the character. If it's close to a direction changing node and can travel in the cached direction, change the direction and clear the cached direction. Else if the input direction is opposite the current direction (it's in a hallway and the player wants to reverse), reverse the direction and then clear the key press. This all assumes that the character should continue moving, always, until it hits a wall and stops. If you want the character to stop anywhere, a reverse direction key should simply stop it in place.

Caching the key press allows the player to preemptively change directions as the character approaches an intersection. This way the character doesn't have to wait to stop before moving on, and the player doesn't feel the game is unresponsive. Polling constantly lets the player reverse directions at any time, and makes direction changes at intersections seem faster.

执手闯天涯 2024-07-25 02:34:26

您应该能够摆脱当前的精灵并在需要时准确加载新的精灵 - 即如果角色移动到下一个图块的一半并且玩家按下一个键返回,则取消精灵并开始加载新的精灵为 0.5。

You should be able to get rid of current sprite and load the new one exactly when you need it - i.e. if the character moved half way to the next tile and the player presses a key to return, cancel the sprite and load the new one starting at 0.5.

埋葬我深情 2024-07-25 02:34:26

我想这取决于你希望它如何表现。

假设您正在向上移动,然后单击向右..您希望它停止向上移动并向右移动吗?
当您进行移动时,我会存储之前的网格位置。 这样,如果您单击以移动到其他地方,您可以向后跳并更改移动方向。

实际情况相反...

或者,如果您向上移动并单击向下...您可以将移动速度设置为与 如果不知道你想要它的表现如何,很难回答。

I guess it depends on how you want it to behave.

Say you are moving up, and you click right.. do you want it to stop moving up and go right?
I would store the previous grid location when you make a movement. that way if you click to move elsewhere you can hop backwards and change the movement direction..

Or if you are moving up and you click down... you could set the movement speed to be the opposite of what it is...

It is hard to answer with out knowing what you want it to behave like.

惯饮孤独 2024-07-25 02:34:26

听起来好像您通过更改其包含的网格单元来更新玩家位置(动画的开始)。

为了能够实现所需的效果,精灵渲染位置应该独立于该精灵所代表的实体在网格上的位置。 并允许用户在单元格中心范围内的网格内移动,而无需更改单元格。 如果这个范围没有正确选择,你可能会遇到“doh!我在这个(视觉上)单元格中,但它说我在未来的单元格中”问题,或者“我的头像与墙壁重叠”。

不要更改玩家单元格,直到他处于包含他的单元格的限制/边界的范围内(假设为精灵边界球体的 1/3)。 您还必须修改动画过程以使其可中断。

这与你赋予玩家的自由度无关(例如,它只能向上、向下、向左、向右和对角线移动,而不是 360 度)。

你应该注意的另一件事是,如果精灵比单元格大尺寸。 例如,一个胖怪物,或一个蛇老板,跨越 X 个单元格,但不一定是矩形。

It sounds like you update the players position (start of the animation) by changing the grid cell in which its contained.

To be able to achieve the desired effect, the sprites render position should be independent of where the entity represented by that sprite is located on the grid. And allow the user to move inside that grid within a range of the cell's center without changing cells. If this range is not chosen correctly you may en up with the "doh! im in this (visually) cell but it says im in the future cell" problem, or the "my avatar is overlapping a wall".

Dont change the players cell until he is within a range (lets say 1/3 of the bounding sphere of the sprite) from the limits/borders of the cell that contains him. You would also have to modify the animation process for it to be interruptible.

This is independent of the degrees of liberty you give the player (like, it could only move up, down, left, right, and diagonals vs 360 degrees)

Another thing you should watch out for is, if the sprite is larger than the cell size. For instance, a Fat monster, or a Snake boss which spans across X cells but not necessarily in rectangular form.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文