Skip to content

Instantly share code, notes, and snippets.

@hiroyuki
Last active June 18, 2019 09:02
Show Gist options
  • Save hiroyuki/5b8a33175f5e2a2d13ac7f51286e8ecd to your computer and use it in GitHub Desktop.
Save hiroyuki/5b8a33175f5e2a2d13ac7f51286e8ecd to your computer and use it in GitHub Desktop.
badminton simlate
#pragma once
#include "ofMain.h"
class BadSim
{
public:
const float mass = 5.19;
const float grav = 9.80665;
const float terminal_velocity = 6.7;
float scale = 30; // 30 pix == 1m
void setScale(const float& scale)//pix == 1m
{
this->scale = scale;
}
/*
initial_speed:最初のベロシティ(meter per sec )
tan_rad:撃ち込まれたradian up + buttom -
t:時間(sec)
*/
glm::vec2 getPosition(const float& initial_spead, const float& tan_rad, const float& t)
{
float rx = getX2(t, tan_rad, initial_spead);
float ry = getY2(t, tan_rad, initial_spead);
float x = rx * scale;
float y = ry * scale;
return glm::vec2(x,y);
}
void draw1(float rad, float initialV)
{
for (float t = 0; t < 3; t += 0.05)
{
float initialV = 30;//
float rx = getX1(t, rad, initialV);
float ry = getY1(t, rad, initialV);
float x = rx * scale;
float y = - ry * scale;
ofDrawCircle(x, y, 4);
//ofDrawBitmapString(ofToString(t) + "/" + ofToString(rx) + "," + ofToString(ry), x + 5, y + 10);
}
}
void draw2(float rad, float initialV)
{
for (float t = 0; t < 3; t += 0.01)
{
float initialV = 30;//
glm::vec2 pos = getPosition(initialV, rad, t);
ofLog() << t << ",(" << pos.x << "," << pos.y << ")";
float x = pos.x;
float y = -pos.y;
ofDrawCircle(x, y, 4);
//if (ry < 0)
//{
// break;
//}
//ofDrawBitmapString(ofToString(t) + "/" + ofToString(rx) + "," + ofToString(ry), x + 5, y + 10);
}
}
private:
//https://pdfs.semanticscholar.org/24d3/816bcfb48fce9684bff997863afee0f6649d.pdf?_ga=2.27159165.1148479536.1560757194-83735025.1560757194
float getY2(float t, float tan_rad, float initialV)
{
float result;
float initYV = initialV * sin(tan_rad);
result = pow(terminal_velocity, 2) / grav *
log(abs(sin(grav * t / terminal_velocity + atan(terminal_velocity / initYV))
/ sin(atan(terminal_velocity / initYV))));
return result;
}
float getX2(float t, float tan_rad, float initialV)
{
float result;
float initXV = initialV * cos(tan_rad);
result = pow(terminal_velocity, 2) / grav * log((initXV *grav * t + pow(terminal_velocity, 2)) / pow(terminal_velocity, 2));
return result;
}
float getY1(float t, float tan_rad, float initialV)
{
float result;
float initYV = initialV * sin(tan_rad);
result = terminal_velocity / grav * (terminal_velocity + initYV) * (1 - exp(-grav * t / terminal_velocity)) - terminal_velocity * t;
return result;
}
float getX1(float t, float tan_rad, float initialV)
{
float result;
float initXV = initialV * cos(tan_rad);
result = terminal_velocity * initXV / grav * (1 - exp(-grav * t / terminal_velocity));
return result;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment