Skip to content

Commit 0265ddc

Browse files
author
Patrick Rye
committed
Update to v1.2.0-beta
17 September 2015 Released version 1.2.0-Beta Change log: -Fixed: The Collision equations a bit more -Added: Cannonball now holds its own box -Changed: Attempted to make the code more efficient -Added: Keyboard Press events, (Q/ESC to quit, K to kill all balls, and R to stop all motion) -Updated: Readme -Changed: Default Min Velocity from 0.5 to 0.0 (basically disabling it) -Changed: Disabled the Unrealistic method I added in 1.1.9-beta as it did not help. -Fixed: Drag update acc would sometimes divide by 0 -Changed: Default Collision Method from Elastic to Ineleastic -Changed: fMomentumLoss name to fCoefficientRestitution to be named after its real life counter part -Added: Friction between the ball and floor / wall
1 parent d5c1bac commit 0265ddc

File tree

8 files changed

+176
-79
lines changed

8 files changed

+176
-79
lines changed

ChangesLog.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
17 September 2015
2+
Released version 1.2.0-Beta
3+
4+
Change log:
5+
-Fixed: The Collision equations a bit more
6+
-Added: Cannonball now holds its own box
7+
-Changed: Attempted to make the code more efficient
8+
-Added: Keyboard Press events, (Q/ESC to quit, K to kill all balls, and R to stop all motion)
9+
-Updated: Readme
10+
-Changed: Default Min Velocity from 0.5 to 0.0 (basically disabling it)
11+
-Changed: Disabled the Unrealistic method I added in 1.1.9-beta as it did not help.
12+
-Fixed: Drag update acc would sometimes divide by 0
13+
-Changed: Default Collision Method from Elastic to Ineleastic
14+
-Changed: fMomentumLoss name to fCoefficientRestitution to be named after its real life counter part
15+
-Added: Friction between the ball and floor / wall
16+
17+
118
16 September 2015
219
Released version 1.1.9-Beta
320

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,35 @@ FolderName\SDL2_image.dll
2828
```
2929

3030
Let me know if I am missing a dependences that I might have forgotten about due to my own system set up.
31+
32+
# Using the Program
33+
34+
The first time you run the program a Config.ini will be created it should look something like below:
35+
36+
```
37+
Config File for the Cannon.exe
38+
1.2.0.25-b
39+
Screen Width: 640
40+
Screen Height: 480
41+
Log Ball's path: 0
42+
Enable Drag Mode (experimental): 0
43+
```
44+
45+
You can use this to change things like the screen height and width, and other stuff.
46+
47+
"Log Ball's Path" (if enabled with a 1 instead of 0), will cause a a file called "logfile.log", which will contain the placement of every ball on every update (it gets quite big as you can guess).
48+
49+
"Enable drag Mode" (if enabled with a 1 instead of 0), will cause the cannonballs to be effected by drag. It is listed as experimental because I based the equations used off of several things I found
50+
online and I cannot verify how accuate it is.
51+
52+
Once you start the program you will be greeted with a beautiful white screen. To create a new cannonball you click (and hold down the button)
53+
you can now move your move around to change the angle that the new cannonball will be fired at; as well as the longer the line the faster it will go.
54+
When you are happy with the angle and speed simply release the mouse button and it will be created at the point where you clicked the mouse button down.
55+
56+
You can also press the following keys to yield effects
57+
58+
```
59+
K = All Balls will be "killed".
60+
R = All motion will be stopped.
61+
Q / ESC = Program will exit.
62+
```

project/SDL-Cannon-Simulation.cbp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@
144144
<Unit filename="../src/version.h" />
145145
<Extensions>
146146
<AutoVersioning>
147-
<Scheme minor_max="10" build_max="999" rev_max="999" rev_rand_max="1" build_times_to_increment_minor="1000" />
147+
<Scheme minor_max="10" build_max="10" rev_max="999" rev_rand_max="1" build_times_to_increment_minor="1000" />
148148
<Settings autoincrement="1" date_declarations="1" use_define="1" update_manifest="0" do_auto_increment="1" ask_to_increment="1" language="C" svn="0" svn_directory="" header_path="..\src\version.h" />
149149
<Changes_Log show_changes_editor="1" app_title="Released version %M.%m.%b-%T" changeslog_path="..\ChangesLog.txt" />
150150
<Code header_guard="__VERSION_HEADER__" namespace="Version" prefix="DEFINED_VER" />

src/cannonball.cpp

Lines changed: 54 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -40,52 +40,78 @@ void clsCannonball::Drag_calcvalues(void) {
4040
}
4141
/**********************************************************************************************************************************************/
4242
void clsCannonball::Drag_updateacc(void) {
43-
double flow_velocity = sqrt(pow(vel.x,2) + pow(vel.y,2));
44-
double Drag_Force = (double) (0.5 * Global::Physics::fDensityAir * flow_velocity * Global::Physics::fDragCofficient * props.area);
45-
double Drag_Acc = (double) (Drag_Force / props.mass);
46-
47-
double angle = atan(vel.y / vel.x);
48-
49-
acc.x -= Drag_Acc * cos (angle);
50-
if (vel.y < 0) {acc.y += Drag_Acc * sin (angle);}
51-
else {acc.y -= Drag_Acc * sin (angle);}
43+
acc.x = 0.0;
44+
acc.y = Global::Physics::fGravity;
45+
if (vel.x != 0.0 && vel.y != 0.0 && props.mass != 0.0) {
46+
double flow_velocity = sqrt(pow(vel.x,2) + pow(vel.y,2));
47+
double Drag_Force = (double) (0.5 * Global::Physics::fDensityAir * flow_velocity * Global::Physics::fDragCofficient * props.area);
48+
double Drag_Acc = (double) (Drag_Force / props.mass);
49+
double angle;
50+
51+
if (vel.x != 0.0) {angle = atan(vel.y / vel.x);}
52+
else {angle = PI / 2.0;}
53+
54+
if (vel.x < 0.0) {angle += PI;}
55+
56+
acc.x += Drag_Acc * cos (angle);
57+
acc.y += Drag_Acc * sin (angle);
58+
59+
double Friction = (double)Global::Physics::fKineticFriction * (double)-1.0 * (double)Global::Physics::fGravity;
60+
61+
//Update acc for Friction values
62+
if ( dblLOC.y <= Screen_place.h || dblLOC.y >= window.height ) {
63+
//Ball is in contact with floor or ceiling update x acc
64+
if (vel.x < 0.0){acc.x += Friction;}
65+
else {acc.x -= Friction;}
66+
}
67+
68+
if ( dblLOC.x <= 0.0 || dblLOC.x >= window.width - Screen_place.w ) {
69+
//Ball is in contact with the wall update y acc.
70+
if (vel.y < 0.0) {acc.y += Friction;}
71+
else {acc.y -= Friction;}
72+
}
73+
} //end if things don't equal 0
5274
}
5375
/**********************************************************************************************************************************************/
5476
void clsCannonball::update(double newdeltat) {
5577
deltat = newdeltat;
56-
SDL_Rect dst;
57-
SDL_QueryTexture(ball,NULL,NULL, &dst.w, &dst.h);
5878

5979
if (blnDragEnabled) {Drag_updateacc();}
6080

6181
dblLOC.x = dblLOC.x + vel.x * deltat + 0.5 * acc.x * pow(deltat,2);
6282
vel.x = (vel.x + acc.x * deltat);
63-
if (dblLOC.x <= 0.0 || dblLOC.x >= window.width - dst.w) {
83+
if (dblLOC.x <= 0.0 || dblLOC.x >= window.width - Screen_place.w) {
6484
vel.x *= Global::Physics::fRecoil;
6585
if (dblLOC.x <= 0.0) {dblLOC.x++;}
6686
else {dblLOC.x--;}
6787
} //end if hitting x edges
88+
6889
dblLOC.y = dblLOC.y + vel.y * deltat + 0.5 * acc.y * pow(deltat,2);
6990
vel.y = (vel.y + acc.y * deltat);
70-
if (dblLOC.y <= dst.h || dblLOC.y >= window.height) {
91+
if (dblLOC.y <= Screen_place.h || dblLOC.y >= window.height) {
7192
vel.y *= Global::Physics::fRecoil;
72-
if (dblLOC.y <= dst.h) {dblLOC.y++;}
93+
if (dblLOC.y <= Screen_place.h) {dblLOC.y++;}
7394
else {dblLOC.y--;}
7495
}//end if hitting y edges
7596

76-
if (dblLOC.x < 0.0) {place.x = 0;}
97+
if (dblLOC.x < 0.0) {place.x = 0; dblLOC.x = 0;}
7798
else {place.x = round(dblLOC.x);}
7899

79-
if (dblLOC.y < 0.0) {place.y = 0;}
100+
if (dblLOC.y < 0.0) {place.y = 0; dblLOC.y = 0;}
80101
else {place.y = round(dblLOC.y);}
81102

82-
//if (Global::blnDebugMode) {printf("Ball updated, new position (%f, %f)\n",dblLOC.x,dblLOC.y);}
83103
if (Global::Config.values.blnLogging) {
84104
FILE* logfile = fopen("logfile.log","a");
85105
fprintf(logfile,"Ball %3u \t (%.3f, %.3f)\n",ballID, dblLOC.x,dblLOC.y);
86106
fclose(logfile);
87107
}
88108

109+
//Update the collision box for the new location
110+
CollisionBox.left = place.x;
111+
CollisionBox.top = window.height - place.y;
112+
CollisionBox.right = CollisionBox.left + Screen_place.w;
113+
CollisionBox.bottom = CollisionBox.top + Screen_place.h;
114+
89115
double total_v;
90116
total_v = sqrt( pow(vel.x,2) + pow(vel.y,2) );
91117
if (total_v < Global::Physics::fMinVelocity || isnan(total_v) ) {
@@ -100,22 +126,24 @@ void clsCannonball::update(double newdeltat) {
100126
/**********************************************************************************************************************************************/
101127
void clsCannonball::setSDLScreen(SDL_Texture* SDLball, WINATT SDLwindow) {
102128
ball = SDLball;
129+
SDL_QueryTexture(ball,NULL,NULL, &Screen_place.w, &Screen_place.h);
103130
window = SDLwindow;
104131
}
105132
/**********************************************************************************************************************************************/
106133
void clsCannonball::show() {
107-
SDL_Rect dst;
108-
SDL_QueryTexture(ball,NULL,NULL, &dst.w, &dst.h);
109-
dst.x = place.x;
110-
dst.y = window.height - place.y;
134+
Screen_place.x = place.x;
135+
Screen_place.y = window.height - place.y;
111136
//Place the ball
112-
SDL_RenderCopy(window.ren,ball,NULL,&dst);
137+
SDL_RenderCopy(window.ren,ball,NULL,&Screen_place);
113138
}
114139
/**********************************************************************************************************************************************/
115140
void clsCannonball::setValues(double r, LOC init_place, double init_vel, double init_angle, uint ID) {
116141
props.radius = r; //in meters
117142
props.density = Global::Physics::uBallDensity; //density of steel in kg/m^3
118143

144+
acc.x = 0.00;
145+
acc.y = Global::Physics::fGravity;
146+
119147
place = init_place;
120148
dblLOC.x = (double) place.x;
121149
dblLOC.y = (double) place.y;
@@ -156,3 +184,7 @@ void clsCannonball::setPhysicalProps(PP newprops) {
156184
props = newprops;
157185
}
158186
/**********************************************************************************************************************************************/
187+
BOX clsCannonball::getBOX() {
188+
return CollisionBox;
189+
}
190+
/**********************************************************************************************************************************************/

src/cannonball.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,17 @@ struct stcPhysicalProperties {
2323
double volume;
2424
double density;
2525
};
26+
27+
struct stcBox {
28+
uint left;
29+
uint right;
30+
uint top;
31+
uint bottom;
32+
};
33+
2634
typedef struct stcDoubleValues dblXY;
2735
typedef struct stcPhysicalProperties PP;
36+
typedef struct stcBox BOX;
2837
/**********************************************************************************************************************************************/
2938
class clsCannonball {
3039
public:
@@ -40,6 +49,7 @@ class clsCannonball {
4049
void setVelocity(dblXY);
4150
PP getPhysicalProps(void);
4251
void setPhysicalProps(PP);
52+
BOX getBOX(void);
4353

4454
bool blnstarted;
4555
private:
@@ -49,6 +59,9 @@ class clsCannonball {
4959
SDL_Texture* ball;
5060
WINATT window;
5161

62+
BOX CollisionBox;
63+
SDL_Rect Screen_place;
64+
5265
LOC place;
5366
dblXY vel;
5467
dblXY dblLOC;

src/global.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ namespace Global {
1616
extern const uint uBallDensity;
1717
extern const float fGravity;
1818
extern const float fDragCofficient;
19+
extern const float fKineticFriction;
1920
extern const float fDensityAir;
2021
extern const float fRecoil;
2122
extern const float fVelocityScalar;
2223
extern const float fMinVelocity;
23-
extern const float fMomentumLoss;
24+
extern const float fCoefficientRestitution;
2425
extern const uchar CollisionMethod;
2526
}
2627
}

0 commit comments

Comments
 (0)