[英]Two points rotating around same center but distance grows
我想实现两点相互转动。 因此我使用旋转矩阵。 然而,我现在遇到的问题是点之间的距离正在增长(见附录视频1 )。 然而,距离应该在整个模拟中保持不变。
这是我用来计算速度的代码:
其中p1和p2是两点。
double xPos = p0.x+p1.x;
double yPos = p0.y+p1.y;
//The center between p1 and p2
xPos /=2;
yPos /=2;
//the rotating angle
double omega = 0.1;
//calculate the new positions
double x0new = xPos + (p0.x-xPos)*std::cos(omega) - (p0.y-yPos)*std::sin(omega);
double y0new = yPos + (p0.x-xPos)*std::sin(omega) + (p0.y-yPos)*std::cos(omega);
double x1new = xPos + (p1.x-xPos)*std::cos(omega) - (p1.y-yPos)*std::sin(omega);
double y1new = yPos + (p1.x-xPos)*std::sin(omega) + (p1.y-yPos)*std::cos(omega);
//the speed is exatly the difference as I integrate one timestep
p0.setSpeed(p0.x-x0new, p0.y-y0new);
p1.setSpeed(p1.x-x1new, p1.y-y1new);
然后我将速度恰好整合一次。 我的计算有什么问题?
更新似乎我的集成是错误的。 如果我直接设置位置,那就完美了。 但是我现在不知道这种集成有什么问题:
setSpeed(ux,uy){
ux_=ux;
uy_=uy;
}
// integrate one timestep t = 1
move(){
x = x + ux_;
y = y + uy_;
}
乍一看,主要原因是您在每次迭代中更新p0
和p1
坐标。 这会累积不准确,可能来自setSpeed
。
相反,您应该使用常量初始坐标p0
和p1
,但增加omega
角度。
这段代码没有明显的错误,但是未显示的“速度”积分表明你可能在新旧位置之间线性积分,这会使速度>标称速度时轨道扩展,速度时收缩“ nominal_speed。
我怀疑。 积分实际上是在点p0和p1之间的线段上的外推,它们应该与原点保持固定的距离(物理模拟可能会使轨迹椭圆...)
因此,如果外推因子为0,则新位置将在计算的周长上。 如果它<0(和> -1),则您将在预期轨迹内插值。
O This beautiful ascii art is trying to illustrate the integration
/ x is the original position, o is the new one and O is the
/ ___----- "integrated" value and the arc is a perfect circle :)
o-- Only at the calculated position o, there is no expansion.
--/
/ /
/ /
| /
x
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.