@@ -28,7 +28,6 @@ clsCannonball::clsCannonball() {
2828 deltat_ = (1.00 / 60.00 );
2929 forces_ = {0 ,0 };
3030 acc_ = {0.00 , global::physics::kGravity };
31- spin_ = 1.0 ;
3231
3332 props_.radius = 5.0 ; // in meters
3433 props_.density = global::physics::kBallDensity ; // density of steel in kg/m^3
@@ -156,13 +155,6 @@ void clsCannonball::update(double newdeltat) {
156155
157156 deltat_ = newdeltat;
158157
159- if (blndragenabled_) {
160- doMagnusEffect ();
161- dragUpdateAcc ();
162- }
163- doFriction ();
164- checkEdges ();
165-
166158 acc_.x = forces_.x / props_.mass ;
167159 acc_.y = forces_.y / props_.mass ;
168160
@@ -172,6 +164,8 @@ void clsCannonball::update(double newdeltat) {
172164 dblLOC_.x += vel_.x * deltat_ /* + 0.5 * acc_.x * pow(deltat_,2)*/ ;
173165 dblLOC_.y += vel_.y * deltat_ /* + 0.5 * acc_.y * pow(deltat_,2)*/ ;
174166
167+ setEdgePosition ();
168+
175169 if (global::config.values .blnLogging ) {
176170 FILE* logfile = fopen (" logfile.log" ," a" );
177171 fprintf (logfile," Ball %3u \t (%.3f, %.3f)\n " ,ballID_, dblLOC_.x ,dblLOC_.y );
@@ -191,7 +185,6 @@ void clsCannonball::update(double newdeltat) {
191185 updateCollisionBox ();
192186 show (); // show the ball on the screen
193187 // reset the forces so strange things don't happen
194- forces_ = {0.0 , props_.mass * global::physics::kGravity };
195188}
196189/* ****************************************************************************/
197190void clsCannonball::show () {
@@ -256,12 +249,9 @@ void clsCannonball::setValues(double r, LOC init_place,
256249
257250 acc_ = {0.00 , global::physics::kGravity };
258251
259- spin_ = (rand () % 1000 - 500 ) / 500 ;
260-
261-
262252 place_ = init_place;
263- dblLOC_ = { (double ) place_.x ,
264- (double ) place_.y };
253+ dblLOC_ = { (double ) place_.x / global::physics:: kMeterPixelRatio ,
254+ (double ) place_.y / global::physics:: kMeterPixelRatio };
265255
266256 vel_ = { (double )(init_vel) * (cos (init_angle)),
267257 (double )(init_vel) * (sin (init_angle)) };
@@ -307,8 +297,8 @@ void clsCannonball::setplace(LOC newplace) {
307297 // ///////////////////////////////////////////////
308298 // / @brief Set the ball's place
309299 // ///////////////////////////////////////////////
310- dblLOC_.x = (double )newplace.x ;
311- dblLOC_.y = (double )newplace.y ;
300+ dblLOC_.x = (double )newplace.x / global::physics:: kMeterPixelRatio ;
301+ dblLOC_.y = (double )newplace.y / global::physics:: kMeterPixelRatio ;
312302}
313303/* ****************************************************************************/
314304void clsCannonball::setPhysicalProps (PP newprops) {
@@ -365,12 +355,12 @@ void clsCannonball::updateCollisionBox() {
365355 // ///////////////////////////////////////////////
366356
367357 // update place and collision box again in case something changed
368- place_.x = round (dblLOC_.x );
369- place_.y = round (dblLOC_.y );
358+ place_.x = round (dblLOC_.x * global::physics:: kMeterPixelRatio );
359+ place_.y = round (dblLOC_.y * global::physics:: kMeterPixelRatio );
370360
371361 // Update the collision box for the new location
372- collisionbox_.left = place_.x - floor (screen_place_.w / 2 );
373- collisionbox_.top = screen::screenatt.height - (place_.y + floor (screen_place_.h / 2 ));
362+ collisionbox_.left = place_.x - ( int ) (screen_place_.w / 2 );
363+ collisionbox_.top = screen::screenatt.height - (place_.y + ( int ) (screen_place_.h / 2 ));
374364 collisionbox_.bottom = collisionbox_.top + screen_place_.h ;
375365 collisionbox_.right = collisionbox_.left + screen_place_.w ;
376366}
@@ -392,38 +382,12 @@ void clsCannonball::doFriction() {
392382 }
393383}
394384/* ****************************************************************************/
395- void clsCannonball::doMagnusEffect () {
396- // ///////////////////////////////////////////////
397- // / @brief Calculates the Magnus Effect caused by the ball's spin
398- // ///////////////////////////////////////////////
399-
400- double magnus_force = pow (M_PI,2 ) * pow (props_.radius ,3 ) *
401- global::physics::kDensityAir ;
402-
403- forces_.x -= magnus_force * vel_.y * spin_;
404- forces_.y += magnus_force * vel_.x * spin_;
405- }
406- /* ****************************************************************************/
407385void clsCannonball::addForce (dblXY newforces) {
408386
409387 forces_.x += newforces.x ;
410388 forces_.y += newforces.y ;
411389}
412390/* ****************************************************************************/
413- double clsCannonball::getSpin () {
414- // ///////////////////////////////////////////////
415- // / @brief Returns the ball's spin
416- // ///////////////////////////////////////////////
417- return spin_;
418- }
419- /* ****************************************************************************/
420- void clsCannonball::setSpin (double newspin) {
421- // ///////////////////////////////////////////////
422- // / @brief Sets the ball's spin
423- // ///////////////////////////////////////////////
424- spin_ = newspin;
425- }
426- /* ****************************************************************************/
427391void clsCannonball::writeInfo () {
428392 // ///////////////////////////////////////////////
429393 // / @brief Writes information bout the ball to the console
@@ -436,7 +400,6 @@ void clsCannonball::writeInfo() {
436400 printf (" Velocity: \t \t (%5.5f, %5.5f)\n " ,vel_.x ,vel_.y );
437401 printf (" Acceleration: \t \t (%5.5f, %5.5f)\n " ,acc_.x ,acc_.y );
438402 printf (" Forces: \t \t (%5.5f, %5.5f)\n " , forces_.x , forces_.y );
439- printf (" Spin: \t \t \t (%5.5f)\n\n\n " , spin_);
440403
441404}
442405/* ****************************************************************************/
@@ -462,20 +425,16 @@ LOC clsCannonball::getScreenPlace() {
462425 // ///////////////////////////////////////////////
463426 LOC temp;
464427 temp.x = screen_place_.x ;
465- temp.x += floor (screen_place_.w /2 );
428+ temp.x += ( int ) (screen_place_.w /2 );
466429 temp.y = screen_place_.y ;
467- temp.y += floor (screen_place_.h /2 );
430+ temp.y += ( int ) (screen_place_.h /2 );
468431 return temp;
469432}
470433/* ****************************************************************************/
471434void clsCannonball::checkEdges () {
472435 // ///////////////////////////////////////////////
473436 // / @brief Checks and does stuff if ball is colliding with edges.
474437 // ///////////////////////////////////////////////
475-
476- /* * @bug (GamerMan7799#1#): Balls appear to bounce forever because position is updated again after
477- running this function. */
478-
479438 // get new velocities if collision with edges
480439 double coefres = (global::config.values .uchrCollisionMethod == CollideInelastic) ?
481440 global::physics::kCoefficientRestitution : 1.0 ;
@@ -484,28 +443,71 @@ void clsCannonball::checkEdges() {
484443 vel_.x *= -1 * coefres;
485444 vel_.y *= coefres;
486445 forces_.x = 0 ;
487- dblLOC_.x = screen_place_.w + 1 ;
488446 }
489447
490448 if (collisionbox_.right > (screen::screenatt.width )) {
491449 vel_.x *= -1 * coefres;
492450 vel_.y *= coefres;
493451 forces_.x = 0 ;
494- dblLOC_.x = screen::screenatt.width - screen_place_.w / 2 - 1 ;
495452 }
496453
497454 if (collisionbox_.bottom > (screen::screenatt.height )) {
498455 vel_.x *= coefres;
499456 vel_.y *= -1 * coefres;
500457 forces_.y = 0 ;
501- dblLOC_.y = screen_place_.h / 2 + 1 ;
502458 }
503459
504460 if (collisionbox_.top < 0 ) {
505461 vel_.x *= coefres;
506462 vel_.y *= -1 * coefres;
507463 forces_.y = 2 * props_.mass * global::physics::kGravity ;
508- dblLOC_.y = (screen::screenatt.height ) - screen_place_.h / 2 - 1 ;
509464 }
510465}
511466/* ****************************************************************************/
467+ void clsCannonball::setEdgePosition () {
468+ // ///////////////////////////////////////////////
469+ // / @brief Set position if colliding with edges
470+ // ///////////////////////////////////////////////
471+
472+ if (collisionbox_.left < screen_place_.w / 2 ) {
473+ dblLOC_.x = (screen_place_.w + 1 );
474+ dblLOC_.x /= global::physics::kMeterPixelRatio ;
475+ }
476+
477+ if (collisionbox_.right > (screen::screenatt.width )) {
478+ dblLOC_.x = (screen::screenatt.width - screen_place_.w / 2 - 1 );
479+ dblLOC_.x /= global::physics::kMeterPixelRatio ;
480+ }
481+
482+ if (collisionbox_.bottom > (screen::screenatt.height )) {
483+ dblLOC_.y = (screen_place_.h / 2 + 1 );
484+ dblLOC_.y /= global::physics::kMeterPixelRatio ;
485+ }
486+
487+ if (collisionbox_.top < 0 ) {
488+ dblLOC_.y = ((screen::screenatt.height ) - screen_place_.h / 2 - 1 );
489+ dblLOC_.y /= global::physics::kMeterPixelRatio ;
490+ }
491+ }
492+ /* ****************************************************************************/
493+ dblXY clsCannonball::getForces () {
494+ // ///////////////////////////////////////////////
495+ // / @brief Returns forces on ball.
496+ // ///////////////////////////////////////////////
497+
498+ return forces_;
499+ }
500+ /* ****************************************************************************/
501+ void clsCannonball::updateForces () {
502+ // ///////////////////////////////////////////////
503+ // / @brief Updates all the forces on the ball.
504+ // ///////////////////////////////////////////////
505+
506+ forces_ = {0 ,props_.mass * global::physics::kGravity };
507+
508+ if (blndragenabled_) { dragUpdateAcc (); }
509+ doFriction ();
510+ checkEdges ();
511+
512+ }
513+ /* ****************************************************************************/
0 commit comments