Skip to content

Commit 8352ceb

Browse files
author
Patrick Rye
committed
Update to v1.1.7-beta
15 September 2015 UNFINISHED Released version 1.1.7-Beta Change log: -Added: Collisons -Fixed: Bug with mouse keeping old coordinates -Fixed: X Fire velocity not being right Still have some more work to go to get the collinsons completely working.
1 parent 010cd5d commit 8352ceb

File tree

6 files changed

+77
-23
lines changed

6 files changed

+77
-23
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,4 @@ Cannon
5858
Cannon-PRIVATE
5959

6060
/RandomCrap/
61+
/SDL2Stuff/

ChangesLog.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
Change log:
55
-Added: Collisons
6+
-Fixed: Bug with mouse keeping old coordinates
7+
-Fixed: X Fire velocity not being right
68

79
15 September 2015
810
Released version 1.1.6-Beta

project/SDL-Cannon-Simulation.cbp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,11 @@
111111
<Compiler>
112112
<Add option="-std=c++11" />
113113
<Add directory="C:/MinGW/SDL2-2.0.4/i686-w64-mingw32/include" />
114+
<Add directory="../SDL2Stuff/include" />
114115
</Compiler>
115116
<Linker>
116117
<Add directory="C:/MinGW/SDL2-2.0.4/i686-w64-mingw32/lib" />
118+
<Add directory="../SDL2Stuff/lib" />
117119
</Linker>
118120
<Unit filename="../README.md">
119121
<Option target="Debug-Windows" />

src/cannonball.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,10 @@ void clsCannonball::update(double newdeltat) {
9090

9191
double total_v;
9292
total_v = sqrt( pow(vel.x,2) + pow(vel.y,2) );
93-
if (total_v < Global::Physics::fMinVelocity) {blnstarted = false;}
93+
if (total_v < Global::Physics::fMinVelocity) {
94+
blnstarted = false;
95+
if (Global::blnDebugMode) {printf("Ball moving too slow; killing it\n");}
96+
}
9497
show();
9598
}
9699
/**********************************************************************************************************************************************/
@@ -127,4 +130,21 @@ LOC clsCannonball::getplace() {
127130
return place;
128131
}
129132
/**********************************************************************************************************************************************/
130-
133+
dblXY clsCannonball::getVelocity() {
134+
return vel;
135+
}
136+
/**********************************************************************************************************************************************/
137+
void clsCannonball::setVelocity(dblXY newvel) {
138+
vel.x = newvel.x;
139+
vel.y = newvel.y;
140+
}
141+
/**********************************************************************************************************************************************/
142+
double clsCannonball::getmass() {
143+
return props.mass;
144+
}
145+
/**********************************************************************************************************************************************/
146+
void clsCannonball::setplace(LOC newplace) {
147+
dblLOC.x = newplace.x;
148+
dblLOC.y = newplace.y;
149+
}
150+
/**********************************************************************************************************************************************/

src/cannonball.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,14 @@ class clsCannonball {
3131
bool blnstarted;
3232
void setValues(double, LOC, double, double);
3333
LOC getplace(void);
34+
void setplace(LOC);
3435

3536
void enableDrag(void);
3637
void update(double);
3738
void setSDLScreen(SDL_Texture*, WINATT);
39+
dblXY getVelocity(void);
40+
void setVelocity(dblXY);
41+
double getmass(void);
3842
private:
3943
bool blnDragEnabled;
4044

src/main.cpp

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ struct stcBox {
1919
typedef struct stcBox BOX;
2020
/**********************************************************************************************************************************************************************/
2121
void addNewCannonball(LOC, LOC);
22-
void checkForCollisons(void);
22+
void checkForCollisons(uint);
2323
bool checkCollide(BOX, BOX);
2424
void doCollision(uint, uint);
2525
BOX 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
/**********************************************************************************************************************************************************************/
152151
bool 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
/**********************************************************************************************************************************************************************/
165190
BOX boxMaker(LOC place) {

0 commit comments

Comments
 (0)