@@ -19,7 +19,7 @@ struct stcBox {
1919typedef struct stcBox BOX;
2020/* *********************************************************************************************************************************************************************/
2121void addNewCannonball (LOC, LOC);
22- void checkForCollisons (void );
22+ void checkForCollisons (uint );
2323bool checkCollide (BOX, BOX);
2424void doCollision (uint, uint);
2525BOX boxMaker (LOC);
@@ -37,9 +37,9 @@ namespace Global {
3737 const float fDragCofficient = 0.47 ;
3838 const float fDensityAir = 1.2754 ; // Density of air
3939 const float fRecoil = -0.56 ;
40- const float fVelocityScalar = 1 ;
41- const float fMinVelocity = 15 ; // If a ball has less velocity than the it will "die"
42- const float fMomentumLoss = 0.89 ; // How much total momentum stays after a collisions
40+ const float fVelocityScalar = 1 ; // Changing this effects the fire velocity
41+ const float fMinVelocity = 0.005 ; // If a ball has less velocity than the it will "die"
42+ const float fMomentumLoss = 1 ; // How much total momentum stays after a collisions
4343 }
4444}
4545/* *********************************************************************************************************************************************************************/
@@ -84,6 +84,7 @@ int main(int argc, char *argv[]) {
8484 } else if ( event.type == SDL_MOUSEBUTTONDOWN ) {
8585 holding = true ;
8686 SDL_GetMouseState (&OldMouse.x , &OldMouse.y );
87+ CurrentMouse = OldMouse;
8788 if (Global::blnDebugMode) {printf (" Mouse Button down at (%d,%d)\n " ,OldMouse.x ,OldMouse.y );}
8889 } // end check of event
8990 } // end if event
@@ -93,9 +94,9 @@ int main(int argc, char *argv[]) {
9394 for (uint i = 0 ; i < DEFINED_CANNONBALL_LIMIT; i++) {
9495 if (Cannonballs[i].blnstarted ) {
9596 Cannonballs[i].update (tempdeltat);
97+ checkForCollisons (i);
9698 } // end if started
9799 } // end for loop
98- checkForCollisons ();
99100 CannonWindow.update ();
100101 } while (!quit);
101102 return 0 ;
@@ -106,17 +107,17 @@ void addNewCannonball(LOC mouseC, LOC mouseO ) {
106107 double fire_v;
107108 double angle;
108109
109- fire_v = sqrt ( pow (mouseC.x - mouseO.x , 2 ) + pow (mouseC.y - mouseO.y , 2 ) );
110- fire_v /= Global::Physics::fVelocityScalar ;
110+ fire_v = - 1 * sqrt ( pow (mouseC.x - mouseO.x , 2 ) + pow (mouseC.y - mouseO.y , 2 ) );
111+ fire_v /= ( double ) Global::Physics::fVelocityScalar ;
111112
112113 if (mouseC.x == mouseO.x ) {
113114 if (mouseC.y > mouseO.y ) {angle = -90.0 ;}
114115 else if (mouseC.y < mouseO.y ) {angle = 90.0 ;}
115116 else {angle = 0 ;}
116117 } else {
117- angle = atan ( (mouseC.y - mouseO.y ) / (mouseC.x - mouseO.x ) );
118- if (mouseC. x < mouseO. x ) { angle = - 1 * angle;}
119- angle *= (180 / 3.1415926535 );
118+ angle = ( double )- 1.0 * atan ( (mouseC.y - mouseO.y ) / (mouseC.x - mouseO.x ) );
119+ angle *= ( double )( 180 / 3.1415926535 );
120+ if (mouseC. x < mouseO. x ) { angle *= (double )- 1.0 ;}
120121 } // end if x = x
121122
122123 // mod mouse start
@@ -136,17 +137,15 @@ void addNewCannonball(LOC mouseC, LOC mouseO ) {
136137 printf (" You cannot add more cannonballs, please wait for some to decay.\n " );
137138}
138139/* *********************************************************************************************************************************************************************/
139- void checkForCollisons () { // Checks every cannonball against every other cannonball to see if they collide
140+ void checkForCollisons (uint j ) { // Checks every cannonball against every other cannonball to see if they collide
140141 BOX A, B;
141- for (uint j = 0 ; j < DEFINED_CANNONBALL_LIMIT; j++) {
142- A = boxMaker ( Cannonballs[j].getplace () );
143- for (uint i = 0 ; i < DEFINED_CANNONBALL_LIMIT; i++) {
144- if (Cannonballs[i].blnstarted && i != j) {
145- B = boxMaker ( Cannonballs[i].getplace () );
146- if ( checkCollide (A, B) ) { doCollision (j, i); }
147- } // end if started and not same ball
148- } // end for loop inner
149- } // end for loop outer
142+ A = boxMaker ( Cannonballs[j].getplace () );
143+ for (uint i = 0 ; i < DEFINED_CANNONBALL_LIMIT; i++) {
144+ if (Cannonballs[i].blnstarted && i != j) {
145+ B = boxMaker ( Cannonballs[i].getplace () );
146+ if ( checkCollide (A, B) ) { doCollision (j, i); }
147+ } // end if started and not same ball
148+ } // end for loop inner
150149}
151150/* *********************************************************************************************************************************************************************/
152151bool checkCollide (BOX A, BOX B) { // checks if two objects (made with the BOXES collide)
@@ -158,8 +157,34 @@ bool checkCollide(BOX A, BOX B) { //checks if two objects (made with the BOXES c
158157 return true ;
159158}
160159/* *********************************************************************************************************************************************************************/
161- void doCollision (uint num1, uint num2) {
160+ void doCollision (uint numA, uint numB) {
161+ dblXY Avel, Bvel, NewVel;
162+ double Amass, Bmass;
163+ Avel = Cannonballs[numA].getVelocity ();
164+ Bvel = Cannonballs[numB].getVelocity ();
165+ Amass = Cannonballs[numA].getmass ();
166+ Bmass = Cannonballs[numB].getmass ();
167+
168+ double totalMomX = Amass * Avel.x - Bmass * Bvel.x ;
169+ double totalMomY = Amass * Avel.y - Bmass * Bvel.y ;
170+ totalMomX *= (double )Global::Physics::fMomentumLoss ;
171+ totalMomY *= (double )Global::Physics::fMomentumLoss ;
172+
173+ // I know that this is not a true Momentum equation
174+ // I'm just trying to get something up to test things out.
175+ NewVel.x = totalMomX / (Amass + Bmass);
176+ NewVel.y = totalMomY / (Amass + Bmass);
177+
178+ Cannonballs[numA].setVelocity (NewVel);
179+ NewVel.y *= -1 ;
180+ NewVel.x *= -1 ;
181+ Cannonballs[numB].setVelocity (NewVel);
182+
162183
184+ /* LOC Aplace = Cannonballs[numA].getplace();
185+ Aplace.x ++;
186+ Aplace.y --;
187+ Cannonballs[numA].setplace(Aplace);*/
163188}
164189/* *********************************************************************************************************************************************************************/
165190BOX boxMaker (LOC place) {
0 commit comments