C++如果位块传输到特定表面,则发生 SDL 碰撞
我有一个简单的游戏,其中包括 2 个将精灵传输到其上的表面。第一个是我将角色要跳过的小方块,第二个是我将角色精灵复制到的表面。我想进行碰撞检测,它只检测我的角色不在表面上的物体(如果这有意义的话)。所以本质上把它想象成两层,第一层有障碍,第二层有我的性格。我希望它只检测第一层上的障碍物精灵。有办法做到这一点吗?
I have a simple game that includes 2 surfaces that I blit sprites onto. One I blit little squares which your character is supposed to jump over, the second is the surface I blit my characters sprite onto. I want to make a collision detection where it only detects objects that are on the surface my character is NOT on if that makes sense. So essentially think of it like 2 layers, the first one has obstacles, the second has my character. I want it to only detect the sprites as obstacles on the first layer. Is there a way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想到的一种方法如下:
首先,将每层的对象分开,例如
std::vectorCharLayerObjects;
(std::vector
用于当该平面上有更多对象时,例如环境内容)和std::vectorCollisionLayerObjects;
。然后只需针对CollisionLayerObjects
向量中的对象测试角色即可。当然,这仅在两个表面具有相同尺寸的情况下才有效,但我认为这是可以预料的。One approach that comes to mind is the following:
First, seperate the objects per layer, say
std::vector<MyObjects*> CharLayerObjects;
(std::vector
for when you have more objects on that plane, like environment stuff) andstd::vector<MyObjects*> CollisionLayerObjects;
. Then just test the character against the objects in theCollisionLayerObjects
vector. Of course, this only works if both surfaces have the same dimensions, but I think that's to be expected.通常,碰撞检测是对抽象几何体而不是像素数据执行的,因此 SDL 不提供碰撞检测功能。如果您使用抽象表示(例如,正方形或圆形),那么您在何处位图传输曲面并不会真正对碰撞产生任何影响 - 您可以比较所需的任何对象,而不管哪些曲面已被位图传输到其他曲面。
Normally collision detection is performed on abstract geometry rather than pixel data, and as such SDL doesn't provide collision detection functionality. If you use the abstract representation (eg, a square or a circle) then where you blit your surfaces doesn't really make any difference to the collisions - you can compare whichever objects you want regardless of which surfaces have been blitted to other surfaces.