|
|
@ -19,6 +19,9 @@ struct Line{ |
|
|
|
vf2d startPos={}; |
|
|
|
vf2d startPos={}; |
|
|
|
vf2d endPos={}; |
|
|
|
vf2d endPos={}; |
|
|
|
float radius=0; |
|
|
|
float radius=0; |
|
|
|
|
|
|
|
float bounceFactor=0.8; |
|
|
|
|
|
|
|
float bumperLeft=0; |
|
|
|
|
|
|
|
float bumperRight=0; |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
class MiniGolfPro : public olc::PixelGameEngine |
|
|
|
class MiniGolfPro : public olc::PixelGameEngine |
|
|
@ -46,27 +49,42 @@ public: |
|
|
|
balls.emplace_back(b); |
|
|
|
balls.emplace_back(b); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void AddLine(vf2d pos1,vf2d pos2,float r){ |
|
|
|
void AddLine(vf2d pos1,vf2d pos2,float r,float bounceFactor){ |
|
|
|
Line l; |
|
|
|
Line l; |
|
|
|
l.startPos=pos1; |
|
|
|
l.startPos=pos1; |
|
|
|
l.endPos=pos2; |
|
|
|
l.endPos=pos2; |
|
|
|
l.radius=r; |
|
|
|
l.radius=r; |
|
|
|
|
|
|
|
l.bounceFactor=bounceFactor; |
|
|
|
lines.push_back(l); |
|
|
|
lines.push_back(l); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool isLeft(Line line, vf2d point) { |
|
|
|
|
|
|
|
return (line.endPos.x - line.startPos.x)*(point.y - line.startPos.y) - (line.endPos.y - line.startPos.y)*(point.x - line.startPos.x) > 0; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public: |
|
|
|
public: |
|
|
|
bool OnUserCreate() override |
|
|
|
bool OnUserCreate() override |
|
|
|
{ |
|
|
|
{ |
|
|
|
for (int i=0;i<20;i++){ |
|
|
|
for (int i=0;i<20;i++){ |
|
|
|
AddBall({float(rand()%ScreenWidth()),float(rand()%ScreenHeight())},rand()%16+2); |
|
|
|
AddBall({float(rand()%ScreenWidth()),float(rand()%ScreenHeight())},rand()%16+2); |
|
|
|
} |
|
|
|
} |
|
|
|
AddLine({12,4},{64,4},5); |
|
|
|
AddLine({0,450},{640,450},10,0.8); |
|
|
|
AddLine({76,4},{132,4},5); |
|
|
|
|
|
|
|
AddLine({12,68},{64,68},5); |
|
|
|
|
|
|
|
AddLine({76,68},{132,68},5); |
|
|
|
|
|
|
|
AddLine({4,12},{4,60},5); |
|
|
|
|
|
|
|
AddLine({140,12},{140,60},5); |
|
|
|
|
|
|
|
ConsoleCaptureStdOut(true); |
|
|
|
ConsoleCaptureStdOut(true); |
|
|
|
|
|
|
|
for(int x=0;x<320;x+=20){ |
|
|
|
|
|
|
|
AddLine({float(x+320),(1.f/18000)*std::abs(powf(float(x),2.6f))},{x+20+320.f,(1.f/18000)*std::abs(powf(float(x+20),2.6f))},10,0.4); |
|
|
|
|
|
|
|
if(x<260){ |
|
|
|
|
|
|
|
AddLine({float(-x+320),(1.f/18000)*std::abs(powf(float(x),2.6f))},{float(-x-20+320),(1.f/18000)*std::abs(powf(float(x+20),2.6f))},10,0.4); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
for(int x=0;x<260;x+=20){ |
|
|
|
|
|
|
|
AddLine({float(x+320),(1.f/12000)*std::abs(powf(float(x),2.6f))+60},{x+20+320.f,(1.f/12000)*std::abs(powf(float(x+20),2.6f))+60},10,0.4); |
|
|
|
|
|
|
|
if(x<150){ |
|
|
|
|
|
|
|
AddLine({float(-x+320),(1.f/12000)*std::abs(powf(float(x),2.6f))+60},{float(-x-20+320),(1.f/12000)*std::abs(powf(float(x+20),2.6f))+60},10,0.4); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
AddLine({640,190},{640,480},10,0.4); |
|
|
|
|
|
|
|
AddLine({580,220},{580,480},10,0.4); |
|
|
|
|
|
|
|
AddLine({60,100},{90,290},15,1.5); |
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -149,7 +167,7 @@ public: |
|
|
|
b.originalPos.x=b.pos.x; |
|
|
|
b.originalPos.x=b.pos.x; |
|
|
|
b.originalPos.y=b.pos.y; |
|
|
|
b.originalPos.y=b.pos.y; |
|
|
|
b.acc.x=-b.vel.x*0.8f; |
|
|
|
b.acc.x=-b.vel.x*0.8f; |
|
|
|
b.acc.y=-b.vel.y*0.8f/*+100.f //Gravity constant*/; |
|
|
|
b.acc.y=-b.vel.y*0.8f+100.f; //Gravity constant;
|
|
|
|
b.vel.x+=b.acc.x*b.simTimeRemaining; |
|
|
|
b.vel.x+=b.acc.x*b.simTimeRemaining; |
|
|
|
b.vel.y+=b.acc.y*b.simTimeRemaining; |
|
|
|
b.vel.y+=b.acc.y*b.simTimeRemaining; |
|
|
|
b.pos.x+=b.vel.x*b.simTimeRemaining; |
|
|
|
b.pos.x+=b.vel.x*b.simTimeRemaining; |
|
|
@ -187,7 +205,7 @@ public: |
|
|
|
if(dist<=b.radius+l.radius){ |
|
|
|
if(dist<=b.radius+l.radius){ |
|
|
|
Ball*fakeBall=new Ball(); |
|
|
|
Ball*fakeBall=new Ball(); |
|
|
|
fakeBall->radius=l.radius; |
|
|
|
fakeBall->radius=l.radius; |
|
|
|
fakeBall->mass=b.mass*0.8f; |
|
|
|
fakeBall->mass=b.mass*l.bounceFactor; |
|
|
|
fakeBall->pos={closestPoint.x,closestPoint.y}; |
|
|
|
fakeBall->pos={closestPoint.x,closestPoint.y}; |
|
|
|
fakeBall->vel={-b.vel.x,-b.vel.y}; |
|
|
|
fakeBall->vel={-b.vel.x,-b.vel.y}; |
|
|
|
fakeBalls.push_back(fakeBall); |
|
|
|
fakeBalls.push_back(fakeBall); |
|
|
@ -195,6 +213,11 @@ public: |
|
|
|
float overlap=1.1f*(dist-b.radius-fakeBall->radius); |
|
|
|
float overlap=1.1f*(dist-b.radius-fakeBall->radius); |
|
|
|
b.pos.x-=overlap*(b.pos.x-fakeBall->pos.x)/dist; |
|
|
|
b.pos.x-=overlap*(b.pos.x-fakeBall->pos.x)/dist; |
|
|
|
b.pos.y-=overlap*(b.pos.y-fakeBall->pos.y)/dist; |
|
|
|
b.pos.y-=overlap*(b.pos.y-fakeBall->pos.y)/dist; |
|
|
|
|
|
|
|
if(isLeft(l,b.pos)){ |
|
|
|
|
|
|
|
l.bumperLeft=1; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
l.bumperRight=1; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
for(Ball&b2:balls){ |
|
|
|
for(Ball&b2:balls){ |
|
|
@ -253,8 +276,10 @@ public: |
|
|
|
vf2d normal={-(l.endPos.y-l.startPos.y),l.endPos.x-l.startPos.x}; |
|
|
|
vf2d normal={-(l.endPos.y-l.startPos.y),l.endPos.x-l.startPos.x}; |
|
|
|
float dist=sqrt(normal.x*normal.x+normal.y*normal.y); |
|
|
|
float dist=sqrt(normal.x*normal.x+normal.y*normal.y); |
|
|
|
normal/=dist; |
|
|
|
normal/=dist; |
|
|
|
DrawLine(l.startPos.x+normal.x*l.radius,l.startPos.y+normal.y*l.radius,l.endPos.x+normal.x*l.radius,l.endPos.y+normal.y*l.radius); |
|
|
|
l.bumperLeft=std::max(0.f,l.bumperLeft-fElapsedTime); |
|
|
|
DrawLine(l.startPos.x-normal.x*l.radius,l.startPos.y-normal.y*l.radius,l.endPos.x-normal.x*l.radius,l.endPos.y-normal.y*l.radius); |
|
|
|
l.bumperRight=std::max(0.f,l.bumperRight-fElapsedTime); |
|
|
|
|
|
|
|
DrawLine(l.startPos.x+normal.x*l.radius,l.startPos.y+normal.y*l.radius,l.endPos.x+normal.x*l.radius,l.endPos.y+normal.y*l.radius,l.bumperLeft?RED:WHITE); |
|
|
|
|
|
|
|
DrawLine(l.startPos.x-normal.x*l.radius,l.startPos.y-normal.y*l.radius,l.endPos.x-normal.x*l.radius,l.endPos.y-normal.y*l.radius,l.bumperRight?BLUE:WHITE); |
|
|
|
} |
|
|
|
} |
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|