Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- float delta_pos(float velocity, float acceleration);
- class block {
- public:
- char *name;
- int mass;
- void grav();
- void init();
- block(float pos_x, float pos_y) {
- this->init_x = pos_x;
- this->init_y = pos_y;
- this->curr_x = pos_x;
- this->curr_y = pos_y;
- this->visibility = true;
- }
- private:
- float init_x, init_y;
- float curr_x, curr_y;
- bool visibility;
- void draw(float pos_x, float pos_y);
- };
- void block::draw(float pos_x, float pos_y) {
- glBegin(GL_POLYGON);
- glColor3f(1, 0, 0);
- glVertex2i(pos_x, pos_y);
- glVertex2i(pos_x + 20, pos_y);
- glVertex2i(pos_x + 20, pos_y + 20);
- glVertex2i(pos_x, pos_y + 20);
- glEnd();
- }
- void block::grav() {
- if (this->curr_y > 0) {
- float pos_x = this->curr_x + delta_pos(0, 0);
- float pos_y = this->curr_y - delta_pos(-2, G_CONST);
- this->curr_x = pos_x;
- this->curr_y = pos_y;
- }
- else {
- this->curr_y = 0;
- this->visibility = false;
- }
- this->draw(this->curr_x, this->curr_y);
- printf("x:%f, y:%f\n", this->curr_x, this->curr_y);
- }
- void block::init() {
- float pos_x = this->init_x;
- float pos_y = this->init_y;
- this->draw(pos_x, pos_y);
- }
- float delta_pos(float velocity, float acceleration) {
- //s = s0 + v*t + (a*t^2)/2
- float delta = velocity * t + (acceleration * t * t) / 2;
- printf("delta=%f\n", delta);
- t += 0.0001;
- return delta;
- }
- class cargo {
- public:
- char *name;
- int ord_num;
- int mass;
- int move_direction;
- block* left_block;
- block* right_block;
- cargo* above_cargo;
- cargo* below_cargo;
- void grav(bool);
- void init();
- void add_right_block(block*);
- void add_left_block(block*);
- void add_above_cargo(cargo*);
- void add_below_cargo(cargo*);
- cargo(int n, float pos_x, float pos_y) {
- this->ord_num = n;
- this->init_x = pos_x;
- this->init_y = pos_y;
- this->curr_x = pos_x;
- this->curr_y = pos_y;
- this->velo_x = 0;
- this->velo_y = 0;
- if (this->velo_y == 0) {
- this->move_direction = -1;
- }
- }
- private:
- float init_x, init_y;
- float curr_x, curr_y;
- float velo_x, velo_y;
- void draw(float, float);
- };
- void cargo::draw(float pos_x, float pos_y) {
- glBegin(GL_POLYGON);
- glColor3f(0, 1, 0);
- glVertex2i(pos_x, pos_y);
- glVertex2i(pos_x + 25, pos_y);
- glVertex2i(pos_x + 25, pos_y + 25);
- glVertex2i(pos_x, pos_y + 25);
- glEnd();
- }
- void cargo::grav(bool fall) {
- if (fall) {
- float pos_y;
- if (this->curr_y <= 0) {
- this->curr_y = 0;
- this->velo_y = sqrt(2 * G_CONST * this->init_y) * 0.8;
- this->move_direction = 1;
- }
- if (this->curr_y >= 0) {
- if (this->move_direction < 0) {
- float pos_y = (G_CONST * t * t) / 2;
- this->curr_y -= pos_y;
- }
- if (this->move_direction > 0) {
- float pos_y = this->velo_y * t - (G_CONST * t * t) / 2;
- this->curr_y += pos_y;
- }
- }
- }
- this->draw(this->curr_x, this->curr_y);
- t += 0.001;
- printf("x:%f, y:%f, v=%f\n", this->curr_x, this->curr_y, this->velo_y);
- }
- void cargo::init() {
- float pos_x = this->init_x;
- float pos_y = this->init_y;
- this->draw(pos_x, pos_y);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement