From c2214e1d3356cf1ed70300182720c19de4b2be88 Mon Sep 17 00:00:00 2001 From: "sigonasr2, Sig, Sigo" Date: Tue, 6 Jun 2023 17:24:26 +0000 Subject: [PATCH] Balls now bounce off each other Co-authored-by: sigonasr2 --- C++ProjectTemplate.wasm | Bin 307791 -> 308762 bytes main.cpp | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/C++ProjectTemplate.wasm b/C++ProjectTemplate.wasm index 4a0cc22eb5df267a62c649360f82a873b3e43cc3..ea78f01ec6f05763919c182a264a346fa7a1b453 100755 GIT binary patch delta 6099 zcmb7IeT-d26`woz-uK?V`#$b&7y8(Ju=9p(`&zf5XnDJAOS@AjrBbMgsA-oENs!=s zt)XN!Rl!?q#Ucdh-iH`zu$aaWv9WfmZ4^QiAQml9KM)c~tbz%NjiP@L&{h2X=HBjsSQIl~&OhcklkuYRX9p+0;oRfg>)hv% zPJYvwyx-a5OgrCpCMRbeE8p(;laufGlQSc|4?6Pnk7l0fi!zHYcbrHGvpd(2LW#P3 zx}+U%0-WQergF%1!k@|@{@6Kc(fSWOcWCCJb1szrCJ}_2<6jRBeE6+U22uWw^MzT~ z(*CeezEMqR5#csDq5P#nt%X+KEG5$kw<$k9w_1z5%ZtLDP;v>4Gb@|gtxItz)@4<0 zlj|ukJTOYC)SkYFtcotGV*5JoLCJ$+q0ob}Q{tA@)tQ$7D{qFuhh(BD}38lbNa* zuROOB`DXVE5_{Azy{>%jpda@3H*3;}nhUHTH`E>t^{{pR6I*oFhz$uRSMyZXWVh8a zigF-6YjPW5Pu6VTR?7oSU}of$&Ne9=QZVA(mUbfca-AMKg}I$@Zl`~4r!u!w?Q~p| zYt~%V1EPCWK0!YN+q-6@dR^miVlD#JIhC<9lg%CmoH+ZVr6ytNI0RwM>Efl&o!IbX zTbIUUs3cCg01tt$=yEy`jsZTYimIfX_Ovcq>{VHXwu@!(5sW(4nZ&eSff;0RI#qbn zliO0$G)I;74vO zN*t#O2r@_!LZr6P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`iprqW$s9Q^1geZ5Gf0@FK{AkZNs3@%7GW0v{i}l{DWNjxd+!zl-54#P!I4`fyts(XtQE_sHp_t;BwN$ zPku3eS?mKta<&1!&RWsEw6P37pe&jkz)l=@h=n~yGpv_7>!?Sx9^Hk1YMp$PTv7-X zZa*rWeppZibE$bhjBcIyl<26s(VxW#J-}Am(HT6m=T8aEo6Hc;6mbIZ+A6llUO#zI z4}8V99_TzEvb~HUSqvH2!(|X^>FQ#(sJJ~kq`3l3@>hMsa4$R$?RwJ3MvwVzz=Z{P zqTo%0XNa+)C;Ij(xnl92P1_yMICSy1n*)&!-`Lvg@EwaET>nT9z+F3T=Kz1W`8H9Y zt;_$5>u)>WA^FE0hj>c;?47T6uy=lI;|h!2b7=Ypho(9H#qmSaPP5H-96$S^Gw1>4 RK6d_C=Qkfe-RoX=@V~mPPz(S7 diff --git a/main.cpp b/main.cpp index b09fb87..f6e4f00 100644 --- a/main.cpp +++ b/main.cpp @@ -20,6 +20,7 @@ class MiniGolfPro : public olc::PixelGameEngine public: std::vectorballs; Ball*selectedBall; + std::vector>collidingPairs; MiniGolfPro() { @@ -108,6 +109,39 @@ public: b.vel.y=0; } } + + for(Ball&b:balls){ + for(Ball&b2:balls){ + if(&b!=&b2){ + if(DoCirclesOverlap(b.pos,b.radius,b2.pos,b2.radius)){ + collidingPairs.push_back({&b,&b2}); + float dist=sqrtf((b.pos.x-b2.pos.x)*(b.pos.x-b2.pos.x)+(b.pos.y-b2.pos.y)*(b.pos.y-b2.pos.y)); + float overlap=0.5f*(dist-b.radius-b2.radius); + b.pos.x-=overlap*(b.pos.x-b2.pos.x)/dist; + b.pos.y-=overlap*(b.pos.y-b2.pos.y)/dist; + b2.pos.x+=overlap*(b.pos.x-b2.pos.x)/dist; + b2.pos.y+=overlap*(b.pos.y-b2.pos.y)/dist; + } + } + } + } + + for(std::pair&pair:collidingPairs){ + Ball*b1=pair.first; + Ball*b2=pair.second; + float dist=sqrtf((b1->pos.x-b2->pos.x)*(b1->pos.x-b2->pos.x)+(b1->pos.y-b2->pos.y)*(b1->pos.y-b2->pos.y)); + vf2d normal={(b2->pos.x-b1->pos.x)/dist,(b2->pos.y-b1->pos.y)/dist}; + 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)}; + 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; + b2->vel.y=tangent.y*dpTangent.y+normal.y*momentum.y; + } + + collidingPairs.clear(); for(Ball&b:balls){ if(selectedBall==&b){