如何旋转三角形(顶点)朝向Qt中的点? [英] How to rotate a triangle (apex point) towards the point in Qt?

查看:900
本文介绍了如何旋转三角形(顶点)朝向Qt中的点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个旋转项目的问题。



我有一个点和一个三角形,需要旋转,它的顶点朝向taregt点。图片的右侧代表它应该是什么,左侧代表它是如何工作的。红色虚线箭头表示运动,三角形沿其箭头移动。绿色虚线箭头表示旋转,三角形应沿其箭头旋转。



如何进行计算:



计算所需速度aka方向

速度(方向)= Vec2DNormalize(targetPoint - locationPoint)* maxVelocity; code>



计算目标点和位置点的角度



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

  #includesteer.h
#include< QPointF>
#include< QBrush>
#include< QPen>
#include< vector2d.h>
#include< QGraphicsPolygonItem>
#include< QPolygonF>
#include< QPointF>
#include& lt; QGraphicsItem>
#include< QDebug>
#include< cmath>
#include< vector>
#include

void Steer :: seek()
{
//计算所需的速度aka方向
rect-> desired = Vec2DNormalize(rect-> ; target-rect-> location)* rect-> maxspeed;

//计算转向力
rect-> steer = rect-> desired - rect-> velocity;

//如果转向力是bgger比maxforce
rect-> steer.Truncate(rect-> maxforce);

//加到加速转向力
rect-> acceleration + = rect-> steer;

//加到只有转向力的速度加速度
rect-> velocity + = rect->加速度;

//如果速度是bgger比maxspeed
rect-> velocity.Truncate(rect-> maxspeed);

//改变我们的位置
rect-> location + = rect-> velocity;

//复位加速度
rect->加速度* = 0;


viewport() - > repaint();
}

Steer :: Steer(QGraphicsView * parent)
:QGraphicsView(parent)
{
scene = new QGraphicsScene;
rect = new车辆;

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();

//移动到位置
painter.translate(rect-> location.x,rect-> location.y);

//计算目标点和位置点的角度
float angleLoc = atan2(rect-> location.y,rect-> location.x);
float angleTarg = atan2(rect-> target.y,rect-> target.x);

//在减法后旋转angleLoc - angleTarg
painter.rotate((angleLoc - angleTarg)* 100);

painter.drawPolygon(polygon);

painter.restore();

for(int i = 0; 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();
}

这里整个项目。

解决方案

问题是在你从弧度到度的转换,再加上:你需要一个偏移90度或绘制你的三角形在0度方向(右):

  // initial point right 
polygon<< QPointF(20,0)<< QPointF(0,-5) QPointF(0,5);

// angle - >度转换
const float angle = atan2(vehicle-> velocity.y,vehicle-> velocity.x);
vehicle-> triangle-> setRotation(
angle * 180./3.14);
//但是在qt 5中,它们在< QtMath>中有这个qRadiansToDegrees。


I have an issue with items rotating.

I have a point and a triangle which needs to be rotated with its apex point towards the taregt point. The right side of the picture represents how it is should to be, and the left side represents how it works. Red dashed arrows represent motion, the triangle moves along its arrow. Green dashed arrow represent rotation, the triangle should rotates along its arrow.

How do I do calculations:

calculating desired velocity aka direction

velocity(direction) = Vec2DNormalize(targetPoint - locationPoint) * maxVelocity;

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 subtracting angleLoc - angleTarg

rotate((angleLoc - angleTarg) * 100);

Here it is the source code.

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 the whole project.

解决方案

The problem is in your conversion from radians to degrees, plus: you need an offset of 90 degrees or draw your triangle in the 0 degrees direction (right):

// 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>

这篇关于如何旋转三角形(顶点)朝向Qt中的点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆