@@ -151,61 +151,63 @@ void clsCannonball::update(double newdeltat) {
151
151
// /
152
152
// ///////////////////////////////////////////////
153
153
154
+ blncheckphysics_ = true ; // enable on update
154
155
if (!paused_) {
155
- blncheckphysics_ = true ; // enable on update
156
+
156
157
deltat_ = newdeltat;
157
158
158
159
// reset the forces so strange things don't happen
159
160
forces_ = {0.0 , props_.mass * global::physics::kGravity };
160
161
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
162
// get new velocities if collision with edges
174
163
double coefres = (global::config.values .uchrCollisionMethod == CollideInelastic) ?
175
164
global::physics::kCoefficientRestitution : 1.0 ;
176
165
177
166
if (collisionbox_.left < screen_place_.w / 2 ) {
178
167
vel_.x *= -1 * coefres;
179
168
vel_.y *= coefres;
169
+ forces_.x -= props_.mass * global::physics::kGravity ;
180
170
dblLOC_.x = screen_place_.w ;
181
171
}
182
172
183
173
if (collisionbox_.right > (screen::screenatt.width )) {
184
174
vel_.x *= -1 * coefres;
185
175
vel_.y *= coefres;
176
+ forces_.x += props_.mass * global::physics::kGravity ;
186
177
dblLOC_.x = screen::screenatt.width - screen_place_.w / 2 ;
187
178
}
188
179
189
180
if (collisionbox_.bottom > (screen::screenatt.height )) {
190
181
vel_.x *= coefres;
191
182
vel_.y *= -1 * coefres;
183
+ forces_.y -= props_.mass * global::physics::kGravity ;
192
184
dblLOC_.y = screen_place_.h / 2 ;
193
185
}
194
186
195
187
if (collisionbox_.top < 0 ) {
196
188
vel_.x *= coefres;
197
189
vel_.y *= -1 * coefres;
190
+ forces_.y += props_.mass * global::physics::kGravity ;
198
191
dblLOC_.y = (screen::screenatt.height ) - screen_place_.h / 2 ;
199
192
}
200
193
194
+ if (blndragenabled_) {
195
+ doMagnusEffect ();
196
+ dragUpdateAcc ();
197
+ }
198
+ doFriction ();
199
+
200
+ acc_.x = forces_.x / props_.mass ;
201
+ acc_.y = forces_.y / props_.mass ;
202
+
203
+ vel_.x = (vel_.x + acc_.x * deltat_);
204
+ vel_.y = (vel_.y + acc_.y * deltat_);
205
+
206
+
207
+
201
208
dblLOC_.x = dblLOC_.x + vel_.x * deltat_ /* + 0.5 * acc_.x * pow(deltat_,2)*/ ;
202
209
dblLOC_.y = dblLOC_.y + vel_.y * deltat_ /* + 0.5 * acc_.y * pow(deltat_,2)*/ ;
203
210
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
211
if (global::config.values .blnLogging ) {
210
212
FILE* logfile = fopen (" logfile.log" ," a" );
211
213
fprintf (logfile," Ball %3u \t (%.3f, %.3f)\n " ,ballID_, dblLOC_.x ,dblLOC_.y );
@@ -222,6 +224,7 @@ void clsCannonball::update(double newdeltat) {
222
224
} // end if debug mode
223
225
} // end if should kill
224
226
} // end if not paused
227
+ updateCollisionBox ();
225
228
show (); // show the ball on the screen
226
229
}
227
230
/* ****************************************************************************/
@@ -299,6 +302,7 @@ void clsCannonball::setValues(double r, LOC init_place,
299
302
blnstarted_ = true ;
300
303
301
304
dragCalcValues ();
305
+ updateCollisionBox ();
302
306
303
307
304
308
forces_ = {0.00 , global::physics::kGravity * props_.mass };
@@ -394,6 +398,10 @@ void clsCannonball::updateCollisionBox() {
394
398
// / @brief Updates the collision box
395
399
// ///////////////////////////////////////////////
396
400
401
+ // update place and collision box again in case something changed
402
+ place_.x = round (dblLOC_.x );
403
+ place_.y = round (dblLOC_.y );
404
+
397
405
// Update the collision box for the new location
398
406
collisionbox_.left = place_.x - floor (screen_place_.w / 2 );
399
407
collisionbox_.top = screen::screenatt.height - (place_.y + floor (screen_place_.h / 2 ));
@@ -417,24 +425,10 @@ void clsCannonball::doFriction() {
417
425
normal_force;
418
426
updateCollisionBox ();
419
427
// Update acc for Friction values
420
- if ( collisionbox_.bottom <= screen_place_.h ||
421
- collisionbox_.top >= screen::screenatt.height ) {
422
- // Ball is in contact with floor or ceiling update x acc
428
+ if ( collisionbox_.bottom <= screen_place_.h ) {
429
+ // Ball is in contact with floor
423
430
forces_.x += friction * (vel_.x < 0.0 ? -1.0 : 1.0 );
424
- } // end if touching top/bottom
425
-
426
-
427
- // Since there really isn't a normal force when the ball contacts
428
- // the edges, this section is commented out.
429
- /*
430
- if ( collisionbox_.left < (screen_place_.w /2 ) ||
431
- collisionbox_.right > screen::screenatt.width ) {
432
- // Ball is in contact with the wall update y acc.
433
- forces_.y += friction * (vel_.y < 0.0 ? -1.0 : 1.0);
434
- // add normal force
435
- forces_.x += normal_force * (collisionbox_.right > screen::screenatt.width) ?
436
- 1.0 : -1.0;
437
- } // end if touching side edges */
431
+ }
438
432
}
439
433
/* ****************************************************************************/
440
434
void clsCannonball::doMagnusEffect () {
@@ -493,3 +487,11 @@ void clsCannonball::togglePause() {
493
487
paused_ = !(paused_);
494
488
}
495
489
/* ****************************************************************************/
490
+ bool clsCannonball::isPaused () {
491
+ // ///////////////////////////////////////////////
492
+ // / @brief Returns if paused or not
493
+ // ///////////////////////////////////////////////
494
+
495
+ return paused_;
496
+ }
497
+ /* ****************************************************************************/
0 commit comments