落砂模拟碰撞检测C++和SFML
Falling sand simulation collision detection C++ and SFML
我真的需要一些帮助。我正在尝试做一个落沙模拟,并编写一些基本代码,但我不知道如何进行碰撞检测。这是我到目前为止的代码:
//SFML Include
#include <SFML/Graphics.hpp>
//Include Vectors
#include <vector>
//Main Loop
int main()
{
//Window Init
sf::RenderWindow window(sf::VideoMode(720, 480, 32), "Yip Yip Physics", sf::Style::Default);
//Global Envoirmental Variables
static float gravity = 0.25;
static float frict = 3;
//Particle Structures
struct Particle
{
//Shape
sf::RectangleShape rect;
//Cell Position
sf::Vector2f cell_pos;
//Vel and frict
float slide_vel = 3;
float grv_vel = 0;
//Init Particle (size, color and origin)
void init()
{
rect.setSize(sf::Vector2f(3, 3));
rect.setFillColor(sf::Color::Green);
rect.setOrigin(rect.getSize());
}
};
//Particle Setup
std::vector<Particle> particles;
//Window Loop
while (window.isOpen())
{
//Update
if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left))
{
//Make and Position Particle
Particle part;
part.rect.setPosition(sf::Vector2f(sf::Mouse::getPosition(window)));
//Initalize the Particle and add to vector
part.init();
particles.push_back(part);
}
//Pixel Update
for (auto& p : particles)
{
//Draw in window
window.draw(p.rect);
}
//Display
window.display();
//Event Variable
sf::Event event;
//Window Event System
while (window.pollEvent(event))
{
//Close Window
if (event.type == sf::Event::Closed)
{
window.close();
}
}
//Window Clear
window.clear(sf::Color::Black);
}
//Return 0
return 0;
}
代码的工作原理是制作粒子,将它们放在向量中,并将它们绘制到屏幕上,但我不知道该去哪里进行碰撞。我不知道该在哪里找,我真的很感激在哪里找到它的帮助或指导。我听过一些关于细胞自动机的知识,但不知道如何做,也不知道这是否是最好的选择。
落沙模拟通常不是这样做的!它们与细胞自动机非常相似。通常,您可以通过将世界存储在2d阵列中来实现它们,并通过在帧缓冲区上绘制像素来绘制粒子
这可以防止模拟的性能下降(而不是O(n^2(,得到O(n((,还可以提高渲染性能(而不是0(n(,得到了O(1((闪电传输帧缓冲区是一种恒定时间操作((
只需在谷歌上搜索主题,你就可以获得很多有用的信息!
相关文章:
- SFML纹理像播放器
- 如何使用AngelScript注册SFML Vector2运算符
- 如何修复sfml c++代码编译错误
- SFML library: http request
- 落砂模拟碰撞检测C++和SFML
- 使用 SFML 和 C++ 将 Pixel 打印到屏幕上
- SFML RenderWindow打开窗口需要很长时间
- 矢量下标超出SFML游戏中的范围
- 获取 SFML 窗口的 HWND 和高可用性?
- 将 Vulkan 与 SFML 结合使用?
- SFML 文本未绘制在窗口上
- SFML 碰撞永远不会在我的系统中注册
- 使用对象数组对 SFML 进行动画处理
- (SFML)按下键时,播放器构造函数未使用正确的动画进行更新
- 如何使用 SFML 在贪吃蛇游戏中定义游戏结束?
- 如何通过多类"Union variable" (sfml) 使用轮询事件
- 1d.exe找不到SFML文件?
- 当我尝试通过 mingw 使用 CMake 和 SFML 库编译项目时出错
- Declaration of IntRect SFML
- SFML:错误:")"标记之前的预期主表达式