Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 5
- c
- 1001 100 380 100
- 2001
- b
- 2001 200 700 0
- 1001 0 2002
- b
- 2002 300 480 10
- 2001 1002 0
- c
- 1002 350 250 30
- 2002
- p
- 800 930 600 250
- end_scheme
- 5
- b
- 2001 150 480 10
- 0 1001 2002
- c
- 1001 120 250 30
- 2001
- b
- 2002 250 700 0
- 2001 0 1002
- c
- 1002 310 280 100
- 2002
- p
- 800 930 600 250
- end_scheme
- 10
- c
- 1001 190 400 100
- 2001
- b
- 2001 250 700 0
- 1001 0 2002
- b
- 2002 350 500 5
- 2001 1002 2003
- c
- 1002 400 230 30
- 2002
- b
- 2003 450 700 0
- 2002 0 2004
- b
- 2004 550 500 10
- 2003 1003 2005
- c
- 1003 570 300 50
- 2004
- b
- 2005 650 700 0
- 2004 0 1004
- c
- 1004 750 400 100
- 2005
- p
- 800 930 600 250
- end_scheme
- 3
- c
- 1001 100 350 100
- 2000
- b
- 2000 200 800 0
- 1001 0 1002
- c
- 1002 300 500 10
- 2000
- end_scheme
- // stdafx.h : include file for standard system include files,
- // or project specific include files that are used frequently, but
- // are changed infrequently
- //
- #pragma once
- #include "targetver.h"
- #define _CRT_SECURE_NO_WARNINGS
- //if print is lagging switch this
- #include <stdio.h>
- //#include <iostream>
- #include <vector>
- #include <tchar.h>
- #include <stdlib.h>
- #define _USE_MATH_DEFINES
- #include <cmath>
- #include <math.h>
- #include <ctime>
- #include <time.h>
- #include <Windows.h>
- #include <string>
- #define GLEW_STATIC
- #include <GL\glew.h>
- #include <GLFW\glfw3.h>
- typedef struct { float x, y; } coord;
- #include "body.h"
- #include "cell.h"
- #include "map.h"
- #include "pendulum.h"
- #include "node.h"
- #include "dll.h"
- #define SCREEN_WIDTH 1000
- #define SCREEN_HEIGHT 1000
- #define ROOF_HEIGHT 950
- #define FLOOR_HEIGHT 50
- #define SPLIT_X 10
- #define SPLIT_Y 10
- #define G_CONST 9.781665
- #define MAXRANDID 99999
- #define REVD(COORD_Y) ((SPLIT_Y - 1) - (COORD_Y))
- #define PC_MODE 16
- #define CONSOLE_MODE 0
- #pragma once
- class node
- {
- public:
- int id;
- char type;
- int listpos;
- int mass;
- float velo, accel;
- bool halt_move, halt_pend;
- node *next_node;
- node *prev_node;
- node *cp_left_node, *cp_center_node, *cp_right_node;
- int cp_left_id, cp_center_id, cp_right_id;
- coord init;
- coord curr;
- float pend_angle, pend_rope_len, pend_amplitude, pend_start_amplitude;
- coord ticker, carrier;
- void count_pend_param();
- void set_list_param(char type, int id, int listpos, float pos_x, float pos_y, int mass);
- void set_cp_ids(int left, int center, int right);
- void set_cp_ids(int center);
- void set_list_size(int list_size, int cargos_amount, int blocks_amount);
- void draw_body();
- void draw_ropes();
- void phys_pend(float t);
- void phys_move(float t);
- void deliver_motion_to_list();
- void deliver_motion_by_id(int id);
- node *get_by_id(int id);
- node *get_next_by_type(char type);
- node *get_prev_by_type(char type);
- coord get_cp_left_pos();
- coord get_cp_center_pos();
- coord get_cp_right_pos();
- node(int id);
- ~node();
- private:
- int list_size, cargos_amount, blocks_amount;
- int size_x, size_y, size_border, size_rope, size_bracket;
- coord cp_left_pos, cp_center_pos, cp_right_pos;
- };
- //cpp
- #include "stdafx.h"
- using namespace std;
- node::node(int x)
- {
- id = x;
- next_node = NULL;
- prev_node = NULL;
- halt_move = false;
- halt_pend = false;
- }
- node::~node()
- {
- }
- void node::set_list_param(char type, int id, int listpos, float pos_x, float pos_y, int mass) {
- this->init.x = pos_x;
- this->init.y = pos_y;
- this->curr.x = pos_x;
- this->curr.y = pos_y;
- this->type = type;
- this->id = id;
- this->listpos = listpos;
- this->mass = mass;
- this->size_x = 100;
- this->size_y = 100;
- this->size_border = 5;
- this->size_rope = 3;
- this->size_bracket = 15;
- this->velo = 0;
- this->accel = (-1)*G_CONST;
- this->halt_move = false;
- this->halt_pend = false;
- }
- void node::set_cp_ids(int left, int center, int right) {
- this->cp_left_id = left;
- this->cp_center_id = center;
- this->cp_right_id = right;
- }
- void node::set_cp_ids(int center) {
- this->cp_center_id = center;
- }
- void node::set_list_size(int size, int cargos, int blocks) {
- //for every block to know are there another blocks
- this->list_size = size;
- this->cargos_amount = cargos;
- this->blocks_amount = blocks;
- }
- node* node::get_by_id(int id) {
- node *tmp = this;
- while (tmp != NULL) {
- if (tmp->prev_node == NULL)
- break;
- tmp = tmp->prev_node;
- }
- while (tmp != NULL) {
- if (tmp->id == id) {
- return tmp;
- }
- tmp = tmp->next_node;
- }
- return NULL;
- }
- node* node::get_next_by_type(char type) {
- node *tmp = this;
- tmp = tmp->next_node;
- while (tmp != NULL) {
- if (tmp->type == type) {
- return tmp;
- }
- tmp = tmp->next_node;
- }
- return NULL;
- }
- node* node::get_prev_by_type(char type) {
- node *tmp = this;
- tmp = tmp->prev_node;
- while (tmp != NULL) {
- if (tmp->type == type) {
- return tmp;
- }
- tmp = tmp->prev_node;
- }
- return NULL;
- }
- coord node::get_cp_left_pos() {
- coord retval;
- retval.x = this->curr.x - this->size_x / 2;
- retval.y = this->curr.y;
- this->cp_left_pos.x = retval.x;
- this->cp_left_pos.y = retval.y;
- return retval;
- }
- coord node::get_cp_center_pos() {
- if ('b' == this->type) {
- cp_center_pos = curr;
- return this->curr;
- }
- coord retval;
- retval.x = this->curr.x;
- retval.y = this->curr.y + this->size_y / 2;
- this->cp_center_pos.x = retval.x;
- this->cp_center_pos.y = retval.y;
- return retval;
- }
- coord node::get_cp_right_pos() {
- coord retval;
- retval.x = this->curr.x + this->size_x / 2;
- retval.y = this->curr.y;
- this->cp_right_pos.x = retval.x;
- this->cp_right_pos.y = retval.y;
- return retval;
- }
- void draw_line(float from_x, float from_y, float to_x, float to_y) {
- glColor3f(0, 0, 0);
- glBegin(GL_LINES);
- glVertex2f(from_x, from_y);
- glVertex2f(to_x, to_y);
- glEnd();
- }
- void draw_line(coord from, coord to) {
- glColor3f(0, 0, 0);
- glBegin(GL_LINES);
- glVertex2f(from.x, from.y);
- glVertex2f(to.x, to.y);
- glEnd();
- }
- void draw_bracket(int x, int y, int size) {
- glLineWidth(3.0);
- glColor3f(0, 0, 0);
- glBegin(GL_LINES);
- glVertex2f(x, y);
- glVertex2f(x, y - size);
- glEnd();
- //outer
- GLfloat phi = 0, theta = 0;
- glBegin(GL_POLYGON);
- glColor3f(0, 0, 0);
- for (phi = 0; phi < 2 * M_PI; phi += 0.1) {
- glVertex2f(x + 8 * cos(phi), (y - size) + 8 * sin(phi));
- }
- glEnd();
- //inner
- glBegin(GL_POLYGON);
- glColor3f(0.6, 0.6, 0.6);
- for (phi = 0; phi < 2 * M_PI; phi += 0.1) {
- glVertex2f(x + 5 * cos(phi), (y - size) + 5 * sin(phi));
- }
- glEnd();
- glBegin(GL_POLYGON);
- glColor3f(0.6, 0.6, 0.6);
- glVertex2f(x - 8, y - size);
- glVertex2f(x - 8, y - 7);
- glVertex2f(x - 2, y - 7);
- glVertex2f(x - 2, y - size);
- glEnd();
- }
- void node::draw_body() {
- float center_x = this->curr.x;
- float center_y = this->curr.y;
- int size_x = this->size_x;
- int size_y = this->size_y;
- int rad = size_x / 2;
- int border = this->size_border;
- if ('c' == this->type) {
- coord lb, lt, rt, rb;
- lb.x = center_x - size_x / 2;
- lb.y = center_y - size_y / 2;
- lt.x = center_x - size_x / 2;
- lt.y = center_y + size_y / 2;
- rt.x = center_x + size_x / 2;
- rt.y = center_y + size_y / 2;
- rb.x = center_x + size_x / 2;
- rb.y = center_y - size_y / 2;
- //outer black square
- glBegin(GL_POLYGON);
- glColor3f(0, 0, 0);
- glVertex2f(lb.x, lb.y);
- glColor3f(0.4, 0.4, 0.4);
- glVertex2f(lt.x, lt.y);
- glColor3f(0, 0, 0);
- glVertex2f(rt.x, rt.y);
- glVertex2f(rb.x, rb.y);
- glEnd();
- //inner white square
- glBegin(GL_POLYGON);
- glColor3f(0.7, 0.6, 0.6);
- glVertex2f(lb.x + border, lb.y + border);
- glColor3f(1, 1, 1);
- glVertex2f(lt.x + border, lt.y - border);
- glColor3f(0.8, 0.9, 0.8);
- glVertex2f(rt.x - border, rt.y - border);
- glColor3f(0.4, 0.4, 0.6);
- glVertex2f(rb.x - border, rb.y + border);
- glEnd();
- }
- if ('b' == this->type) {
- GLfloat phi = 0, theta = 0;
- //outer black circle
- glBegin(GL_POLYGON);
- glColor3f(0, 0, 0);
- for (phi = 0; phi < 2 * M_PI; phi += 0.1) {
- glVertex2f(center_x + rad*cos(phi), center_y + rad*sin(phi));
- }
- glEnd();
- //inner white circle
- glBegin(GL_POLYGON);
- for (phi = 0; phi < 2 * M_PI; phi += 0.1) {
- glColor3f(0.7, 0.7, 0.8);
- if (cos(phi) < 0 && sin(phi) > 0) {
- glColor3f(0.75, 0.7, 0.75);
- }
- glVertex2f(center_x + (rad - border)*cos(phi), center_y + (rad - border)*sin(phi));
- }
- glEnd();
- }
- }
- void node::draw_ropes() {
- node *next = this->next_node;
- node *prev = this->prev_node;
- if (NULL == next) {
- return;
- }
- glLineWidth((GLfloat)this->size_rope);
- glColor3f(0, 0, 0);
- if ('b' == this->type) {
- //suspension to the ceiling
- if (0 == this->cp_left_id) {
- coord this_block_cp_left = this->get_cp_left_pos();
- draw_bracket(this_block_cp_left.x, ROOF_HEIGHT, this->size_bracket);
- draw_line(this_block_cp_left.x, this_block_cp_left.y, this_block_cp_left.x, ROOF_HEIGHT - this->size_bracket - 2);
- }
- if (0 == this->cp_center_id) {
- coord this_block_cp_center = this->get_cp_center_pos();
- draw_bracket(this_block_cp_center.x, ROOF_HEIGHT, this->size_bracket);
- draw_line(this_block_cp_center.x, this_block_cp_center.y, this_block_cp_center.x, ROOF_HEIGHT - this->size_bracket -2);
- }
- if (0 == this->cp_right_id) {
- coord this_block_cp_right = this->get_cp_right_pos();
- draw_bracket(this_block_cp_right.x, ROOF_HEIGHT, this->size_bracket);
- draw_line(this_block_cp_right.x, this_block_cp_right.y, this_block_cp_right.x, ROOF_HEIGHT - this->size_bracket -2);
- }
- //mark central point
- coord this_block_cp_center = this->get_cp_center_pos();
- glPointSize(7.0);
- glColor3f(0, 0, 0);
- glBegin(GL_POINTS);
- glVertex2f(this_block_cp_center.x, this_block_cp_center.y);
- glEnd();
- }
- if ('c' == this->type && 'b' == next->type) {
- if (NULL != this->prev_node && 'b' == this->prev_node->type && 'b' == this->next_node->type) return;
- coord this_cargo_cp = this->get_cp_center_pos();
- coord next_block_cp_left = next->get_cp_left_pos();
- draw_line(this_cargo_cp, next_block_cp_left);
- }
- if ('b' == this->type && 'b' == next->type) {
- coord this_block_cp_right = this->get_cp_right_pos();
- coord next_block_cp_left = next->get_cp_left_pos();
- draw_line(this_block_cp_right, next_block_cp_left);
- }
- if ('b' == this->type && 'c' == next->type) {
- //cargo hanging on a block
- if (0 != this->cp_center_id && -1 != this->cp_center_id) {
- coord this_block_cp_center = this->get_cp_center_pos();
- coord next_cargo_cp_center = this->next_node->get_cp_center_pos();
- draw_line(this_block_cp_center, next_cargo_cp_center);
- }
- //crago is twined
- if (0 == this->cp_center_id && 0 != this->cp_right_id) {
- coord this_block_cp_right = this->get_cp_right_pos();
- coord next_cargo_cp = next->get_cp_center_pos();
- draw_line(this_block_cp_right, next_cargo_cp);
- }
- if (NULL != next->next_node && 'b' == next->next_node->type) {
- coord this_block_cp_right = this->get_cp_right_pos();
- coord nearest_block_cp_left = next->next_node->get_cp_left_pos();
- draw_line(this_block_cp_right, nearest_block_cp_left);
- }
- }
- }
- void node::phys_move(float t) {
- if (0 != this->mass && false == this->halt_move) {
- float a, v, y0;
- float bot_limit = (float)FLOOR_HEIGHT;
- float top_limit = (float)ROOF_HEIGHT;
- node *next_cargo = this->get_next_by_type('c');
- node *prev_cargo = this->get_prev_by_type('c');
- node *next_block = this->get_next_by_type('b');
- node *prev_block = this->get_prev_by_type('b');
- if ('c' == this->type) {
- if (NULL != this->next_node)
- top_limit = this->next_node->curr.y - this->next_node->size_y / 2;
- if (NULL != this->prev_node)
- top_limit = this->prev_node->curr.y - this->prev_node->size_y / 2;
- }
- if (NULL == prev_cargo && ((NULL != next_cargo) || (NULL != prev_block))) {
- //next cargo should be the last cargo
- a = G_CONST *(next_cargo->mass - this->mass) / (next_cargo->mass + this->mass);
- v = this->velo;
- y0 = this->curr.y;
- this->accel = a;
- this->velo = v;
- this->curr.y = y0;
- this->deliver_motion_to_list();
- /*if (bot_limit <= this->curr.y - this->size_y / 2 && top_limit >= this->curr.y + this->size_y / 2) {
- this->curr.x = this->curr.x;
- this->curr.y = y0 + v*t + (a*t*t) / 2;
- }
- else {
- this->halt = true;
- }
- return;*/
- }
- if (bot_limit <= this->curr.y - this->size_y / 2 && top_limit >= this->curr.y + this->size_y / 2) {
- this->curr.x = this->curr.x;
- this->curr.y = this->curr.y + this->velo*t + (this->accel*t*t) / 2;
- }
- else {
- this->halt_move = true;
- }
- }
- }
- void node::deliver_motion_to_list() {
- float accel = this->accel;
- node *curr = this->next_node;
- int i = 0;
- while (NULL != curr) {
- if ('c' == curr->type) {
- int his_blocks_id = curr->cp_center_id;
- node *his_block = this->get_by_id(his_blocks_id);
- if (NULL != his_block) {
- if (his_block->cp_center_id == curr->id) {
- //this cargo is hanging on block
- //curr->accel = -accel / 2;
- curr->deliver_motion_by_id(curr->cp_center_id);
- } else {
- //its twined
- //its also the last cargo
- curr->accel = -accel;
- }
- } else {
- printf("[node::deliver_motion_to_list]: couldnt find block[id:%d] for cargo[id:%d]\n", his_blocks_id, curr->id);
- }
- }
- if ('b' == curr->type) {
- if (0 != curr->cp_center_id && -1 != curr->cp_center_id) {
- curr->accel = -accel / 2;
- curr->deliver_motion_by_id(curr->cp_center_id);
- }
- i++;
- }
- curr = curr->next_node;
- }
- }
- void node::deliver_motion_by_id(int id) {
- node *tmp = this->get_by_id(id);
- tmp->accel = this->accel;
- }
- void node::count_pend_param() {
- //find pos of carrier (it's cargo cp)
- if (NULL != this->prev_node) {
- if (this->prev_node->cp_center_id == this->id) {
- this->carrier = this->prev_node->get_cp_center_pos();
- }
- if (this->prev_node->cp_right_id == this->id) {
- this->carrier = this->prev_node->get_cp_right_pos();
- }
- }
- if (NULL == this->prev_node && NULL != this->next_node) {
- if (this->next_node->cp_center_id == this->id) {
- this->carrier = this->next_node->get_cp_center_pos();
- }
- if (this->next_node->cp_left_id == this->id) {
- this->carrier = this->next_node->get_cp_left_pos();
- }
- }
- this->ticker = curr;
- float CM = abs(this->carrier.y - this->ticker.y);
- float TM = abs(this->carrier.x - this->ticker.x);
- float CT = sqrt(TM*TM + CM*CM);
- this->pend_rope_len = CT;
- this->pend_angle = asin(TM / CT);
- this->pend_amplitude = this->pend_angle;
- this->pend_start_amplitude = this->pend_angle;
- }
- void node::phys_pend(float t) {
- coord carr = this->carrier;
- coord tick = this->ticker;
- if (!halt_pend && tick.y - this->size_y > FLOOR_HEIGHT && tick.y + this->size_y < ROOF_HEIGHT) {
- float omega = sqrt(G_CONST / this->pend_rope_len);
- //float attenuation = pow(M_E, -log(this->amplitude / (this->amplitude * 0.5)));
- float attenuation_k = 0.999;
- float phi = this->pend_amplitude * cos(omega * t + this->pend_start_amplitude);
- this->pend_amplitude *= attenuation_k;
- this->pend_angle = phi;
- float TM = this->pend_rope_len * sin(phi);
- float CM = this->pend_rope_len * sin(90 * M_PI / 180 - phi); //1,57 rad
- coord T;
- T.x = carr.x - TM;
- T.y = carr.y - CM;
- this->ticker = T;
- //this->curr.x = T.x;
- }
- }
- class dll {
- public:
- bool permit_phys_move;
- bool phys_move_enabled;
- bool phys_pend_enabled;
- void append_front(int id);
- void append_back(int id);
- void destroy_list();
- int get_length();
- node* get_front();
- node* get_back();
- node* get_by_id(int id);
- node* get_by_pos(int pos);
- void display_forward();
- void display_backward();
- void display_objects();
- void draw_objects();
- void draw_ropes();
- void set_connection_points();
- void set_pend_params();
- void enable_phys_pend(float time);
- void enable_phys_move(float time);
- void check_collisions();
- void check_attenuation();
- dll() { front = NULL; back = NULL; length = 0; phys_move_enabled = false; phys_pend_enabled = true; permit_phys_move = false; }
- ~dll() { destroy_list(); }
- private:
- int length;
- int cargos_amount, blocks_amount;
- node *front;
- node *back;
- };
- //cpp
- #include "stdafx.h"
- using namespace std;
- void dll::append_front(int x) {
- node *n = new node(x);
- if (front == NULL) {
- front = n;
- back = n;
- }
- else {
- front->prev_node = n;
- n->next_node = front;
- front = n;
- }
- this->length++;
- printf("fornt appended\n");
- }
- void dll::append_back(int x) {
- node *n = new node(x);
- if (back == NULL) {
- front = n;
- back = n;
- }
- else {
- back->next_node = n;
- n->prev_node = back;
- back = n;
- }
- this->length++;
- printf("back appended\n");
- }
- int dll::get_length() {
- return this->length;
- }
- node* dll::get_front() {
- return this->front;
- }
- node* dll::get_back() {
- return this->back;
- }
- node* dll::get_by_id(int id) {
- node *tmp = front;
- node *retval;
- while (tmp != NULL) {
- if (id == tmp->id) {
- retval = tmp;
- return retval;
- }
- tmp = tmp->next_node;
- }
- return NULL;
- }
- node* dll::get_by_pos(int pos) {
- node *tmp = front;
- node *retval;
- int i = 0;
- while (tmp != NULL) {
- if (i == pos) {
- retval = tmp;
- return retval;
- }
- i++;
- tmp = tmp->next_node;
- }
- return NULL;
- }
- void dll::display_forward() {
- node *tmp = front;
- printf("\n\nnodes(%d) in forward order:\n", this->length);
- while (tmp != NULL) {
- printf("%d ", tmp->id);
- tmp = tmp->next_node;
- }
- printf("\n");
- }
- void dll::display_backward() {
- node *tmp = back;
- printf("\n\nnodes(%d) in reverse order:\n", this->length);
- while (tmp != NULL) {
- printf("%d ", tmp->id);
- tmp = tmp->prev_node;
- }
- printf("\n");
- }
- void dll::display_objects() {
- node *tmp = front;
- printf("\n\nobjects(%d) in forward order:\n", this->length);
- while (NULL != tmp) {
- printf("(id:%d", tmp->id);
- if ('c' == tmp->type)
- printf("C, ");
- if ('b' == tmp->type)
- printf("B, ");
- printf("listpos:%d, mass:%d) ", tmp->listpos, tmp->mass);
- tmp = tmp->next_node;
- }
- printf("\n");
- }
- void dll::draw_objects() {
- node *tmp = front;
- while (NULL != tmp) {
- tmp->draw_body();
- tmp = tmp->next_node;
- }
- }
- void dll::draw_ropes() {
- node *tmp = front;
- int i;
- for (i = 0; i < this->length; i++) {
- tmp->draw_ropes();
- tmp = tmp->next_node;
- }
- }
- void dll::destroy_list() {
- node *T = back;
- while (T != NULL) {
- node *T2 = T;
- T = T->prev_node;
- delete T2;
- }
- front = NULL;
- back = NULL;
- }
- void dll::enable_phys_pend(float t) {
- if (this->phys_pend_enabled) {
- node *tmp = front;
- while (NULL != tmp) {
- if ('c' == tmp->type) {
- tmp->phys_pend(t);
- }
- tmp = tmp->next_node;
- }
- }
- }
- void dll::enable_phys_move(float t) {
- if (this->phys_move_enabled && this->permit_phys_move) {
- node *tmp = front;
- while (NULL != tmp) {
- tmp->phys_move(t);
- tmp = tmp->next_node;
- }
- }
- }
- void dll::check_collisions() {
- if (this->permit_phys_move) {
- node *tmp = front;
- while (NULL != tmp) {
- if (tmp->halt_move) {
- this->phys_move_enabled = false;
- tmp->halt_pend = true;
- }
- tmp = tmp->next_node;
- }
- }
- }
- void dll::check_attenuation() {
- node *tmp = front;
- int mark = 0;
- while (NULL != tmp) {
- if ('c' == tmp->type) {
- if (tmp->pend_amplitude < 0.07) {
- //tmp->halt_pend = true;
- tmp->curr.x = tmp->ticker.x;
- mark++;
- }
- else {
- tmp->curr = tmp->ticker;
- }
- }
- tmp = tmp->next_node;
- }
- if (mark == this->cargos_amount) {
- this->permit_phys_move = true;
- }
- }
- void dll::set_connection_points() {
- node *tmp = front;
- //count by type
- while (NULL != tmp) {
- if ('c' == tmp->type) this->cargos_amount++;
- if ('b' == tmp->type) this->blocks_amount++;
- tmp = tmp->next_node;
- }
- tmp = front;
- while (NULL != tmp) {
- tmp->set_list_size(this->length, this->cargos_amount, this->blocks_amount);
- if ('c' == tmp->type) {
- int cp_center_id = tmp->cp_center_id;
- tmp->cp_center_node = this->get_by_id(cp_center_id);
- }
- if ('b' == tmp->type) {
- tmp->cp_left_node = this->get_by_id(tmp->cp_left_id);
- tmp->cp_center_node = this->get_by_id(tmp->cp_center_id);
- tmp->cp_right_node = this->get_by_id(tmp->cp_right_id);
- }
- tmp = tmp->next_node;
- }
- }
- void dll::set_pend_params() {
- node *tmp = front;
- while (NULL != tmp) {
- if ('c' == tmp->type) {
- tmp->count_pend_param();
- }
- tmp = tmp->next_node;
- }
- }
- #pragma once
- class pendulum
- {
- public:
- coord carrier;
- coord init_ticker;
- coord curr_ticker;
- float rope_len;
- float curr_angle, angle_degree, start_angle;
- float amplitude, start_amplitude;
- int mass;
- void set(coord carrier, coord ticker);
- void set_physics(bool k);
- void count_rope_len();
- void count_angle();
- void draw();
- void move(float t);
- bool get_physics();
- pendulum();
- ~pendulum();
- private:
- bool physics;
- int ticker_rad, ticker_border;
- };
- //cpp
- #include "stdafx.h"
- using namespace std;
- pendulum::pendulum() {
- physics = false;
- }
- pendulum::~pendulum()
- {
- }
- void pendulum::set_physics(bool ka) {
- this->physics = ka;
- }
- bool pendulum::get_physics() {
- return this->physics;
- }
- void pendulum::set(coord carrier, coord ticker) {
- this->carrier = carrier;
- this->init_ticker = ticker;
- this->curr_ticker = ticker;
- this->start_angle = 0;
- this->count_angle();
- this->amplitude = this->curr_angle;
- this->start_amplitude = this->amplitude;
- this->physics = false;
- this->ticker_rad = 40;
- this->ticker_border = 5;
- }
- void pendulum::count_angle() {
- this->count_rope_len();
- float TM = abs(this->carrier.x - this->curr_ticker.x);
- float CT = this->rope_len;
- float angle = asin(TM / CT);
- this->curr_angle = angle;
- this->angle_degree = angle * 180 / M_PI;
- }
- void pendulum::count_rope_len() {
- coord carr = this->carrier;
- coord tick = this->curr_ticker;
- float CM = abs(carr.y - tick.y);
- float TM = abs(carr.x - tick.x);
- float CT = sqrt(TM*TM + CM*CM);
- this->rope_len = CT;
- }
- void draw_pendulum_bracket(int x, int y, int size) {
- glLineWidth(3.0);
- glColor3f(0, 0, 0);
- glBegin(GL_LINES);
- glVertex2f(x, y);
- glVertex2f(x, y - size);
- glEnd();
- //outer
- GLfloat phi = 0, theta = 0;
- glBegin(GL_POLYGON);
- glColor3f(0, 0, 0);
- for (phi = 0; phi < 2 * M_PI; phi += 0.1) {
- glVertex2f(x + 8 * cos(phi), (y - size) + 8 * sin(phi));
- }
- glEnd();
- //inner
- glBegin(GL_POLYGON);
- glColor3f(0.6, 0.6, 0.6);
- for (phi = 0; phi < 2 * M_PI; phi += 0.1) {
- glVertex2f(x + 5 * cos(phi), (y - size) + 5 * sin(phi));
- }
- glEnd();
- }
- void pendulum::draw() {
- coord carrier = this->carrier;
- coord ticker = this->curr_ticker;
- //carrier
- draw_pendulum_bracket(carrier.x, carrier.y + 20, 15);
- //ticker outer
- GLfloat phi = 0, theta = 0;
- glBegin(GL_POLYGON);
- glColor3f(0, 0, 0);
- for (phi = 0; phi < 2 * M_PI; phi += 0.1) {
- glVertex2f(ticker.x + this->ticker_rad * cos(phi), ticker.y + this->ticker_rad * sin(phi));
- }
- glEnd();
- //ticker inner
- glBegin(GL_POLYGON);
- glColor3f(1, 1, 1);
- for (phi = 0; phi < 2 * M_PI; phi += 0.1) {
- glVertex2f(ticker.x + (this->ticker_rad - this->ticker_border) * cos(phi), ticker.y + (this->ticker_rad - this->ticker_border) * sin(phi));
- }
- glEnd();
- //ticker center
- glPointSize(7.0);
- glColor3f(0, 0, 0);
- glBegin(GL_POINTS);
- glVertex2f(ticker.x, ticker.y);
- glEnd();
- //rope
- glLineWidth(3.0);
- glColor3f(0, 0, 0);
- glBegin(GL_LINES);
- glVertex2f(carrier.x, carrier.y);
- glVertex2f(ticker.x, ticker.y);
- glEnd();
- }
- void pendulum::move(float t) {
- coord carr = this->carrier;
- coord tick = this->curr_ticker;
- if (tick.y - this->ticker_rad > FLOOR_HEIGHT && tick.y + this->ticker_rad < ROOF_HEIGHT) {
- this->count_angle();
- float omega = sqrt(G_CONST / this->rope_len);
- //float attenuation = pow(M_E, -log(this->amplitude / (this->amplitude * 0.5)));
- float attenuation_k = 0.99999;
- float phi = this->amplitude * cos(omega * t + this->start_angle);
- this->amplitude *= attenuation_k;
- this->curr_angle = phi;
- float TM = this->rope_len * sin(phi);
- float CM = this->rope_len * sin(90*M_PI/180 - phi); //1,57 rad
- coord T;
- T.x = carr.x - TM;
- T.y = carr.y - CM;
- this->curr_ticker = T;
- }
- }
- //glfw
- // glfw_30.cpp : Defines the entry point for the console application.
- // http://www.glfw.org/docs/latest/quick.html
- #include "stdafx.h"
- #include <GLAux.h>
- #pragma comment(lib,"glaux.lib")
- using namespace std;
- GLdouble A, B, C, D;
- float t_main = 0;
- float t_list_pend = 0, t_list_move = 0, t_pend = 0;
- static float alpha;
- bool pendulum_exists = false;
- int mouse_curr_x, mouse_curr_y;
- class cell;
- class map;
- class node;
- class dll;
- class pendulum;
- map *m;
- dll *list;
- pendulum *pend;
- GLuint texture[1];
- void load_textures()
- {
- AUX_RGBImageRec *texture1;
- texture1 = auxDIBImageLoadA("tex_snoop.bmp");
- glGenTextures(1, &texture[0]);
- glBindTexture(GL_TEXTURE_2D, texture[0]);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexImage2D(GL_TEXTURE_2D, 0, 3, texture1->sizeX, texture1->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, texture1->data);
- glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
- }
- void draw_constraints(int h) {
- glLineWidth(2.0);
- glColor3f(0, 0, 0);
- int i, n = 20;
- float di = SCREEN_WIDTH / n;
- if (h > SCREEN_HEIGHT / 2) {
- glBegin(GL_POLYGON);
- glColor3f(1, 1, 1);
- glVertex2f(0, h);
- glVertex2f(0, SCREEN_HEIGHT);
- glVertex2f(SCREEN_WIDTH, SCREEN_HEIGHT);
- glVertex2f(SCREEN_WIDTH, h);
- glEnd();
- for (i = 0; i < SCREEN_WIDTH; i += (int)di) {
- glBegin(GL_LINES);
- glColor3f(0,0,0);
- glVertex2f(i, h);
- glVertex2f(i + di, h + di);
- glEnd();
- }
- }
- else {
- glBegin(GL_POLYGON);
- glColor3f(1, 1, 1);
- glVertex2f(0, -h);
- glVertex2f(0, h);
- glVertex2f(SCREEN_WIDTH, h);
- glVertex2f(SCREEN_WIDTH, -h);
- glEnd();
- for (i = 0; i < SCREEN_WIDTH; i += (int)di) {
- glBegin(GL_LINES);
- glColor3f(0,0,0);
- glVertex2f(i, h);
- glVertex2f(i - di, h - di);
- glEnd();
- }
- }
- glLineWidth(4.0);
- glBegin(GL_LINES);
- glColor3f(0,0,0);
- glVertex2f(0, h);
- glVertex2f((GLfloat)SCREEN_WIDTH, h);
- glEnd();
- }
- void draw_gears(float start_angle, float angle, int x, int y, int inner_rad, int outer_rad, int teeth_len, int teeth, float color) {
- glPushMatrix();
- glRotatef(angle + start_angle, 0, 0, 1);
- int i;
- GLfloat phi;
- float da = 2 * M_PI / teeth / 4.0;
- glBegin(GL_QUAD_STRIP);
- glColor3f(color, color, color);
- for (i = 0; i <= teeth; i++) {
- phi = i * 2.0 * M_PI / teeth;
- glVertex2f(x + inner_rad * cos(phi), y + inner_rad * sin(phi));
- glVertex2f(x + outer_rad * cos(phi), y + outer_rad * sin(phi));
- glVertex2f(x + inner_rad * cos(phi), y + inner_rad * sin(phi));
- glVertex2f(x + outer_rad * cos(phi + 3 * da), y + outer_rad * sin(phi + 3 * da));
- }
- glEnd();
- glBegin(GL_QUADS);
- da = 2.0 * M_PI / teeth / 4.0;
- for (i = 0; i < teeth; i++) {
- phi = i * 2.0 * M_PI / teeth;
- glVertex2f(x + outer_rad * cos(phi), y + outer_rad * sin(phi));
- glVertex2f(x + (outer_rad + teeth_len) * cos(phi + da), y + (outer_rad + teeth_len) * sin(phi + da));
- glVertex2f(x + (outer_rad + teeth_len) * cos(phi + 2 * da), y + (outer_rad + teeth_len) * sin(phi + 2 * da));
- glVertex2f(x + outer_rad * cos(phi + 3 * da), y + outer_rad * sin(phi + 3 * da));
- }
- glEnd();
- glPopMatrix();
- }
- void draw_permission_signal() {
- GLfloat phi = 0, theta = 0;
- int center_x = 50, center_y = ROOF_HEIGHT - 50;
- int rad = 20;
- //outer
- glBegin(GL_POLYGON);
- glColor3f(0, 0, 0);
- for (phi = 0; phi < 2 * M_PI; phi += 0.1) {
- glVertex2f(center_x + rad*cos(phi), center_y + rad*sin(phi));
- }
- glEnd();
- //inner
- rad = 18;
- glBegin(GL_POLYGON);
- glColor3f(1, 0.2, 0.2);
- for (phi = 0; phi < 2 * M_PI; phi += 0.1) {
- glVertex2f(center_x + rad*cos(phi), center_y + rad*sin(phi));
- }
- glEnd();
- rad = 14;
- glBegin(GL_POLYGON);
- glColor3f(1, 0, 0);
- for (phi = 0; phi < 2 * M_PI; phi += 0.1) {
- glVertex2f(center_x + rad*cos(phi), center_y + rad*sin(phi));
- }
- glEnd();
- }
- static void cursor_callback(GLFWwindow* window, double x, double y)
- {
- mouse_curr_x = (int)x;
- mouse_curr_y = (int)y;
- int tmpx = (int)(x / (SCREEN_WIDTH / SPLIT_X));
- int tmpy = (int)(y / (SCREEN_HEIGHT / SPLIT_Y));
- //printf("%d\t%d\t%c\n", (int)x, (int)y, m->get_cell_type(tmpx, tmpy));
- m->dispell_map();
- //m->highlight_cell(tmpx, tmpy);
- }
- static void mouse_callback(GLFWwindow* window, int button, int action, int mods)
- {
- int tmpx = (int)(mouse_curr_x / (SCREEN_WIDTH / SPLIT_X));
- int tmpy = (int)(mouse_curr_y / (SCREEN_HEIGHT / SPLIT_Y));
- if (button == GLFW_MOUSE_BUTTON_RIGHT)
- {
- if (action == GLFW_PRESS) {
- //printf("%d\t%d\t%c\n", (int)mouse_curr_x, (int)mouse_curr_y, m->get_cell_type(tmpx, tmpy));
- }
- if (action == GLFW_RELEASE) glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
- }
- if (button == GLFW_MOUSE_BUTTON_LEFT)
- {
- if (action == GLFW_PRESS) {
- //printf("M: %d\t%d\t%c(before click)\n", mouse_curr_x, mouse_curr_y, m->get_cell_type(tmpx, tmpy));
- /*
- if ('e' == m->get_cell_type(tmpx, tmpy)) {
- m->erase_cell(tmpx, tmpy);
- m->set_cell(1, tmpx, tmpy, 'b');
- return;
- }
- if ('b' == m->get_cell_type(tmpx, tmpy)) {
- m->erase_cell(tmpx, tmpy);
- m->set_cell(1, tmpx, tmpy, 'c');
- return;
- }
- if ('c' == m->get_cell_type(tmpx, tmpy)) {
- m->erase_cell(tmpx, tmpy);
- m->set_cell(1, tmpx, tmpy, 'e');
- return;
- }*/
- }
- }
- }
- static void resize_callback(GLFWwindow* window, int width, int height)
- {
- // windowWidth = width;
- // windowHeight = height;
- glViewport(0, 0, width, height);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(0.0, (GLdouble)width, 0.0, (GLdouble)height, -1, 1);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- A = width / 4.0;
- B = 0.0;
- C = D = height / 2.0;
- //printf("Reshape occured\n");
- }
- static void keyboard_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
- {
- if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
- glfwSetWindowShouldClose(window, GL_TRUE);
- if (GLFW_KEY_S == key && action == GLFW_PRESS) {
- m->validate_map();
- }
- if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) {
- //scheme
- if (list->permit_phys_move) {
- if (!list->phys_move_enabled) {
- list->phys_move_enabled = true;
- }
- else {
- list->phys_move_enabled = false;
- }
- }
- else {
- printf("physics can't be enabled at the moment. check back later.\n");
- }
- }
- }
- static void error_callback(int error, const char* description)
- {
- fputs(description, stderr);
- }
- void display(GLFWwindow* window)
- {
- float LO = 0.2;
- float HI = 0.8;
- float clr1 = LO + static_cast <float> (rand()) / (static_cast <float> (RAND_MAX / (HI - LO)));
- float clr2 = LO + static_cast <float> (rand()) / (static_cast <float> (RAND_MAX / (HI - LO)));
- float clr3 = LO + static_cast <float> (rand()) / (static_cast <float> (RAND_MAX / (HI - LO)));
- float clr4 = LO + static_cast <float> (rand()) / (static_cast <float> (RAND_MAX / (HI - LO)));
- float clr5 = LO + static_cast <float> (rand()) / (static_cast <float> (RAND_MAX / (HI - LO)));
- alpha = 0;
- while (!glfwWindowShouldClose(window))
- {
- glClearColor(0.6, 0.6, 0.6, 1);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- draw_gears(0, alpha*0.7, -1500, 1500, 200, 1500, 20, 300, 0.8);
- draw_gears(0, -alpha*0.8, 200, 900, 200, 500, 30, 30, clr1);
- draw_gears(0, alpha * 0.7, -200, 900, 60, 480, 30, 100, clr2);
- draw_gears(0, -alpha, 260, 120, 40, 90, 30, 10, clr3);
- draw_gears(400, alpha*1.2, 850, 330, 50, 200, 50, 17, clr4);
- //draw_gears(clr4*100, alpha*0.3, 850, 330, 50, 200, clr4*10, 17, clr4);
- draw_gears(500, alpha*1.1, 850, 800, 60, 200, 40, 20, clr5);
- draw_gears(clr5 * 100, alpha*clr5, 850, 800, clr4 * 10, 200, 40, clr5 * 10, clr5);
- draw_constraints(ROOF_HEIGHT);
- draw_constraints(FLOOR_HEIGHT);
- if (!list->permit_phys_move && cos(t_main) > 0) {
- draw_permission_signal();
- }
- if (glfwGetKey(window, GLFW_KEY_P) == GLFW_PRESS) {
- m->print_map();
- }
- //m->draw_map();
- if (pendulum_exists) {
- pend->draw();
- pend->move(t_pend);
- t_pend += 0.05;
- }
- list->draw_objects();
- list->draw_ropes();
- list->check_attenuation();
- list->enable_phys_pend(t_list_pend);
- list->check_collisions();
- list->enable_phys_move(t_list_move);
- if (list->phys_move_enabled) {
- t_list_move += 0.005;
- }
- if (list->phys_pend_enabled) {
- t_list_pend += 0.06;
- }
- t_main += 0.02;
- alpha += 0.03;
- glfwSwapBuffers(window);
- glfwPollEvents();
- //glfwWaitEvents();
- }
- }
- int main(int argc, _TCHAR* argv[])
- {
- if (!glfwInit()) {
- printf("glfwInit failed\n");
- return -1;
- }
- glfwWindowHint(GLFW_SAMPLES, PC_MODE);
- glfwSetErrorCallback(error_callback);
- GLFWwindow* window;
- // glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 1);
- // glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
- //glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);
- window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Anthony's Porject", NULL, NULL);
- if (window == NULL) {
- printf("glfwOpenWindow failed.\n");
- glfwTerminate();
- return -2;
- }
- int i, attrib;
- attrib = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MAJOR);
- attrib = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MINOR);
- attrib = glfwGetWindowAttrib(window, GLFW_OPENGL_PROFILE);
- glfwMakeContextCurrent(window);
- glfwSetKeyCallback(window, keyboard_callback);
- glfwSetFramebufferSizeCallback(window, resize_callback);
- glfwSetMouseButtonCallback(window, mouse_callback);
- glfwSetCursorPosCallback(window, cursor_callback);
- resize_callback(window, SCREEN_WIDTH, SCREEN_HEIGHT);
- srand(time(NULL));
- FILE *fp;
- fp = fopen("scheme.txt", "r");
- if (NULL == fp) {
- printf("cannot open file\n");
- }
- list = new dll();
- pend = new pendulum();
- m = new map();
- m->set_map(SPLIT_X, SPLIT_Y);
- printf("reading scheme\n");
- int num;
- fscanf(fp, "%d", &num);
- for (i = 0; i < num; i++) {
- char el_type[6];
- printf("type ");
- fscanf(fp, "%s", el_type);
- if (0 == strcmp(el_type, "end_scheme"))
- break;
- node *curr_node;
- int pos_x, pos_y, mass;
- int body_name, conn_left_name, conn_center_name, conn_right_name;
- if (0 == strcmp(el_type, "block") || 0 == strcmp(el_type, "b")) {
- printf("name pos_x pos_y mass ");
- fscanf(fp, "%d%d%d%d", &body_name, &pos_x, &pos_y, &mass);
- printf("connection[L C R] ");
- fscanf(fp, "%d%d%d", &conn_left_name, &conn_center_name, &conn_right_name);
- list->append_back(body_name);
- curr_node = list->get_by_id(body_name);
- curr_node->set_list_param('b', body_name, i, pos_x, pos_y, mass);
- curr_node->set_cp_ids(conn_left_name, conn_center_name, conn_right_name);
- printf("block:%d[x:%d y:%d m:%d CL:%d CC:%d CR:%d]\n\n", body_name, pos_x, pos_y, mass, conn_left_name, conn_center_name, conn_right_name);
- }
- if (0 == strcmp(el_type, "cargo") || 0 == strcmp(el_type, "c")) {
- printf("name pos_x pos_y mass ");
- fscanf(fp, "%d%d%d%d", &body_name, &pos_x, &pos_y, &mass);
- printf("connection[T B] ");
- fscanf(fp, "%d", &conn_center_name);
- list->append_back(body_name);
- curr_node = list->get_by_id(body_name);
- curr_node->set_list_param('c', body_name, i, pos_x, pos_y, mass);
- curr_node->set_cp_ids(conn_center_name);
- printf("cargo:%d[x:%d y:%d m:%d CC:%d]\n\n", body_name, pos_x, pos_y, mass, conn_center_name);
- }
- if (0 == strcmp(el_type, "pendulum") || 0 == strcmp(el_type, "p")) {
- pendulum_exists = true;
- float carrier_x, carrier_y, ticker_x, ticker_y;
- fscanf(fp, "%f%f%f%f", &carrier_x, &carrier_y, &ticker_x, &ticker_y);
- coord carr, tick;
- carr.x = carrier_x;
- carr.y = carrier_y;
- tick.x = ticker_x;
- tick.y = ticker_y;
- pend->set(carr, tick);
- printf("pendulum [CRR:%f,%f TKR:%f,%f] has been added\n\n", pend->carrier.x, pend->carrier.y, pend->curr_ticker.x, pend->curr_ticker.y);
- }
- }
- fclose(fp);
- printf("file has been closed\n");
- list->set_connection_points();
- list->set_pend_params();
- list->display_objects();
- //load_textures();
- //glEnable(GL_TEXTURE_2D);
- if (NULL != window) {
- display(window);
- }
- glfwDestroyWindow(window);
- glfwTerminate();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement