提问人:Dedsi 提问时间:9/11/2023 更新时间:9/11/2023 访问量:11
如何解决旋转矩形和边之间的冲突
How to resolve a collision between a rotating rectangle and an edge
问:
我正在尝试找出解决矩形和边之间冲突的正确方法, 目前,我有兴趣解决矩形的一个点与边的碰撞, 并希望矩形的边和边之间的碰撞会自然而然地发生。 不过,我在查找有关此类碰撞如何工作的信息时遇到了问题。
我想知道如何计算旋转速度,我假设它取决于矩形质心的速度、碰撞点的速度以及边的法向量和速度矢量的关系,以计算出速度的百分比(速度? 您是否同时考虑了点的速度和 COM 的速度?应转化为旋转速度。
我还假设根据撞击角度,力也可能施加到 COM 的速度上,我可以直觉地知道这应该如何工作,但似乎无法将它们全部组合在一起, 再 如何计算出应该应用速度的哪一部分来将矩形推离它碰撞的边缘,以及如何计算出方向。
我还猜测撞击时的摩擦可能会对力的分布产生相当大的影响,任何提示也将不胜感激。
我为我所追求的效果做了一些说明,尽管我不确定它是否完全准确到实际的碰撞响应
这是我的代码,尽管它没有实际意义,因为我对实际的物理原理感到困惑。
void caclulateRotationSpeedAndDirection(int id, sf::Vector2f lineNormal)
{
//update the centre of mass position
centre = { (points[0].x + points[2].x) * 0.5f, (points[0].y + points[2].y) * 0.5f };
sf::Vector2f centrePoint = points[id] - centre;
sf::Vector2f pointVec = points[id] - pointsPrev[id];
float direction;
// Determine direction of rotation by checking if the angle between the line normal and the vector between the centre and collision point is clockwise or anti-clockwise
if (angleDirection(lineNormal, centre - points[id])) direction = -1;
else direction = 1;
//Calculate how much force to apply to the velocity vector
float pushFraction = (90-GetAngleBetweenVectors(vel, centrePoint));
if (pushFraction < 0) pushFraction = -pushFraction;
//angle variable determines speed of rotation, positive or negative -> clockwise or anti-clockwise
angle = -direction * calculateRotationalVelocity(id,lineNormal) * getMagnitude(pointVec);
//force applied to COM velocity
sf::Vector2f v = normalise(-centrePoint * pushFraction * 0.011f) * getMagnitude(vel);
vel += v;
}
float calculateRotationalVelocity(int id, const sf::Vector2f& lineNormal) {
// Calculate the distance between the center of mass and the collision point
sf::Vector2f collisionPoint = points[id];
sf::Vector2f distance = collisionPoint - centre;
// Calculate the perpendicular distance from the collision point to the line
float perpendicularDistance = std::abs(distance.x * lineNormal.x + distance.y * lineNormal.y);
cout << "perpendicular distance: " << perpendicularDistance << endl;
// Calculate the rotational velocity using the perpendicular distance and the distance from the center of mass
float rotationalVelocity = perpendicularDistance / getMagnitude(distance);
return rotationalVelocity;
}
答: 暂无答案
评论