Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions include/sslworld.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Copyright (C) 2011, Parsian Robotic Center (eew.aut.ac.ir/~parsian/grsim)
#include <QObject>
#include <QUdpSocket>
#include <QList>
#include <QTimer>
#include <QElapsedTimer>


#include "graphics.h"
Expand Down Expand Up @@ -76,13 +78,17 @@ class SSLWorld : public QObject
Robot *robot) ;
void processTeleportBall(SimulatorResponse &simulatorResponse, const TeleportBall &teleBall) const;
static void processTeleportRobot(const TeleportRobot &teleBot, Robot *robot);
QTimer* simulationTimer {nullptr};
QElapsedTimer elapsed;
public:
dReal customDT;
bool isGLEnabled;
SSLWorld(QGLWidget* parent, ConfigWidget* _cfg, RobotsFormation *form1, RobotsFormation *form2);
~SSLWorld() override;
void glinit();
void step(dReal dt=-1);
void simulate(dReal dt=-1);
void render();
SSL_WrapperPacket* generatePacket(int cam_id=0);
void addFieldLinesArcs(SSL_GeometryFieldSize *field);
static void addFieldLine(SSL_GeometryFieldSize *field, const std::string &name, float p1_x, float p1_y, float p2_x, float p2_y, float thickness);
Expand Down Expand Up @@ -124,6 +130,7 @@ public slots:
void simControlSocketReady();
void blueControlSocketReady();
void yellowControlSocketReady();
void stepSimulation();
signals:
void fpsChanged(int newFPS);
};
Expand Down
24 changes: 9 additions & 15 deletions src/glwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,27 +346,21 @@ void GLWidget::step()
const dReal* ballV = dBodyGetLinearVel(ssl->ball->body);
double ballSpeed = ballV[0]*ballV[0] + ballV[1]*ballV[1] + ballV[2]*ballV[2];
ballSpeed = sqrt(ballSpeed);

rendertimer.restart();
m_fps = frames /(time.elapsed()/1000.0);
if (!(frames % ((int)(ceil(cfg->DesiredFPS()))))) {

m_fps = frames / (time.elapsed() / 1000.0);
if (!(frames % static_cast<int>(ceil(cfg->DesiredFPS())))) {
time.restart();
frames = 0;
}
if (first_time) {ssl->step();first_time = false;}
else {
if (cfg->SyncWithGL())
{
dReal ddt=rendertimer.elapsed()/1000.0;
if (ddt>0.05) ddt=0.05;
ssl->step(ddt);
}
else {
ssl->step(cfg->DeltaTime());
}
}
frames ++;

ssl->render();

frames++;
}


void GLWidget::paintGL()
{
if (!ssl->g->isGraphicsEnabled()) return;
Expand Down
81 changes: 62 additions & 19 deletions src/sslworld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include <QtGlobal>
#include <QtNetwork>
#include <QTimer>

#include <QDebug>

Expand Down Expand Up @@ -316,6 +317,14 @@

elapsedLastPackageBlue.start();
elapsedLastPackageYellow.start();

elapsed.start();

simulationTimer = new QTimer(this);
simulationTimer->setInterval(10);
connect(simulationTimer, &QTimer::timeout,
this, &SSLWorld::stepSimulation);
simulationTimer->start();
}

int SSLWorld::robotIndex(int robot,int team) {
Expand Down Expand Up @@ -410,92 +419,127 @@
p->glinit();
}

void SSLWorld::step(dReal dt) {
if (customDT > 0) dt = customDT;
const auto ratio = m_parent->devicePixelRatio();
g->initScene(m_parent->width()*ratio,m_parent->height()*ratio,0,0.7,1);
void SSLWorld::stepSimulation()
{
if (!elapsed.isValid()) {
elapsed.start();
return;
}

qint64 nsec = elapsed.nsecsElapsed();
elapsed.restart();

dReal dt = static_cast<dReal>(nsec) / 1e9;
if (dt <= 0) {
dt = (last_dt > 0) ? last_dt : 0.01;
}

simulate(dt);
}

void SSLWorld::simulate(dReal dt)
{
if (customDT > 0) {
dt = customDT;
}

int ballCollisionTry = 5;
for (int kk=0;kk < ballCollisionTry;kk++) {
const dReal* ballvel = dBodyGetLinearVel(ball->body);
dReal ballspeed = ballvel[0]*ballvel[0] + ballvel[1]*ballvel[1] + ballvel[2]*ballvel[2];
ballspeed = sqrt(ballspeed);
if (ballspeed > 0.01) {
dReal fk = cfg->BallFriction()*cfg->BallMass()*cfg->Gravity();
dReal ballfx = -fk*ballvel[0] / ballspeed;
dReal ballfy = -fk*ballvel[1] / ballspeed;
dReal ballfz = -fk*ballvel[2] / ballspeed;
dReal balltx = -ballfy*cfg->BallRadius();
dReal ballty = ballfx*cfg->BallRadius();
dReal balltz = 0;
dBodyAddTorque(ball->body,balltx,ballty,balltz);
dBodyAddForce(ball->body,ballfx,ballfy,ballfz);
} else {
dBodySetAngularVel(ball->body, 0, 0, 0);
dBodySetLinearVel(ball->body, 0, 0, 0);
}
if (dt == 0) dt = last_dt;
else last_dt = dt;

selected = -1;
p->step(dt/ballCollisionTry);
}

sim_time += last_dt;

int best_k=-1;
dReal best_dist = 1e8;
dReal xyz[3],hpr[3];
if (selected==-2) {
best_k=-2;
dReal bx,by,bz;
ball->getBodyPosition(bx,by,bz);
g->getViewpoint(xyz,hpr);
best_dist =(bx-xyz[0])*(bx-xyz[0])
+(by-xyz[1])*(by-xyz[1])
+(bz-xyz[2])*(bz-xyz[2]);
}
for (int k=0;k<cfg->Robots_Count() * 2;k++)
{
if (robots[k]->selected)
{
g->getViewpoint(xyz,hpr);
dReal dist= (robots[k]->select_x-xyz[0])*(robots[k]->select_x-xyz[0])
+(robots[k]->select_y-xyz[1])*(robots[k]->select_y-xyz[1])
+(robots[k]->select_z-xyz[2])*(robots[k]->select_z-xyz[2]);
if (dist < best_dist) {
best_dist = dist;
best_k = k;
}
}

// Yellow robots are on the last half of count
if(k >= cfg->Robots_Count())
robots[k]->chassis->setColor(ROBOT_YELLOW_CHASSIS_COLOR);
else
robots[k]->chassis->setColor(ROBOT_BLUE_CHASSIS_COLOR);
}
if(best_k>=0)
{
if(best_k >= cfg->Robots_Count())
robots[best_k]->chassis->setColor(
QColor::fromRgbF(ROBOT_YELLOW_CHASSIS_COLOR.redF()*2,
ROBOT_YELLOW_CHASSIS_COLOR.greenF()*1.5,
ROBOT_YELLOW_CHASSIS_COLOR.blueF()*1.5)
);
else
robots[best_k]->chassis->setColor(
QColor::fromRgbF(ROBOT_BLUE_CHASSIS_COLOR.redF()*2,
ROBOT_BLUE_CHASSIS_COLOR.greenF()*1.5,
ROBOT_BLUE_CHASSIS_COLOR.blueF()*1.5)
);
}
selected = best_k;
ball->tag = -1;
for (int k=0;k<cfg->Robots_Count() * 2;k++)
{
robots[k]->step();
robots[k]->selected = false;
}

sendVisionBuffer();
frame_num++;
}

Check notice on line 532 in src/sslworld.cpp

View check run for this annotation

codefactor.io / CodeFactor

src/sslworld.cpp#L440-L532

Complex Method
void SSLWorld::render()
{
if (!g->isGraphicsEnabled())
return;

const auto ratio = m_parent->devicePixelRatio();
g->initScene(m_parent->width() * ratio,
m_parent->height() * ratio,
0, 0.7, 1);

p->draw();
g->drawSkybox(4 * cfg->Robots_Count() + 6 + 1, //31 for 6 robot
4 * cfg->Robots_Count() + 6 + 2, //32 for 6 robot
Expand All @@ -504,24 +548,23 @@
4 * cfg->Robots_Count() + 6 + 5, //31 for 6 robot
4 * cfg->Robots_Count() + 6 + 6);//36 for 6 robot

dMatrix3 R;

if (g->isGraphicsEnabled())
if (show3DCursor)
{
dRFromAxisAndAngle(R,0,0,1,0);
g->setColor(1,0.9,0.2,0.5);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
g->drawCircle(cursor_x,cursor_y,0.001,cursor_radius);
glDisable(GL_BLEND);
}
if (show3DCursor) {
dMatrix3 R;
dRFromAxisAndAngle(R,0,0,1,0);
g->setColor(1,0.9,0.2,0.5);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
g->drawCircle(cursor_x,cursor_y,0.001,cursor_radius);
glDisable(GL_BLEND);
}

g->finalizeScene();
}


sendVisionBuffer();
frame_num ++;
void SSLWorld::step(dReal dt)
{
simulate(dt);
render();
}

void SSLWorld::addRobotStatus(Robots_Status& robotsPacket, int robotID, bool infrared, KickStatus kickStatus) {
Expand Down
Loading