我在一行的底部添加了一个项目符号(QGraphicsItem)。advance方法在项目中移动项目,使其看起来像来自行的顶端。添加碰撞后,此操作不起作用。有没有一种方法可以给setPos(x,y)值添加一个偏移量,使它出现在行尾而不是根部。
此外,这条线旋转了360度角,因此它需要平移到它所指向的任何地方。
//function that adds item to base of line created
qreal dirx = m_FireTarget1.x()+140;
qreal diry = m_FireTarget1.y()-195;
qreal length = sqrt(dirx*dirx+diry*diry);
if (length!=0)
{
// normalized direction vector
qreal invLength= 1.0/length;
dirx *= invLength;
diry *= invLength;
// creating an angle perturbation of +/- 3°
qreal alphaPerturbation = static_cast<qreal>(qrand()%6-3) * M_PI / 180.0;
qreal xPerturbation = cos(alphaPerturbation);
qreal yPerturbation = sin(alphaPerturbation);
dirx = dirx*xPerturbation - diry*yPerturbation;
diry = diry*xPerturbation + dirx*yPerturbation;
GraphicsCircle * circle = new GraphicsCircle(dirx, diry, -140, 195);
addItem(circle);-140,195是创建直线的基础。看来我已经照你说的做了,我相信。
发布于 2014-04-17 00:30:45
假设您的行有一个特定的degreeAngle,并且您想要在该方向上将项目符号移动到某个distance,您必须这样做:
// cos and sin functions get radians angle as argument so you must convert it
radiansAngle = degreeAngle * PI / 180;
offsetX = distance * cos(radiansAngle);
offsetY = distance * sin(radiansAngle);在您的情况下,这将转换为:
qreal radiansAngle = line.angle() * M_PI / 180;
qreal offsetX = line.length() * cos(radiansAngle);
qreal offsetY = line.length() * sin(radiansAngle);所以你的新位置是旧位置加上偏移量:
qreal newX = -140 + offsetX;
qreal newY = 195 + offsetY;很抱歉,我不能理解如何将参数传递给GraphicsCircle构造函数,但是如果GraphicsCircle * circle = new GraphicsCircle(dirx, diry, -140, 195);将圆放置到坐标(-140,195),那么您应该使用...
GraphicsCircle * circle = new GraphicsCircle(dirx, diry, newX, newY);
addItem(circle);...to把它放到新的坐标上。
https://stackoverflow.com/questions/23097420
复制相似问题