for (int i = 0; i < agvs.size(); i++) { for (int j = 0; j < tasks.size(); j++) { if (tasks[j].id == agvs[i].get_task_id()) { completed_task_index = j; break; } } if (tasks[completed_task_index].completed == 2 ) { // 如果已经完成任务 paths[i].clear(); continue; // 跳过此次循环 } if (! paths[i].empty()) { int cur_x = agvs[i].getCurrentX(); int cur_y = agvs[i].getCurrentY(); Node* next_node = paths[i][0]; float speed = agvs[i].getSpeed(); float distance = sqrt(pow(next_node->x - agvs[i].getCurrentX(), 2) + pow(next_node->y - agvs[i].getCurrentY(), 2)); float time = distance / speed * 1000; //_MAP[cur_x][cur_y] = 1; QTimer::singleShot(time, this, [&, i, next_node, cur_x, cur_y]() { // 离开当前位置时将标记设为0 //_MAP[cur_x][cur_y] = 0; agvs[i].setCurrentX(next_node->x); agvs[i].setCurrentY(next_node->y); this->update(); // 在窗口中重绘 }); } }, if (agvs[i].getLoad()){ painter.drawPixmap(agvs[i].getCurrentX()*25+200-(nodeSpacing-nodeSize)/2,(agvs[i].getCurrentY()+1)*25+50-(nodeSpacing-nodeSize)/2,25,25,QPixmap(":/new/prefix1/agvload.png").scaled(25,25)); } else { painter.drawPixmap(agvs[i].getCurrentX()*25+200-(nodeSpacing-nodeSize)/2,(agvs[i].getCurrentY()+1)*25+50-(nodeSpacing-nodeSize)/2,25,25,QPixmap(":/new/prefix1/agv1.png").scaled(25,25)); },修改代碼為,小車進行平滑的移動
时间: 2024-01-02 07:04:46 浏览: 112
可以使用QPropertyAnimation来实现小车的平滑移动。具体做法如下:
1. 在类中声明一个QPropertyAnimation指针变量:`QPropertyAnimation* animation;`
2. 在路径规划部分的定时器中,创建QPropertyAnimation对象并设置相关属性:
```cpp
animation = new QPropertyAnimation(agvs[i], "pos");
animation->setDuration(time);
animation->setStartValue(QPoint(cur_x*25+200-(nodeSpacing-nodeSize)/2, (cur_y+1)*25+50-(nodeSpacing-nodeSize)/2));
animation->setEndValue(QPoint(next_node->x*25+200-(nodeSpacing-nodeSize)/2, (next_node->y+1)*25+50-(nodeSpacing-nodeSize)/2));
```
其中,第一个参数指定要进行动画的对象,这里是agvs[i],即第i个小车;第二个参数指定要进行动画的属性,这里是位置,即小车的坐标;setDuration()方法设置动画的时长;setStartValue()和setEndValue()方法设置动画的起始值和终止值。
3. 启动动画:
```cpp
animation->start();
```
4. 在动画完成后,重置路径和更新界面:
```cpp
connect(animation, &QPropertyAnimation::finished, this, [&, i]() {
paths[i].pop_front();
if (paths[i].empty()) {
agvs[i].set_task_id(-1);
}
update();
});
```
其中,connect()方法用于连接动画完成信号和槽函数,第一个参数是动画对象,第二个参数是信号,这里指定动画完成信号;第三个参数是槽函数,这里使用lambda表达式,用于重置路径、更新界面和清空任务。
5. 在绘制小车部分,直接绘制agvs[i]对象即可,无需再使用painter绘制。
完整代码如下:
```cpp
for (int i = 0; i < agvs.size(); i++) {
for (int j = 0; j < tasks.size(); j++) {
if (tasks[j].id == agvs[i].get_task_id()) {
completed_task_index = j;
break;
}
}
if (tasks[completed_task_index].completed == 2 ) { // 如果已经完成任务
paths[i].clear();
continue; // 跳过此次循环
}
if (! paths[i].empty()) {
int cur_x = agvs[i].getCurrentX();
int cur_y = agvs[i].getCurrentY();
Node* next_node = paths[i][0];
float speed = agvs[i].getSpeed();
float distance = sqrt(pow(next_node->x - agvs[i].getCurrentX(), 2) + pow(next_node->y - agvs[i].getCurrentY(), 2));
float time = distance / speed * 1000;
QPropertyAnimation* animation = new QPropertyAnimation(agvs[i], "pos");
animation->setDuration(time);
animation->setStartValue(QPoint(cur_x*25+200-(nodeSpacing-nodeSize)/2, (cur_y+1)*25+50-(nodeSpacing-nodeSize)/2));
animation->setEndValue(QPoint(next_node->x*25+200-(nodeSpacing-nodeSize)/2, (next_node->y+1)*25+50-(nodeSpacing-nodeSize)/2));
animation->start();
connect(animation, &QPropertyAnimation::finished, this, [&, i]() {
paths[i].pop_front();
if (paths[i].empty()) {
agvs[i].set_task_id(-1);
}
update();
});
}
}
for (int i = 0; i < agvs.size(); i++) {
if (agvs[i].getLoad()){
painter.drawPixmap(agvs[i].x()*25+200-(nodeSpacing-nodeSize)/2, agvs[i].y()*25+50-(nodeSpacing-nodeSize)/2, 25, 25, QPixmap(":/new/prefix1/agvload.png").scaled(25,25));
} else {
painter.drawPixmap(agvs[i].x()*25+200-(nodeSpacing-nodeSize)/2, agvs[i].y()*25+50-(nodeSpacing-nodeSize)/2, 25, 25, QPixmap(":/new/prefix1/agv1.png").scaled(25,25));
}
}
```
阅读全文