Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <list>
- #include <cmath>
- #include <algorithm>
- #include <vector>
- #include <GL/glut.h>
- #include <GL/gl.h>
- #include <GL/glu.h>
- using namespace std;
- int msec = 10;
- double scale = 0.8;
- double vx=0,vy=0;
- double p=3.14,r_1=0;
- double kx=6,ky=5;
- double angle_step = p/30/100.0 * msec;
- void render_square(double x, double y) {
- glPushMatrix();
- double dx = 0.5;
- double dy = 0.5;
- glBegin(GL_POLYGON);
- glVertex2f(x, y);
- glVertex2f(x + dx, y);
- glVertex2f(x + dx, y - dy);
- glVertex2f(x, y - dy);
- glEnd();
- glPopMatrix();
- }
- void draw_line(double x, double y, double x1, double y1) {
- glBegin(GL_LINES);
- glVertex2d(x, y);
- glVertex2d(x1, y1);
- glEnd();
- }
- class Vector{
- public:
- double x,y;
- Vector() : x(0), y(0) {}
- Vector(float a,float b):x(a),y(b){
- }
- Vector operator +(Vector other){
- return Vector(x+other.x, y+other.y);
- }
- Vector operator -(Vector other){
- return Vector(x-other.x, y-other.y);
- }
- Vector operator *(float a){
- return Vector(x*a,y*a);
- }
- Vector operator /(float a){
- return Vector(x/a,y/a);
- }
- float dot (Vector other){
- return x*other.x+y*other.y;
- }
- void print(){
- cout<<'('<<x<<','<<y<<')'<<endl;
- }
- void draw(){
- static double d = 6.0;
- Vector n1(-y, x);
- Vector n2(y, -x);
- Vector a(x,y);
- Vector ar_1 = (n1 - a*2)/d;
- Vector ar_2 = (n2 - a*2)/d;
- draw_line(0,0,x,y);
- draw_line(x, y, ar_1.x + x, ar_1.y + y);
- draw_line(x, y, ar_2.x + x, ar_2.y + y);
- }
- };
- class circle{
- public:
- double r;
- Vector vec_pos, vec_speed;
- circle(double x_, double y_, double r_, double vx_, double vy_): r(r_) {
- vec_pos.x = x_;
- vec_pos.y = y_;
- vec_speed.x = vx_ / 10000 * msec;
- vec_speed.y = vy_ / 10000 * msec;
- }
- void print_circle(){
- int n=40;
- double cx=vec_pos.x + r;
- double cy=vec_pos.y;
- double step = p/n;
- for (float f=step; f<=p*2;f+=step){
- draw_line(cx, cy ,cos(f)*r+vec_pos.x ,sin(f)*r+vec_pos.y);
- cx=vec_pos.x + cos(f)*r;
- cy=vec_pos.y + sin(f)*r;
- }
- }
- void update(){
- if(vec_pos.x + r >kx || vec_pos.x - r < -kx){
- vec_speed.x *= -1;
- }
- if(vec_pos.y + r > ky || vec_pos.y - r < -ky){
- vec_speed.y *= -1;
- }
- vec_pos.x+=vec_speed.x;
- vec_pos.y+=vec_speed.y;
- }
- };
- //Vector b(2,2);
- vector<circle> particles;
- void draw_clocks(float stop_angle){
- int j = 0;
- float coef = 0.8;
- for(float angle = 0; angle < 2 * p; angle += p/30.0, j++){
- float x = cos(angle);
- float y = sin(angle);
- if(j % 5 == 0) coef = 0.6;
- else coef = 0.8;
- draw_line(x, y, x * coef, y * coef);
- }
- }
- void modul(float a){
- if(a < 0)
- a = -a;
- }
- void colide(vector <circle> & part){
- for(int i = 0; i < part.size(); i++){
- for(int j = i + 1; j < part.size(); j++){
- Vector AB = part[i].vec_pos - part[j].vec_pos; //(part[i].vec_pos.x - part[j].vec_pos.x) * (part[i].vec_pos.x - part[j].vec_pos.x) + (part[i].vec_pos.y - part[j].vec_pos.y) * (part[i].vec_pos.y - part[j].vec_pos.y);
- double d = AB.dot(AB);
- double r = (part[i].r + part[j].r) * (part[i].r + part[j].r);
- if(d <= r){
- cout << i << " - " << j << endl;
- double dd = r - d;
- //Vector AB (part[j].vec_pos.x - part[i].vec_pos.x, part[j].vec_pos.y - part[i].vec_pos.y);
- Vector l = AB / sqrt(d);
- Vector n (-l.y, l.x);
- //part[i].vec_pos.x * (dd/2);
- float v1l = part[i].vec_speed.dot(l);
- float v1n = part[i].vec_speed.dot(n);
- float v2l = part[j].vec_speed.dot(l);
- float v2n = part[j].vec_speed.dot(n);
- part[i].vec_speed = l * v2l + n * v1n;
- part[j].vec_speed = l * v1l + n * v2n;
- }
- }
- }
- }
- void Render() {
- static double cur_angle = 1.5*p;
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glLoadIdentity();
- glTranslatef(0, 0, -10);
- glColor3d(1, 1, 1);
- glScalef(scale, scale, scale);
- for(int i = 0; i < particles.size(); i++){
- particles[i].print_circle();
- particles[i].update();
- }
- for(int i = 0; i < 5; i++){
- for(int j = 0; j < 6; j++){
- circle a(i, j, 0.4, 1*i, 2*j);
- }
- }
- colide(particles);
- //draw_clocks(cur_angle);
- //Vector b(cos(-cur_angle),sin(-cur_angle));
- // Liniya c()
- //cur_angle += angle_step;
- //if (cur_angle - 1.5 * p > 2 * p) cur_angle -= 2*p;
- draw_line(-kx,-ky,kx,-ky);
- draw_line(kx,-ky,kx,ky);
- draw_line(kx,ky,-kx,ky);
- draw_line(-kx,ky,-kx,-ky);
- //b.draw();
- //n.print_circle();
- glFlush();
- }
- void update(int t) {
- Render();
- glutTimerFunc(msec, update, 0);
- }
- void keyb(unsigned char key, int x, int y) {
- if(key == '+') {
- scale *= 1.1;
- }
- if(key == '-') {
- scale *= 0.9;
- }
- if(key == 'w') {
- kx *= 1.1;
- }
- if(key == 's') {
- kx *= 0.9;
- }
- if(key == 'd') {
- ky *= 1.1;
- }
- if(key == 'a') {
- ky *= 0.9;
- }
- Render();
- }
- void reshape(int w, int h) {
- // предупредим деление на ноль
- // если окно сильно перетянуто будет
- if(h == 0)
- h = 1;
- float ratio = 1.0* w / h;
- // используем матрицу проекции
- glMatrixMode(GL_PROJECTION);
- // Reset матрицы
- glLoadIdentity();
- // определяем окно просмотра
- glViewport(0, 0, w, h);
- // установить корректную перспективу.
- gluPerspective(45,ratio,1,1000);
- // вернуться к модели
- glMatrixMode(GL_MODELVIEW);
- }
- int main(int argc, char **argv)
- {
- particles.push_back(a);
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
- glutInitWindowSize(800, 600);
- glutCreateWindow("Physics Engine");
- glEnable(GL_DEPTH_TEST);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glClearColor(0, 0, 0.0, 0.0);
- glutTimerFunc(50, update, 0);
- glutReshapeFunc(reshape);
- glutDisplayFunc(Render);
- glutKeyboardFunc(keyb);
- glutMainLoop();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement