Line collisions and dragging added

Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
master
parent f52788aa04
commit 94d0746ed5
  1. BIN
      C++ProjectTemplate.wasm
  2. 43
      main.cpp

Binary file not shown.

@ -31,6 +31,7 @@ public:
Line*selectedLine;
std::vector<std::pair<Ball*,Ball*>>collidingPairs;
std::vector<Ball*>fakeBalls;
bool selectedLineStart=false;
MiniGolfPro()
{
@ -78,7 +79,7 @@ public:
return fabs((pos.x - point.x)*(pos.x - point.x) + (pos.y - point.y)*(pos.y - point.y)) < (r * r);
};
if (GetMouse(0).bPressed||GetMouse(1).bPressed){
if (GetMouse(0).bPressed){
selectedBall = nullptr;
for (Ball&b:balls){
if (IsPointInCircle(b.pos,b.radius,GetMousePos()))
@ -87,23 +88,43 @@ public:
break;
}
}
selectedLine=nullptr;
for(Line&l:lines){
if(IsPointInCircle(l.startPos,l.radius,GetMousePos())){
selectedLine=&l;
selectedLineStart=true;
break;
}
if(IsPointInCircle(l.endPos,l.radius,GetMousePos())){
selectedLine=&l;
selectedLineStart=false;
break;
}
}
}
if(GetMouse(0).bHeld){
if(selectedBall!=nullptr){
selectedBall->pos.x=GetMouseX();
selectedBall->pos.y=GetMouseY();
if(selectedLine!=nullptr){
if(selectedLineStart){
selectedLine->startPos=GetMousePos();
}else{
selectedLine->endPos=GetMousePos();
}
}
if(GetMouse(0).bReleased){
selectedBall=nullptr;
}
if(GetMouse(1).bReleased){
if(GetMouse(0).bReleased){
if(selectedBall!=nullptr){
selectedBall->vel.x=5*(selectedBall->pos.x-(float)GetMouseX());
selectedBall->vel.y=5*(selectedBall->pos.y-(float)GetMouseY());
selectedBall->vel.x=5*((selectedBall->pos.x)-GetMouseX());
selectedBall->vel.y=5*((selectedBall->pos.y)-GetMouseY());
}
selectedBall=nullptr;
selectedLine=nullptr;
}
if(GetMouse(1).bHeld){
for(Ball&b:balls){
b.vel.x+=(GetMouseX()-b.pos.x)*0.01f;
b.vel.y+=(GetMouseY()-b.pos.y)*0.01f;
}
}
Clear(BLACK);
@ -156,7 +177,7 @@ public:
vf2d line2={b.pos.x-l.startPos.x,b.pos.y-l.startPos.y};
float edgeLength=line1.x*line1.x+line1.y*line1.y;
float t=std::max(0.f,std::min(edgeLength,(line1.x*line2.x+line1.y*line2.y)))/edgeLength;
vf2d closestPoint={l.startPos.x+t*line1.x,l.startPos.y+t*line1.x};
vf2d closestPoint={l.startPos.x+t*line1.x,l.startPos.y+t*line1.y};
float dist=sqrtf((b.pos.x-closestPoint.x)*(b.pos.x-closestPoint.x)+(b.pos.y-closestPoint.y)*(b.pos.y-closestPoint.y));
if(dist<=b.radius+l.radius){
Ball*fakeBall=new Ball();
@ -199,7 +220,7 @@ public:
vf2d tangent={-normal.y,normal.x};
vf2d dpTangent={tangent.dot(b1->vel),tangent.dot(b2->vel)};
vf2d dpNormal={normal.dot(b1->vel),normal.dot(b2->vel)};
vf2d momentum={(dpNormal.x*(b1->mass-b2->mass)+2.f*b2->mass*dpNormal.y)/(b1->mass+b2->mass),(dpNormal.y*(b2->mass-b1->mass)+2.f*b1->mass*dpNormal.x)/(b1->mass+b2->mass)};
vf2d momentum={efficiency*(dpNormal.x*(b1->mass-b2->mass)+2.f*b2->mass*dpNormal.y)/(b1->mass+b2->mass),efficiency*(dpNormal.y*(b2->mass-b1->mass)+2.f*b1->mass*dpNormal.x)/(b1->mass+b2->mass)};
b1->vel.x=tangent.x*dpTangent.x+normal.x*momentum.x;
b1->vel.y=tangent.y*dpTangent.x+normal.y*momentum.x;
b2->vel.x=tangent.x*dpTangent.y+normal.x*momentum.y;

Loading…
Cancel
Save