Commit 832cda0d authored by Jeff Niu's avatar Jeff Niu

Fixed velocity calculations for friction on simulated SAM

parent 41748d16
#include <QPainter>
#include <chrono>
#include <sstream>
#include "renderscene.h"
......@@ -44,6 +45,7 @@ void RenderScene::animate() {
}
void RenderScene::startRender() {
m_sam.reset();
if (!m_timer.isActive()) {
m_timer.start();
}
......@@ -78,6 +80,12 @@ void RenderScene::paintEvent(QPaintEvent *event) {
solenoid.draw(&painter, event, deltaMillis, 2000);
}
m_sam.draw(&painter, event, deltaMillis, 2000);
std::stringstream ss;
ss << "(" << m_sam.mag().x() << ", " << m_sam.mag().y() << ")";
painter.setPen(Qt::white);
painter.drawText(QPoint(10, 10), QString::fromStdString(ss.str()));
Arrow samForce(this, m_sam.pos(), m_sam.mag());
samForce.draw(&painter, event, deltaMillis, 2000);
painter.end();
......
......@@ -4,7 +4,7 @@
// Magnetic permeability of free space
#define MU_0f ((float) M_PI * 4e-7f)
// Coefficient of static friction
#define MU_sf 15.0f
#define MU_sf 1.0f
// Gravitational field strength
#define gf 9.809f
......
......@@ -19,8 +19,8 @@ void Sam::draw(QPainter *painter, QPaintEvent *, int elapsed, float scale) {
for (auto &solenoid : *solenoids) {
m_mag += solenoid.fieldAt(m_pos);
}
float vel = sqrtf(m_vel.x() * m_vel.x() + m_vel.y() + m_vel.y());
if (vel > 1e-6) {
float vel = hypotf(m_vel.x(), m_vel.y());
if (vel > 0) {
float sfric = gf * m_mass * m_fric;
vector2f vfric(-sfric * m_vel.x() / vel, -sfric * m_vel.y() / vel);
bool xdir = m_vel.x() > 0;
......@@ -69,3 +69,9 @@ void Sam::stop() {
m_vel.setX(0);
m_vel.setY(0);
}
void Sam::reset() {
m_pos.setX(0);
m_pos.setY(0);
stop();
}
......@@ -34,6 +34,11 @@ public:
*/
void stop();
/**
* Reset SAM's position.
*/
void reset();
/**
* @return SAM's current position
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment