C++精灵之间的 SDL 冲突
C++ SDL collision between sprites
经过30分钟的谷歌搜索,我能找到的就是这个:http://www.sdltutorials.com/sdl-collision
我认为标题具有误导性,然后我注意到这只是检测两个精灵之间碰撞的方法的噩梦。我想要的只是检查我的玩家精灵何时触摸其他东西(另一个精灵)。我怎样才能做到这一点?
我读到有一个名为 SDL_collision.h 的库,但它要么是 Pascal 的,要么是空的。
您很有可能会使用SDL_Rect
作为边界框。其中x
和y
是精灵的位置,w
和h
是精灵的宽度和高度。然后您需要做的就是使用 SDL_HasIntersection
.
下面是一个简单的示例:
SDL_Surface *Srfc1, *Srfc2;
Srfc1= IMG_Load("foo.png");
Srfc2= Srfc1;
Srfc2->refcount++;
SDL_Rect box1, box2;
box1.x = box1.y = 0;
box2.x = box2.y = 100;
box1.w = box2.w = Srfc1->w;
box2.h = box2.h = Srfc1->h;
// ... somewhere in your event handling logic
if (SDL_HasIntersection(&box1, &box2))
{
printf("Collision.");
}
// ...
SDL_FreeSurface(Srfc1);
SDL_FreeSurface(Srfc2);
<小时 />由于您没有SDL_HasIntersection
,这里有一个适合您需求的快速小函数:
bool IntersectRect(const SDL_Rect * r1, const SDL_Rect * r2)
{
return !(r2->x > (r1->x + r1->w) ||
(r2->x + r2->w) < r1->x ||
r2->y > (r1->y + r1->h) ||
(r2->y + r2->h) < r1->y);
);
}
作为参考,逻辑为:
return !(r2.left > r1.right ||
r2.right < r1.left ||
r2.top > r1.bottom ||
r2.bottom < r1.top);
其中"右"和"底部"分别指"x + 宽度"和"y + 高度"。使用它来修复该功能,以防我打错字。
相关文章:
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- 写入位置0x0000000C时发生访问冲突
- Mix_Init和Mix_OpenAudio SDL之间的区别是什么
- SDL 窗口不会弹出
- GL_SHADERSTORAGE_BUFFER位置是否与其他着色器位置冲突
- 使用cmake从源代码构建MySQL连接器/C++失败(与以前的声明冲突)
- 引发异常:读取访问冲突**dynamicArray**为0x1118235.发生
- C++LinkedList问题.数据类型之间存在冲突?没有匹配的构造函数
- 链表中写入访问冲突的未知原因
- C++中的openCV Mat访问冲突
- 如何使 std::sort 在 std::swap 和我的命名空间的模板化交换之间没有名称冲突?
- SDL 操纵杆按钮按下/C++
- (SDL 渲染问题)C++ 引发异常:读取访问冲突.这是无效的
- C++精灵之间的 SDL 冲突
- SDL 和 C++ 中的冲突
- visual c++和SDL-访问冲突错误
- c++程序和SDL中的main()冲突
- opengl / glsl / sdl代码中的c++访问冲突
- C++ "File" .exe 中 SDL 0x6C7DB2AA (SDL2.dll) 处未处理的异常: 0xC0000005:访问冲突读取位置0x00000044
- 视觉C++ 将SDL_ttf与 SDL 和 SDL2 一起使用时出现访问冲突