Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <math.h>
- #include <SDL.h>
- #include <vector>
- const int scr_w=500;
- const int scr_h=500;
- using namespace std;
- struct Vector
- {
- float x;
- float y;
- };
- float magnitude(Vector a)
- {
- float m=sqrt(a.x*a.x+a.y*a.y);
- return m;
- }
- class Display
- {
- public:
- Display(string win_name,int w,int h)
- {
- win=SDL_CreateWindow(win_name.c_str(),SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,w,h,SDL_WINDOW_SHOWN);
- ren=SDL_CreateRenderer(win,-1,SDL_RENDERER_ACCELERATED);
- }
- ~Display()
- {
- SDL_DestroyTexture(tex);
- SDL_DestroyRenderer(ren);
- SDL_DestroyWindow(win);
- }
- void initTexture(string path)
- {
- SDL_Surface* surf=NULL;
- surf=SDL_LoadBMP(path.c_str());
- if(surf==NULL)
- {
- cout<<"Error:\n"<<SDL_GetError();
- }
- tex=SDL_CreateTextureFromSurface(ren,surf);
- texSize={surf->w,surf->h};
- SDL_FreeSurface(surf);
- }
- void drawLine(SDL_Point a,SDL_Point b,SDL_Color c)
- {
- SDL_SetRenderDrawColor(ren,c.r,c.g,c.b,c.a);
- SDL_RenderDrawLine(ren,a.x,a.y,b.x,b.y);
- }
- void renderTexture(int x,int y)
- {
- SDL_Rect paste={x-texSize.x/2,y-texSize.y/2,texSize.x,texSize.y};
- SDL_RenderCopy(ren,tex,NULL,&paste);
- }
- void clearScreen(SDL_Color c)
- {
- SDL_SetRenderDrawColor(ren,c.r,c.g,c.b,c.a);
- SDL_RenderFillRect(ren,NULL);
- }
- void updateScreen()
- {
- SDL_RenderPresent(ren);
- }
- private:
- SDL_Window* win=NULL;
- SDL_Renderer* ren=NULL;
- SDL_Texture* tex=NULL;
- SDL_Point texSize;
- };
- class Softbody
- {
- public:
- Softbody(float restitution,float spring_constant,float damping)
- {
- e=restitution;
- k=spring_constant;
- c=damping;
- }
- void appendPoint(int x, int y)
- {
- SDL_Point newVertex={x,y};
- soft_body.push_back(newVertex);
- Vector zero={0,0};
- velocity.push_back(zero);
- }
- void acceleratePoints()
- {
- vector<SDL_Point> soft_body_copy=soft_body;
- vector<Vector> velocity_copy=velocity;
- for(int i=0;i<soft_body.size();++i)
- {
- for(int j=0;j<soft_body.size();++j)
- {
- if(j==i-1||j==i+1||(j==soft_body.size()-1&&i==0)||(i==soft_body.size()-1&&j==0))
- {
- Vector d={(soft_body[j].x-soft_body[i].x)/100.0,(soft_body[j].y-soft_body[i].y)/100.0};
- float t=atan2(d.y,d.x);
- float disp=abs(magnitude(d))-natural_length/100.0;
- velocity_copy[i].x+=(k*disp*cos(t))/10000.0;
- velocity_copy[i].y+=(k*disp*sin(t))/10000.0;
- velocity_copy[i].x-=c*velocity_copy[i].x/100.0;
- velocity_copy[i].y-=c*velocity_copy[i].y/100.0;
- soft_body_copy[i].x+=velocity_copy[i].x;
- soft_body_copy[i].y+=velocity_copy[i].y;
- }
- }
- soft_body=soft_body_copy;
- velocity=velocity_copy;
- }
- }
- void checkCollision()
- {
- for(int k=0;k<soft_body.size();++k)
- {
- if(soft_body[k].x>=scr_w||soft_body[k].x<=0)
- {
- velocity[k].x*=e;
- soft_body[k].x=soft_body[k].x>scr_w/2?scr_w-1:1;
- }
- if(soft_body[k].y>=scr_h||soft_body[k].y<=0)
- {
- velocity[k].y*=e;
- soft_body[k].y=soft_body[k].y>scr_h/2?scr_h-1:1;
- }
- }
- }
- void drawBody(Display& display,SDL_Color c)
- {
- for(int z=0;z<soft_body.size();++z)
- {
- display.renderTexture(soft_body[z].x,soft_body[z].y);
- display.drawLine(soft_body[z],soft_body[(z+1)%soft_body.size()],c);
- }
- }
- private:
- vector<SDL_Point> soft_body;
- vector<Vector> velocity;
- float natural_length=50;
- float e;
- float k;
- float c;
- };
- int main(int argc,char *argv[])
- {
- Softbody body(.5,1000,20);
- Display display("Soft Body Physics",scr_w,scr_h);
- display.initTexture("img/point.bmp");
- SDL_Color white={255,255,255,255};
- SDL_Color red={255,0,0,255};
- bool init=true,quit=false;
- SDL_Event e;
- while(!quit)
- {
- while(SDL_PollEvent(&e)!=0)
- {
- if(e.type==SDL_QUIT)
- {
- quit=true;
- break;
- }
- else if(init)
- {
- if(e.type==SDL_MOUSEMOTION||e.type==SDL_MOUSEBUTTONDOWN)
- {
- if(e.button.button==SDL_BUTTON_RIGHT)
- init=false;
- else if(e.button.button==SDL_BUTTON_LEFT)
- {
- int x,y;
- SDL_GetMouseState(&x,&y);
- body.appendPoint(x,y);
- }
- }
- }
- }
- if(!init)
- {
- body.checkCollision();
- body.acceleratePoints();
- }
- display.clearScreen(white);
- body.drawBody(display,red);
- display.updateScreen();
- SDL_Delay(10);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement