所以,基本上我正在尝试使用SFML在C++中实现基本重力模拟器,这是可行的,但我遇到了碰撞问题。有一个行星(引力源)-不可移动的球,它没有任何速度,不改变它的位置,它是固定的,我们有粒子-可移动的球对引力源作出React。在某个时刻,这两个球将碰撞,我试图计算正确的新的速度矢量-碰撞后,为粒子(重力源不移动!),但无论我做什么,这都不是我想达到的目标。
感谢所有这将是善良的帮助我解决这个问题!
以下是我目前为止创建的方法:
void updatePhysics(GravitySource& gravitySource, float deltaTime)
{
//Normal Vector
float distanceX = gravitySource.getPos().x - pos.x;
float distanceY = gravitySource.getPos().y - pos.y;
//Distance between GravitySource and Particle
float distance = std::sqrt(distanceX * distanceX + distanceY * distanceY);
//Simplifying division to speed up calculations
float inverseDistance = 1.f / distance;
//Unit Vector
float normalizedX = inverseDistance * distanceX;
float normalizedY = inverseDistance * distanceY;
float inverseSquareDropOff = inverseDistance * inverseDistance;
//Calculating Acceleration
float accelerationX = normalizedX * gravitySource.getStrength() * inverseSquareDropOff * deltaTime;
float accelerationY = normalizedY * gravitySource.getStrength() * inverseSquareDropOff * deltaTime;
//Updating Velocity
vel.x += accelerationX;
vel.y += accelerationY;
//Updating Position
pos.x += vel.x;
pos.y += vel.y;
//Updating Render Position
relPos.x = pos.x - radius;
relPos.y = pos.y - radius;
//TODO: Make collision work properly!!!
//Checking for collision
if (distance <= radius + gravitySource.getRadius())
{
//Tangent Vector
float tangentX = -normalizedY;
float tangentY = normalizedX;
//Velocity scalar in normal direction
float normalScalarX = normalizedX * vel.x;
float normalScalarY = normalizedY * vel.y;
//New Unit Vector
normalizedX = normalScalarX * normalizedX;
normalizedY = normalScalarY * normalizedY;
std::cout << vel.x << " " << vel.y << std::endl;
vel.x = normalizedX + tangentX;
vel.y = normalizedY + tangentY;
std::cout << vel.x << " " << vel.y << std::endl;
}
}
class GravitySource
{
sf::Vector2f pos;
sf::Vector2f relPos;
float strength;
float radius;
sf::CircleShape circleShape;
};
class Particle
{
sf::Vector2f pos;
sf::Vector2f relPos;
sf::Vector2f vel;
float radius;
sf::CircleShape circleShape;
};
2条答案
按热度按时间odopli941#
Lennard-Jones势是应用平滑推+拉交互的简单方法:
x1c 0d1x的数据
字符串
lskq00tm2#
所以我终于在@n.m.couldbeanAI的帮助下弄明白了,代码看起来像这样(这个函数的其余部分你已经在问题中了):
字符串
谢谢大家的帮助!