XNA 基于图块的碰撞
我有一个 64 x 64 像素图块(10 行,10 列)的图块地图,我想检测移动单元与任何墙壁或物体之间的碰撞。我怎样才能在移动时保持单位在图块地图的中心,然后检测一个单位是否应该改变方向而不很快更新他的位置,并将他从图块的中心扔掉?
示例:如果 TilePositionX = 3 且 TilePosition = 0 处有一个对象,并且该对象使单位改变方向但始终保持居中。如果单位在每次更新时都以 XVelocity = 1.0f(这可以是任何速度)向右朝向对象。我是否必须检测单元的中心位置,然后添加偏移量并检查我是否完全位于图块内?我无法为我的问题找到好的解决方案。
I have a tile map of 64 x 64 pixel tiles (10 rows, 10 columns) I want to detect collision between the moving units and any walls or objects. How can i keep the units centered in the tile map while moving then detect if a unit should change direction without updating his position to soon, and throwing him off the center of the tile?
Example: If there is an object at TilePositionX = 3, and TilePosition = 0 and that object makes a unit change his direction but always stay centered. If the unit is heading right towards the object at a XVelocity = 1.0f (this could be any velocity) every update. Do i have to detect the center position of the unit then add an offset and check if I'm completely inside a tile? I can't of a good solution for my problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在类似情况下所做的就是计算出单位的目标目的地(以像素为单位),这是它正在移动的图块的中心,然后每次更新都会使单位更接近该目标。只有当该装置位于图块的中心时,它才会检查新的目的地。
例如:单位 A 位于坐标 (0,0),以像素 (32,32) 为单位(该图块的中心)
A 向方格 (0,1) 移动,因此新的目的地是 (32,96)。每次更新,单位都会向下移动 (0,1) 个像素。一旦单位是 (32,96),即图块 (0,1) 的中心,它就可以决定下一步要移动到哪里。
What I did in a similar situation was work out the target destination of the unit (in pixels) which was the centre of the tile it was moving toward, and then every update move the unit closer to that target. Only once the unit was in the centre of the tile did it check for a new destination.
For example: Unit A is at coordinates (0,0) which in pixels (32,32) (the centre of that tile)
A moves toward tile (0,1), so new destination is (32,96). Every update the unit moves (0,1) pixels down. Once the unit is a (32,96) which is the centre of the tile (0,1), it can then decide where it is going to move next.
我在一些游戏中处理这个问题的方式是我持有一个图块和一个像素偏移,图块始终保持固定数字 IE 1,2,并且像素偏移根据方向移动负正。当它超过图块的大小时,我们会将其重置为零并添加或减去相应的图块类型。从那里可以很容易地确定您是否碰到了无法移动的方块。
The way i handled this in a few of my games was i held a Tile and a Pixel offset, the Tile always stays a solid number I.E 1,2 and the pixel offset was moved negative positive based on direction. When it got above the size of a tile we would reset it to zero and add or subtract to the corrosponding Tile type. From there it becomes fairly easy to determine if you are hitting a tile that you cannot move on.