Skip to content

Commit c833113

Browse files
author
Patrick Rye
committed
Updateto v1.2.2-Beta
18 September 2015 Released version 1.2.2-Beta Change log: -Fixed: Mouse line getting thinner the closer to vertical it gets -Cleaned: Up a lot of code; made things simpler -Changed: All angles are now in radians so I don't have to worry about converting -Changed: Ball radius (and therefore mass) is now proportional to the amount of time you held down the mouse button for. -Changed: Upped default allowable number of cannonballs from 10 to 20
1 parent 434bbc1 commit c833113

File tree

9 files changed

+111
-122
lines changed

9 files changed

+111
-122
lines changed

ChangesLog.txt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
18 September 2015
2+
Released version 1.2.2-Beta
3+
4+
Change log:
5+
-Fixed: Mouse line getting thinner the closer to vertical it gets
6+
-Cleaned: Up a lot of code; made things simpler
7+
-Changed: All angles are now in radians so I don't have to worry about converting
8+
-Changed: Ball radius (and therefore mass) is now proportional to the amount of time you held down the mouse button for.
9+
-Changed: Upped default allowable number of cannonballs from 10 to 20
10+
111
17 September 2015
212
Released version 1.2.1-Beta
313

@@ -16,7 +26,7 @@
1626
-Changed: Default Min Velocity from 0.5 to 0.0 (basically disabling it)
1727
-Changed: Disabled the Unrealistic method I added in 1.1.9-beta as it did not help.
1828
-Fixed: Drag update acc would sometimes divide by 0
19-
-Changed: Default Collision Method from Elastic to Ineleastic
29+
-Changed: Default Collision Method from Elastic to Inelastic
2030
-Changed: fMomentumLoss name to fCoefficientRestitution to be named after its real life counter part
2131
-Added: Friction between the ball and floor / wall
2232

src/cannonball.cpp

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
#include "cannonball.h"
33
#include "screen.h"
44
/**********************************************************************************************************************************************/
5-
//Constants
6-
const double Rad_Convert = PI / 180.0;
7-
/**********************************************************************************************************************************************/
85
clsCannonball::clsCannonball() {
96
//Put some default values in; on the off chance I forget to set them later
107
blnDragEnabled = false;
@@ -47,27 +44,23 @@ void clsCannonball::Drag_updateacc(void) {
4744
double Drag_Acc = (double) (Drag_Force / props.mass);
4845
double angle;
4946

50-
if (vel.x != 0.0) {angle = atan(vel.y / vel.x);}
51-
else {angle = PI / 2.0;}
52-
53-
if (vel.x < 0.0) {angle += PI;}
47+
angle = (vel.x != 0.0) ? atan(vel.y / vel.x) : PI / 2.0;
48+
angle += vel.x < 0.0 ? PI : 0.0;
5449

5550
acc.x += Drag_Acc * cos (angle);
5651
acc.y += Drag_Acc * sin (angle);
57-
58-
double Friction = (double)Global::Physics::fKineticFriction * (double)-1.0 * (double)Global::Physics::fGravity;
52+
//Please recall fGravity is negative
53+
double Friction = (double)Global::Physics::fKineticFriction * (double)Global::Physics::fGravity;
5954

6055
//Update acc for Friction values
6156
if ( dblLOC.y <= Screen_place.h || dblLOC.y >= window.height ) {
6257
//Ball is in contact with floor or ceiling update x acc
63-
if (vel.x < 0.0){acc.x += Friction;}
64-
else {acc.x -= Friction;}
58+
acc.x += Friction * (vel.x < 0.0 ? 1.0 : -1.0);
6559
}
6660

6761
if ( dblLOC.x <= 0.0 || dblLOC.x >= window.width - Screen_place.w ) {
6862
//Ball is in contact with the wall update y acc.
69-
if (vel.y < 0.0) {acc.y += Friction;}
70-
else {acc.y -= Friction;}
63+
acc.y += Friction * (vel.y < 0.0 ? 1.0 : -1.0);
7164
}
7265
} //end if things don't equal 0
7366
}
@@ -81,23 +74,18 @@ void clsCannonball::update(double newdeltat) {
8174
vel.x = (vel.x + acc.x * deltat);
8275
if (dblLOC.x <= 0.0 || dblLOC.x >= window.width - Screen_place.w) {
8376
vel.x *= Global::Physics::fRecoil;
84-
if (dblLOC.x <= 0.0) {dblLOC.x++;}
85-
else {dblLOC.x--;}
77+
dblLOC.x += dblLOC.x <= 0.0 ? 1.0 : -1.0;
8678
} //end if hitting x edges
8779

8880
dblLOC.y = dblLOC.y + vel.y * deltat + 0.5 * acc.y * pow(deltat,2);
8981
vel.y = (vel.y + acc.y * deltat);
9082
if (dblLOC.y <= Screen_place.h || dblLOC.y >= window.height) {
9183
vel.y *= Global::Physics::fRecoil;
92-
if (dblLOC.y <= Screen_place.h) {dblLOC.y++;}
93-
else {dblLOC.y--;}
84+
dblLOC.y += dblLOC.y <= Screen_place.y ? 1.0 : -1.0;
9485
}//end if hitting y edges
9586

96-
if (dblLOC.x < 0.0) {place.x = 0; dblLOC.x = 0;}
97-
else {place.x = round(dblLOC.x);}
98-
99-
if (dblLOC.y < 0.0) {place.y = 0; dblLOC.y = 0;}
100-
else {place.y = round(dblLOC.y);}
87+
place.x = dblLOC.x < 0.0 ? 0 : round(dblLOC.x);
88+
place.y = dblLOC.y < 0.0 ? 0 : round(dblLOC.y);
10189

10290
if (Global::Config.values.blnLogging) {
10391
FILE* logfile = fopen("logfile.log","a");
@@ -146,8 +134,8 @@ void clsCannonball::setValues(double r, LOC init_place, double init_vel, double
146134
dblLOC.x = (double) place.x;
147135
dblLOC.y = (double) place.y;
148136

149-
vel.x = (double)(init_vel) * (cos(init_angle * Rad_Convert));
150-
vel.y = (double)(init_vel) * (sin(init_angle * Rad_Convert));
137+
vel.x = (double)(init_vel) * (cos(init_angle));
138+
vel.y = (double)(init_vel) * (sin(init_angle));
151139

152140
ballID = ID;
153141
blnstarted = true;

src/config.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ clsConfig::clsConfig() {
4040
bool clsConfig::exists(void) {
4141
//Returns true or false if config file exists
4242
FILE* pTempFile = fopen(FileName, "r");
43-
if (pTempFile == NULL) {return false;}
44-
else {return true;}
43+
return (pTempFile != NULL);
4544
}
4645
/**********************************************************************************************************************************************/
4746
void clsConfig::make(void) {
@@ -55,8 +54,8 @@ void clsConfig::make(void) {
5554
//this way I only have to change the defaults in one place
5655
fprintf(configFile,"Screen Width: %u\n",values.uintScreenWidth);
5756
fprintf(configFile,"Screen Height: %u\n",values.uintScreenHeight);
58-
fprintf(configFile,"Log Ball's path: 0\n");
59-
fprintf(configFile,"Enable Drag Mode (experimental): 0\n");
57+
fprintf(configFile,"Log Ball's path: %u\n", (values.blnLogging ? 1 : 0) ) ;
58+
fprintf(configFile,"Enable Drag Mode (experimental): %u\n", (values.blnDragMode ? 1 : 0) );
6059
fclose(configFile);
6160
}
6261
/**********************************************************************************************************************************************/
@@ -82,15 +81,13 @@ void clsConfig::load(void) {
8281
intValuesScanned = sscanf(chrTempString, "%*s %*s %*s %d",&intTempBool);
8382
if (intValuesScanned < 1) {printf("ERROR!"); intTempBool = 0;}
8483
if(Global::blnDebugMode) {printf("Log Path \t %d\n",intTempBool);}
85-
if(intTempBool == 1) {values.blnLogging = true;}
86-
else {values.blnLogging = false;}
84+
values.blnLogging = (intTempBool == 1);
8785

8886
fgets(chrTempString,50,configFile);
8987
intValuesScanned = sscanf(chrTempString, "%*s %*s %*s %*s %d",&intTempBool);
9088
if (intValuesScanned < 1) {printf("ERROR!"); intTempBool = 0;}
9189
if(Global::blnDebugMode) {printf("Enable Drag \t %d\n",intTempBool);}
92-
if(intTempBool == 1) {values.blnDragMode = true;}
93-
else {values.blnDragMode = false;}
90+
values.blnDragMode = (intTempBool == 1);
9491

9592
fclose(configFile);
9693
printf("\n\n");
@@ -116,7 +113,7 @@ char clsConfig::verisonCheck(const char *ConfigVerison) {
116113
void clsConfig::Check(void) {
117114
char chrTempString[50], chrConfigVerison;
118115

119-
if (exists() != true) {
116+
if ( !exists() ) {
120117
printf("Config file was not found; creating now one\n");
121118
make();
122119
} else {

src/global.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ namespace Global {
2222
extern const float fVelocityScalar;
2323
extern const float fMinVelocity;
2424
extern const float fCoefficientRestitution;
25+
extern const float fTimetoSizeRatio;
2526
extern const uchar CollisionMethod;
2627
}
2728
}

src/main.cpp

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/* TODO (GamerMan7799#4#): Show path of each ball on the screen */
2+
/* FIXME (GamerMan7799#1#): The balls will become stuck together for seemingly random reasons */
3+
/* TODO (GamerMan7799#1#): Balls are different colors based on their IDS */
4+
/* TODO (GamerMan7799#1#): Balls are more transparent the less mass it has. */
15
/**********************************************************************************************************************************************************************/
26
#include <SDL2/SDL.h>
37
#include <cstdio>
@@ -10,7 +14,7 @@
1014
#include "tick.h"
1115
#include "config.h"
1216
/**********************************************************************************************************************************************************************/
13-
void addNewCannonball(LOC, LOC);
17+
void addNewCannonball(LOC, LOC, double);
1418
void checkForCollisons(uint);
1519
bool checkCollide(BOX, BOX);
1620
void doCollision(uint, uint);
@@ -36,17 +40,18 @@ namespace Global {
3640
//Friction values based on Concrete and Steel
3741
const float fKineticFriction = 0.36;
3842
const float fDensityAir = 1.2754; //Density of air
39-
const float fRecoil = -0.56; //the mulitplier of the velocity when a ball hits the walls / floor
43+
const float fRecoil = -0.56; //the multiplier of the velocity when a ball hits the walls / floor
4044
const float fVelocityScalar = 1; //Changing this effects the fire velocity
4145
const float fMinVelocity = 0.0; //If a ball has less velocity than the it will "die"
4246
const float fCoefficientRestitution = 0.76; //How much total energy remains after a collision
4347
//(see https://en.wikipedia.org/wiki/Coefficient_of_restitution for more info
48+
const float fTimetoSizeRatio = 1.2458; //One second of holding down the mouse button = this many meters for the ball
4449
const uchar CollisionMethod = CollideInelastic;
4550
}
4651
}
4752
/**********************************************************************************************************************************************************************/
4853
//This is the maximum number of cannonballs which can be "alive" at a time
49-
#define DEFINED_CANNONBALL_LIMIT 10
54+
#define DEFINED_CANNONBALL_LIMIT 20
5055
/**********************************************************************************************************************************************************************/
5156
clsCannonball Cannonballs[DEFINED_CANNONBALL_LIMIT];
5257
/**********************************************************************************************************************************************************************/
@@ -79,21 +84,22 @@ int main(int argc, char *argv[]) {
7984
else if ( event.type == SDL_MOUSEBUTTONUP ) {
8085
holding = false;
8186
if (Global::blnDebugMode) {printf("Mouse button up\n");}
82-
addNewCannonball(CurrentMouse, OldMouse);
87+
addNewCannonball(CurrentMouse, OldMouse, Tick.stopHolding() );
8388
} else if ( event.type == SDL_MOUSEMOTION && holding) {
8489
//Draw the line
8590
SDL_GetMouseState(&CurrentMouse.x, &CurrentMouse.y );
8691
if (Global::blnDebugMode) {printf("Mouse found at (%d,%d)\n",CurrentMouse.x,CurrentMouse.y);}
8792
} else if ( event.type == SDL_MOUSEBUTTONDOWN ) {
8893
holding = true;
94+
Tick.startHolding();
8995
SDL_GetMouseState(&OldMouse.x, &OldMouse.y);
9096
CurrentMouse = OldMouse;
9197
if (Global::blnDebugMode) {printf("Mouse Button down at (%d,%d)\n",OldMouse.x,OldMouse.y);}
9298
} else if ( event.type == SDL_KEYDOWN ) {
9399
switch ( event.key.keysym.sym ) {
94100
case SDLK_k:
95101
//kill all the balls
96-
for (uint i = 0; i < DEFINED_CANNONBALL_LIMIT; i++) {Cannonballs[i].blnstarted = false;}
102+
for (uint i = 0; i < DEFINED_CANNONBALL_LIMIT; i++) { Cannonballs[i].blnstarted = false; }
97103
break;
98104
case SDLK_q:
99105
case SDLK_ESCAPE:
@@ -103,8 +109,7 @@ int main(int argc, char *argv[]) {
103109
case SDLK_r:
104110
//stop all motion of balls
105111
dblXY StopVel;
106-
StopVel.x = 0.0;
107-
StopVel.y = 0.0;
112+
StopVel.x = StopVel.y = 0.0;
108113
for (uint i = 0; i < DEFINED_CANNONBALL_LIMIT; i++) { Cannonballs[i].setVelocity(StopVel); }
109114
break;
110115
} //end switch key
@@ -113,7 +118,6 @@ int main(int argc, char *argv[]) {
113118
if (holding) {CannonWindow.drawline(CurrentMouse, OldMouse);}
114119
//Update every ball
115120
tempdeltat = Tick.getTimeDifference();
116-
117121
for (uint i = 0; i < DEFINED_CANNONBALL_LIMIT; i++) { //Loop through each cannonball
118122
if (Cannonballs[i].blnstarted) { //only update cannonball if it is "alive"
119123
//Check for collisions if no collide is not on
@@ -123,29 +127,26 @@ int main(int argc, char *argv[]) {
123127
} //end for loop
124128

125129
CannonWindow.update(); //Update the screen
126-
} while (!quit);
130+
} while (!quit); //keep looping until we get a quit
127131
return 0;
128132
}
129133
/**********************************************************************************************************************************************************************/
130-
void addNewCannonball(LOC mouseC, LOC mouseO ) {
134+
void addNewCannonball(LOC mouseC, LOC mouseO, double HoldTime ) {
131135
//Get location, vel, and angle
132136
double fire_v;
133137
double angle;
138+
double radius = (double)Global::Physics::fTimetoSizeRatio * HoldTime;
134139

135140
//Because y increasing is going down according to SDL we first negative the velocity
136141
fire_v = -1 * sqrt( pow(mouseC.x - mouseO.x, 2) + pow(mouseC.y - mouseO.y, 2) );
137142
fire_v /= (double) Global::Physics::fVelocityScalar;
138143

139144
//if the mouse if pointing straight up or straight down make the angle 90
140-
//Other calculate the angle with atan.
145+
//Otherwise calculate the angle with atan.
141146
if (mouseC.x == mouseO.x) {
142-
if (mouseC.y > mouseO.y) {angle = -90.0;}
143-
else if (mouseC.y < mouseO.y) {angle = 90.0;}
144-
else {angle = 0.0;}
147+
angle = (PI / 2.0) * ( (mouseC.y > mouseO.y) ? -1.0 : 1.0 );
145148
} else {
146-
angle = (double)-1.0 * atan( (mouseC.y - mouseO.y) / (mouseC.x - mouseO.x) );
147-
if (mouseC.x < mouseO.x) {angle += (double)PI;}
148-
angle *= (double)(180.0/PI);
149+
angle = (double) -1.0 * atan( (double)(mouseC.y - mouseO.y) / (double)(mouseC.x - mouseO.x) ) + (double)( (mouseC.x < mouseO.x) ? PI : 0.0 );
149150
} //end if x = x
150151

151152
//mod mouse start, once again because the top left is 0,0 to SDL
@@ -154,12 +155,12 @@ void addNewCannonball(LOC mouseC, LOC mouseO ) {
154155
//loop through array to find next available cannonball slot
155156
for (uint i = 0; i < DEFINED_CANNONBALL_LIMIT; i++) {
156157
if (!Cannonballs[i].blnstarted) {
157-
Cannonballs[i].setValues(2.0, mouseO, fire_v, angle, i);
158+
Cannonballs[i].setValues(radius, mouseO, fire_v, angle, i);
158159
return;
159160
} //end if not started
160161
} //end for cannonballs.
161162

162-
//since the program would have exited this function if a spot was open
163+
//Since the program would have exited this function if a spot was open
163164
//below we tell the use they have to wait
164165
printf("You cannot add more cannonballs, please wait for some to decay.\n");
165166
}
@@ -200,11 +201,6 @@ void doCollision(uint numA, uint numB) {
200201
CenterA = Cannonballs[numA].getplace();
201202
CenterB = Cannonballs[numB].getplace();
202203
203-
CenterA.x += 5;
204-
CenterA.y += 5;
205-
CenterB.x += 5;
206-
CenterB.y += 5;
207-
208204
DeltaCenters.x = abs(CenterA.x - CenterB.x);
209205
DeltaCenters.y = abs(CenterA.y - CenterB.y);
210206
//Since it is r^2 and the sqrt of this give us r, we just drop the sqrt part to save time
@@ -234,23 +230,19 @@ void doCollision(uint numA, uint numB) {
234230
//get the angle for both A and B
235231
if (Avel.x != 0.0) {Aangle = atan(Avel.y/Avel.x);}
236232
else {
237-
if (Avel.y > 0.0) {Aangle = PI / 2.0;}
238-
else if (Avel.y < 0.0) {Aangle = - PI / 2.0;}
239-
else {Aangle = 0.0;}
233+
Aangle = (Avel.y >= 0.0) ? PI / 2.0 : -PI / 2.0;
240234
}
241235

242236
if (Bvel.x != 0.0) {Bangle = atan(Bvel.y/Bvel.x);}
243237
else {
244-
if (Bvel.y > 0.0) {Bangle = PI / 2.0;}
245-
else if (Bvel.y < 0.0) {Bangle = - PI / 2.0;}
246-
else {Bangle = 0.0;}
238+
Bangle = (Bvel.y >= 0.0) ? PI / 2.0 : -PI / 2.0;
247239
}
248240

249241
//Adjust the angle to be the right one.
250242
//Since atan will only yield a number between -PI/2 and PI/2 we
251243
//have to adjust it if xvel is negative.
252-
if (Avel.x < 0.0) {Aangle += PI;}
253-
if (Bvel.x < 0.0) {Bangle += PI;}
244+
Aangle += Avel.x < 0.0 ? PI : 0;
245+
Bangle += Bvel.x < 0.0 ? PI : 0;
254246
//The contact angle has to be the difference between the two angles but
255247
//since sometimes one or the other is negative, we'll use abs to ensure the right number
256248
ContactAngle = abs ( abs(Aangle) - abs(Bangle) );

0 commit comments

Comments
 (0)