如何将三角形(顶点)旋转到 Qt 中的点?
How to rotate a triangle (apex point) towards the point in Qt?
我对物品旋转有疑问。
我有一个点和一个三角形,需要将其顶点旋转到目标点。图片的右侧代表它应该如何,左侧代表它如何工作。红色虚线箭头代表运动,三角形沿着它的箭头移动。绿色虚线箭头代表旋转,三角形应该沿着它的箭头旋转。
如何计算:
计算所需速度也就是方向
velocity(direction) = Vec2DNormalize(targetPoint - locationPoint) * maxVelocity;
正在计算目标点和定位点的角度
float angleLoc = atan2(rect->location.y, rect->location.x);
float angleTarg = atan2(rect->target.y, rect->target.x);
减去angleLoc后旋转-angleTarg
rotate((angleLoc - angleTarg) * 100);
这是源代码。
ster.cpp
#include "steer.h"
#include <QPointF>
#include <QBrush>
#include <QPen>
#include <vector2d.h>
#include <QGraphicsPolygonItem>
#include <QPolygonF>
#include <QPointF>
#include <QGraphicsItem>
#include <QDebug>
#include <cmath>
#include <vector>
#include <QtWidgets>
void Steer::seek()
{
//calculating desired velocity aka direction
rect->desired = Vec2DNormalize(rect->target - rect->location) * rect->maxspeed;
//calculating steering force
rect->steer = rect->desired - rect->velocity;
//if the steer force is bgger than maxforce
rect->steer.Truncate(rect->maxforce);
//adding to acceleration steering force
rect->acceleration += rect->steer;
//add to velocity acceleration which has steering force only
rect->velocity += rect->acceleration;
//if the velocity is bgger than maxspeed
rect->velocity.Truncate(rect->maxspeed);
//changing our position
rect->location += rect->velocity;
//reset the acceleration
rect->acceleration *= 0;
viewport()->repaint();
}
Steer::Steer(QGraphicsView *parent)
: QGraphicsView(parent)
{
scene = new QGraphicsScene;
rect = new Vehicle;
scene->setSceneRect(0, 0, 500, 500);
polygon << QPointF(5.0, 0.0) << QPointF(-5.0, 0.0) << QPointF(0.0, 20.0);
rect->triangle = scene->addPolygon(polygon);
this->setScene(scene);
timer = new QTimer(this);
QObject::connect(timer, SIGNAL(timeout()), this, SLOT(seek()));
timer->start();
this->show();
}
void Steer::paintEvent(QPaintEvent *)
{
QPainter painter(viewport());
painter.setBrush(QBrush(Qt::green));
painter.setPen(QPen(Qt::black));
painter.save();
//moving to position
painter.translate(rect->location.x, rect->location.y);
//calculating angles for target point and location point
float angleLoc = atan2(rect->location.y, rect->location.x);
float angleTarg = atan2(rect->target.y, rect->target.x);
//rotating after substracting angleLoc - angleTarg
painter.rotate((angleLoc - angleTarg) * 100);
painter.drawPolygon(polygon);
painter.restore();
for(int i = 0; i < vec.size(); i++)
painter.drawEllipse(vec[i].x() - 1, vec[i].y() - 1, 1 * 2.0, 1 * 2.0);
}
void Steer::mousePressEvent(QMouseEvent * click)
{
point = mapToScene(click->pos());
vec.push_back(point);
rect->target.x = point.x();
rect->target.y = point.y();
}
Here整个项目。
问题出在从弧度到度的转换中,另外:您需要偏移 90 度或在 0 度方向绘制三角形(右):
// initially point right
polygon << QPointF(20, 0) << QPointF(0, -5) << QPointF(0, 5);
// angle -> degrees conversion
const float angle = atan2(vehicle->velocity.y, vehicle->velocity.x);
vehicle->triangle->setRotation(
angle * 180./3.14);
// but in qt 5 they have this qRadiansToDegrees in <QtMath>
我对物品旋转有疑问。
我有一个点和一个三角形,需要将其顶点旋转到目标点。图片的右侧代表它应该如何,左侧代表它如何工作。红色虚线箭头代表运动,三角形沿着它的箭头移动。绿色虚线箭头代表旋转,三角形应该沿着它的箭头旋转。
如何计算:
计算所需速度也就是方向
velocity(direction) = Vec2DNormalize(targetPoint - locationPoint) * maxVelocity;
正在计算目标点和定位点的角度
float angleLoc = atan2(rect->location.y, rect->location.x);
float angleTarg = atan2(rect->target.y, rect->target.x);
减去angleLoc后旋转-angleTarg
rotate((angleLoc - angleTarg) * 100);
这是源代码。
ster.cpp
#include "steer.h"
#include <QPointF>
#include <QBrush>
#include <QPen>
#include <vector2d.h>
#include <QGraphicsPolygonItem>
#include <QPolygonF>
#include <QPointF>
#include <QGraphicsItem>
#include <QDebug>
#include <cmath>
#include <vector>
#include <QtWidgets>
void Steer::seek()
{
//calculating desired velocity aka direction
rect->desired = Vec2DNormalize(rect->target - rect->location) * rect->maxspeed;
//calculating steering force
rect->steer = rect->desired - rect->velocity;
//if the steer force is bgger than maxforce
rect->steer.Truncate(rect->maxforce);
//adding to acceleration steering force
rect->acceleration += rect->steer;
//add to velocity acceleration which has steering force only
rect->velocity += rect->acceleration;
//if the velocity is bgger than maxspeed
rect->velocity.Truncate(rect->maxspeed);
//changing our position
rect->location += rect->velocity;
//reset the acceleration
rect->acceleration *= 0;
viewport()->repaint();
}
Steer::Steer(QGraphicsView *parent)
: QGraphicsView(parent)
{
scene = new QGraphicsScene;
rect = new Vehicle;
scene->setSceneRect(0, 0, 500, 500);
polygon << QPointF(5.0, 0.0) << QPointF(-5.0, 0.0) << QPointF(0.0, 20.0);
rect->triangle = scene->addPolygon(polygon);
this->setScene(scene);
timer = new QTimer(this);
QObject::connect(timer, SIGNAL(timeout()), this, SLOT(seek()));
timer->start();
this->show();
}
void Steer::paintEvent(QPaintEvent *)
{
QPainter painter(viewport());
painter.setBrush(QBrush(Qt::green));
painter.setPen(QPen(Qt::black));
painter.save();
//moving to position
painter.translate(rect->location.x, rect->location.y);
//calculating angles for target point and location point
float angleLoc = atan2(rect->location.y, rect->location.x);
float angleTarg = atan2(rect->target.y, rect->target.x);
//rotating after substracting angleLoc - angleTarg
painter.rotate((angleLoc - angleTarg) * 100);
painter.drawPolygon(polygon);
painter.restore();
for(int i = 0; i < vec.size(); i++)
painter.drawEllipse(vec[i].x() - 1, vec[i].y() - 1, 1 * 2.0, 1 * 2.0);
}
void Steer::mousePressEvent(QMouseEvent * click)
{
point = mapToScene(click->pos());
vec.push_back(point);
rect->target.x = point.x();
rect->target.y = point.y();
}
Here整个项目。
问题出在从弧度到度的转换中,另外:您需要偏移 90 度或在 0 度方向绘制三角形(右):
// initially point right
polygon << QPointF(20, 0) << QPointF(0, -5) << QPointF(0, 5);
// angle -> degrees conversion
const float angle = atan2(vehicle->velocity.y, vehicle->velocity.x);
vehicle->triangle->setRotation(
angle * 180./3.14);
// but in qt 5 they have this qRadiansToDegrees in <QtMath>