0
\$\begingroup\$

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. enter image description here

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.

\$\endgroup\$
1
\$\begingroup\$

You need to calculate a maximum angle of rotation allowed per game loop. Rather than calculating the angle the triangle needs to rotate to figure out only if it needs to rotate left or right, then rotate (for example) 1 degree.

The only reason to know the exact angle to point at is to calculate if the rotation required is less than the 1-degree-per-loop change (and then rotate exactly).

The "1 degree" portion can be adjusted to be more or less in order to achieve the desired change.

This is essentially doing the same thing in rotational space as you're already doing with movement. Rather than jumping to the destination, increment a little at a time until we're there.

\$\endgroup\$
  • \$\begingroup\$ The problem is i do not know how to calculate the angle of rotation correct. When the triangle's apex point reached destination it points in a wrong direction. \$\endgroup\$ – Mikhail Dec 29 '15 at 15:35
  • \$\begingroup\$ You need the inverse trigonometric functions. Specificially atan2. \$\endgroup\$ – Draco18s no longer trusts SE Dec 29 '15 at 15:36
  • \$\begingroup\$ Ok, i have a target point and a position point. What should i do next? \$\endgroup\$ – Mikhail Dec 29 '15 at 15:43
  • \$\begingroup\$ You already know the angle you should be pointing at, you're already using atan2. You should know what angle the triangle is already pointing in as well (if you don't, make a variable that holds that information), you just have to subtract one from the other. \$\endgroup\$ – Draco18s no longer trusts SE Dec 29 '15 at 15:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.