Skip to content

Commit 8559df6

Browse files
author
GamerMan7799
committed
Update to v1.4.0-beta.4
### Added * Info tool * Ability to pause simulation. ### Fixed * Bug with selecting balls * Doxygen todo list not generating
1 parent 50ef416 commit 8559df6

File tree

12 files changed

+186
-101
lines changed

12 files changed

+186
-101
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,15 @@ R = All motion will be stopped.
108108
Q / ESC = Program will exit.
109109
F = Every so often a randomly generated cannonball will be created
110110
T = Toggle toolbox on/off
111+
P = Toggle pause for the simulation
111112
Right Arrow = Select next tool
112113
Left Arrow = Select previous tool
113114
1 = Select Fire tool
114115
2 = Select Drop tool
115116
3 = Select Rope tool
116117
4 = Select Delete tool
117118
5 = Select Drag tool
119+
6 = Select Info tool
118120
```
119121

120122
## Tools
@@ -140,3 +142,7 @@ Clicking on a ball will delete it.
140142
### Drag tool - WIP
141143

142144
Clicking and dragging on a ball will allow you to move a ball where the mouse is. When released the ball will still have its inital velocity.
145+
146+
### Info Tool - WIP
147+
148+
Clicking on a ball will write infomation about it to the console

docs/ChangesLog.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
All notable changes to this project will be documented here.
44
This project adheres to [Semantic Versioning](http://semver.org/)
55

6-
## [1.4.0-beta.3] - WIP
6+
## [1.4.0-beta.4] - WIP
77
### Added
8-
* Toolbar with different tools including: drag, fire, drop, rope, and delete. Not all are fully working at the moment.
9-
* Currently finished tools: fire, drop, delete
8+
* Toolbar with different tools including: drag, fire, drop, rope, delete, and info. Not all are fully working at the moment.
9+
* Currently finished tools: fire, drop, delete, info
1010
* SDL Licenses.
1111
* Credits to Docs.
1212
* More Doxygen stuff.
1313
* Debug message that prints ball mass.
14+
* Ability to pause the simulation.
1415

1516
### Changed
1617
* Moved Docs to Doc folder.

src/cannonball.cpp

Lines changed: 98 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -151,78 +151,77 @@ void clsCannonball::update(double newdeltat) {
151151
///
152152
/////////////////////////////////////////////////
153153

154-
blncheckphysics_ = true; // enable on update
155-
deltat_ = newdeltat;
156-
157-
// reset the forces so strange things don't happen
158-
forces_ = {0.0, props_.mass * global::physics::kGravity};
159-
160-
if (blndragenabled_) {
161-
doMagnusEffect();
162-
dragUpdateAcc();
163-
}
164-
doFriction();
165-
166-
acc_.x = forces_.x / props_.mass;
167-
acc_.y = forces_.y / props_.mass;
168-
169-
vel_.x = (vel_.x + acc_.x * deltat_);
170-
vel_.y = (vel_.y + acc_.y * deltat_);
171-
172-
// get new velocities if collision with edges
173-
174-
double coefres = (global::config.values.uchrCollisionMethod == CollideInelastic) ?
175-
global::physics::kCoefficientRestitution : 1.0;
176-
177-
178-
/* TODO (GamerMan7799#2#): Make this stuff simpler */
179-
if (collisionbox_.left < screen_place_.w / 2) {
180-
vel_.x *= -1 * coefres;
181-
vel_.y *= coefres;
182-
dblLOC_.x = screen_place_.w;
183-
}
184-
185-
if (collisionbox_.right > (screen::screenatt.width)) {
186-
vel_.x *= -1 * coefres;
187-
vel_.y *= coefres;
188-
dblLOC_.x = screen::screenatt.width - screen_place_.w / 2;
189-
}
190-
191-
if (collisionbox_.bottom > (screen::screenatt.height)) {
192-
vel_.x *= coefres;
193-
vel_.y *= -1 * coefres;
194-
dblLOC_.y = screen_place_.h / 2;
195-
}
196-
197-
if (collisionbox_.top < 0 ) {
198-
vel_.x *= coefres;
199-
vel_.y *= -1 * coefres;
200-
dblLOC_.y = (screen::screenatt.height) - screen_place_.h / 2;
201-
}
202-
203-
dblLOC_.x = dblLOC_.x + vel_.x * deltat_ /*+ 0.5 * acc_.x * pow(deltat_,2)*/;
204-
dblLOC_.y = dblLOC_.y + vel_.y * deltat_ /*+ 0.5 * acc_.y * pow(deltat_,2)*/;
205-
206-
// update place and collision box again in case something changed
207-
place_.x = round(dblLOC_.x);
208-
place_.y = round(dblLOC_.y);
209-
updateCollisionBox();
210-
211-
if (global::config.values.blnLogging) {
212-
FILE* logfile = fopen("logfile.log","a");
213-
fprintf(logfile,"Ball %3u \t (%.3f, %.3f)\n",ballID_, dblLOC_.x,dblLOC_.y);
214-
fclose(logfile);
215-
}
216-
217-
double total_v;
218-
total_v = sqrt( pow(vel_.x,2) + pow(vel_.y,2) );
219-
if (total_v < global::physics::kMinVelocity || isnan(total_v) ) {
220-
blnstarted_ = false;
221-
if (global::blnDebugMode) {
222-
if ( isnan(total_v) ) { printf("Ball velocity is NaN; killing it.\n"); }
223-
else { printf("Ball moving too slow; killing it.\n"); }
224-
} //end if debug mode
225-
} //end if should kill
154+
if (!paused_) {
155+
blncheckphysics_ = true; // enable on update
156+
deltat_ = newdeltat;
157+
158+
// reset the forces so strange things don't happen
159+
forces_ = {0.0, props_.mass * global::physics::kGravity};
160+
161+
if (blndragenabled_) {
162+
doMagnusEffect();
163+
dragUpdateAcc();
164+
}
165+
doFriction();
166+
167+
acc_.x = forces_.x / props_.mass;
168+
acc_.y = forces_.y / props_.mass;
169+
170+
vel_.x = (vel_.x + acc_.x * deltat_);
171+
vel_.y = (vel_.y + acc_.y * deltat_);
172+
173+
// get new velocities if collision with edges
174+
double coefres = (global::config.values.uchrCollisionMethod == CollideInelastic) ?
175+
global::physics::kCoefficientRestitution : 1.0;
176+
177+
if (collisionbox_.left < screen_place_.w / 2) {
178+
vel_.x *= -1 * coefres;
179+
vel_.y *= coefres;
180+
dblLOC_.x = screen_place_.w;
181+
}
182+
183+
if (collisionbox_.right > (screen::screenatt.width)) {
184+
vel_.x *= -1 * coefres;
185+
vel_.y *= coefres;
186+
dblLOC_.x = screen::screenatt.width - screen_place_.w / 2;
187+
}
188+
189+
if (collisionbox_.bottom > (screen::screenatt.height)) {
190+
vel_.x *= coefres;
191+
vel_.y *= -1 * coefres;
192+
dblLOC_.y = screen_place_.h / 2;
193+
}
194+
195+
if (collisionbox_.top < 0 ) {
196+
vel_.x *= coefres;
197+
vel_.y *= -1 * coefres;
198+
dblLOC_.y = (screen::screenatt.height) - screen_place_.h / 2;
199+
}
200+
201+
dblLOC_.x = dblLOC_.x + vel_.x * deltat_ /*+ 0.5 * acc_.x * pow(deltat_,2)*/;
202+
dblLOC_.y = dblLOC_.y + vel_.y * deltat_ /*+ 0.5 * acc_.y * pow(deltat_,2)*/;
203+
204+
// update place and collision box again in case something changed
205+
place_.x = round(dblLOC_.x);
206+
place_.y = round(dblLOC_.y);
207+
updateCollisionBox();
208+
209+
if (global::config.values.blnLogging) {
210+
FILE* logfile = fopen("logfile.log","a");
211+
fprintf(logfile,"Ball %3u \t (%.3f, %.3f)\n",ballID_, dblLOC_.x,dblLOC_.y);
212+
fclose(logfile);
213+
}
214+
215+
double total_v;
216+
total_v = sqrt( pow(vel_.x,2) + pow(vel_.y,2) );
217+
if (total_v < global::physics::kMinVelocity || isnan(total_v) ) {
218+
blnstarted_ = false;
219+
if (global::blnDebugMode) {
220+
if ( isnan(total_v) ) { printf("Ball velocity is NaN; killing it.\n"); }
221+
else { printf("Ball moving too slow; killing it.\n"); }
222+
} //end if debug mode
223+
} //end if should kill
224+
} // end if not paused
226225
show(); //show the ball on the screen
227226
}
228227
/*****************************************************************************/
@@ -278,6 +277,7 @@ void clsCannonball::setValues(double r, LOC init_place,
278277
/////////////////////////////////////////////////
279278

280279
ballID_ = newID;
280+
paused_ = false;
281281

282282
props_.radius = r; //in meters
283283

@@ -417,8 +417,8 @@ void clsCannonball::doFriction() {
417417
normal_force;
418418
updateCollisionBox();
419419
//Update acc for Friction values
420-
if ( collisionbox_.bottom < screen_place_.h ||
421-
collisionbox_.top > screen::screenatt.height ) {
420+
if ( collisionbox_.bottom <= screen_place_.h ||
421+
collisionbox_.top >= screen::screenatt.height ) {
422422
//Ball is in contact with floor or ceiling update x acc
423423
forces_.x += friction * (vel_.x < 0.0 ? -1.0 : 1.0);
424424
} // end if touching top/bottom
@@ -469,3 +469,27 @@ void clsCannonball::setSpin(double newspin) {
469469
spin_ = newspin;
470470
}
471471
/*****************************************************************************/
472+
void clsCannonball::writeInfo() {
473+
/////////////////////////////////////////////////
474+
/// @brief Writes information bout the ball to the console
475+
/////////////////////////////////////////////////
476+
477+
printf("\n\n");
478+
printf("Information about Ball: %i\n",ballID_);
479+
printf("Color: \t \t \t (%i, %i, %i)\n",color_.Red, color_.Green, color_.Blue);
480+
printf("Location: \t \t (%5.5f, %5.5f)\n",dblLOC_.x,dblLOC_.y);
481+
printf("Velocity: \t \t (%5.5f, %5.5f)\n",vel_.x,vel_.y);
482+
printf("Acceleration: \t \t (%5.5f, %5.5f)\n",acc_.x,acc_.y);
483+
printf("Forces: \t \t (%5.5f, %5.5f)\n", forces_.x, forces_.y);
484+
printf("Spin: \t \t \t (%5.5f)\n\n\n", spin_);
485+
486+
}
487+
/*****************************************************************************/
488+
void clsCannonball::togglePause() {
489+
/////////////////////////////////////////////////
490+
/// @brief Enable/disable pause
491+
/////////////////////////////////////////////////
492+
493+
paused_ = !(paused_);
494+
}
495+
/*****************************************************************************/

src/cannonball.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ typedef struct stcDoubleValues dblXY;
6262
typedef struct stcPhysicalProperties PP;
6363
typedef struct stcBox BOX;
6464
typedef struct stcColor clr;
65-
typedef struct stcXYZVector XYZ;
65+
//typedef struct stcXYZVector XYZ;
6666

6767
typedef std::vector<LOC> VPath;
6868
/// @}
@@ -75,17 +75,15 @@ class clsCannonball {
7575
LOC getplace(void);
7676
void setplace(LOC);
7777
void setSpin(double);
78-
7978
void update(double);
80-
//void setSDLScreen(SDL_Texture*, SDL_Texture*, WINATT, uint);
8179
dblXY getVelocity(void);
8280
void setVelocity(dblXY);
8381
PP getPhysicalProps(void);
8482
void setPhysicalProps(PP);
8583
BOX getBOX(void);
8684
double getSpin(void);
87-
88-
85+
void writeInfo(void);
86+
void togglePause(void);
8987
bool blnstarted_; /**< Whether or not the ball is "started" if it is, the program will update
9088
it and won't let a new ball replace it in its array spot. */
9189

@@ -114,6 +112,7 @@ class clsCannonball {
114112

115113
dblXY forces_; /**< Forces on the ball in x and y */
116114
double spin_; /**< The angular velocity of the ball (in rad/s) for the Magnus Effect */
115+
bool paused_; /**< If the simulation is paused or not. */
117116

118117
void show(void);
119118
void drawPath(LOC);

src/core.cpp

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,9 @@ char core::handleEvent(SDL_Event* e ) {
359359
// there is a mouse-based event, so now we have to check what tool we are using.
360360
if (global::blnDebugMode) { printf("Mouse event found.\n"); }
361361
switch ( toolbar.getTool() ) {
362+
/** @todo (GamerMan7799#9#) Simplify the cases, merge similar tool functions
363+
such as ToolFire, and ToolDrop, and ToolRope, ToolDele, ToolDrag, and ToolInfo.
364+
I want to get them all working on their own first before I try to merge them. */
362365
case ToolFire:
363366
default: // make tool fire the default
364367
if(global::blnDebugMode) { printf("Tool Fire event\n"); }
@@ -399,6 +402,18 @@ char core::handleEvent(SDL_Event* e ) {
399402
return 0;
400403
case ToolDrag:
401404
if(global::blnDebugMode) { printf("Tool Drag event\n"); }
405+
SDL_GetMouseState(&currentmouse.x, &currentmouse.y);
406+
ball_num = findSelectedBall(currentmouse);
407+
if (ball_num == -1) {return 0;}
408+
return 0;
409+
case ToolInfo:
410+
if(global::blnDebugMode) { printf("Tool Info event\n"); }
411+
if ( e->type == SDL_MOUSEBUTTONDOWN ) {
412+
SDL_GetMouseState(&currentmouse.x, &currentmouse.y);
413+
ball_num = findSelectedBall(currentmouse);
414+
if (ball_num == -1) {return 0;}
415+
cannonballs::balls[ball_num].writeInfo();
416+
}
402417
return 0;
403418
} // end switch tool type
404419
} else if ( e->type == SDL_KEYDOWN ) {
@@ -416,6 +431,14 @@ char core::handleEvent(SDL_Event* e ) {
416431
for (int i = 0; i < cannonballs::balls.size(); ++i)
417432
{ cannonballs::balls[i].setVelocity({0.0,0.0}); }
418433
return 0;
434+
case SDLK_p:
435+
//"pauses" the simulation by preventing ball from updating
436+
/** @bug (GamerMan7799#1#): If you create a ball while the simulation is paused,
437+
the newly created ball won't be paused. And it will be inverse the rest
438+
of the simulation. */
439+
for (int i = 0; i < cannonballs::balls.size(); ++i)
440+
{ cannonballs::balls[i].togglePause(); }
441+
return 0;
419442
case SDLK_f:
420443
if(global::blnDebugMode) { printf("Random fire triggered\n"); }
421444
return 'f';
@@ -444,6 +467,9 @@ char core::handleEvent(SDL_Event* e ) {
444467
case SDLK_5:
445468
core::toolbar.setTool(ToolDrag);
446469
return 0;
470+
case SDLK_6:
471+
core::toolbar.setTool(ToolInfo);
472+
return 0;
447473
default:
448474
return 0;
449475
} //end switch key
@@ -465,7 +491,8 @@ int core::findSelectedBall(LOC mouseplace) {
465491
ball_box = cannonballs::balls[i].getBOX();
466492

467493
if ( mouseplace.x >= ball_box.left && mouseplace.x <= ball_box.right) {
468-
if ( mouseplace.y >= ball_box.top && mouseplace.y >= ball_box.bottom) {
494+
// SDL y coordinates are measured from top of screen
495+
if ( mouseplace.y >= ball_box.top && mouseplace.y <= ball_box.bottom) {
469496
return i;
470497
}
471498
}

src/global.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,9 @@ enum Tools {
8585
ToolFire = 0, /**< The basic firing tool. */
8686
ToolDrop, /**< Drops a ball without velocity. */
8787
ToolRope, /**< Tool to add a rope between balls, or pin ball to wall. */
88-
ToolDele, /**< Tool to delete selected ball. */
88+
ToolDele, /**< Tool to delete selected ball. */
8989
ToolDrag, /**< Tool that allows ball to be dragged around. */
90+
ToolInfo /**< Tool that writes information about the selected ball to screen */
9091
};
9192
/*****************************************************************************/
9293
#endif // __GLOBAL_H_INCLUDED__

src/image_tools.xpm

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* XPM */
22
static char * image_tools_xpm[] = {
3-
"24 120 2 1",
3+
"24 144 2 1",
44
" c None",
55
". c #FFFFFF",
66
" ",
@@ -122,4 +122,28 @@ static char * image_tools_xpm[] = {
122122
" ............. ",
123123
" ........... ",
124124
" ......... ",
125-
" "};
125+
" ",
126+
" ",
127+
" ",
128+
" ......... ",
129+
" ............ ",
130+
" ................ ",
131+
" ................. ",
132+
" ......... ....... ",
133+
" ......... ....... ",
134+
" .......... ........ ",
135+
" ...................... ",
136+
" .......... ........... ",
137+
" ......... .......... ",
138+
" .......... ......... ",
139+
" .......... .......... ",
140+
" ......... .......... ",
141+
" ......... .......... ",
142+
" ........ ......... ",
143+
" ........ ......... ",
144+
" ....... ....... ",
145+
" .................. ",
146+
" ................ ",
147+
" ............. ",
148+
" .......... ",
149+
" .... "};

0 commit comments

Comments
 (0)