Commit 9fedfc8792e23916e40d5fe5b91b42865e2eff9e

Authored by Bjørn-Richard Pedersen
1 parent 2a0596d2

Cleaned up code and made it ready for hand-in

Showing 2 changed files with 29 additions and 40 deletions   Show diff stats
include/collision_library.cpp
... ... @@ -29,13 +29,11 @@ namespace collision
29 29  
30 30 // If the sphere's state is Rolling, it should use an adjusted DS
31 31 if( S1._state == DynamicPSphere::States::Rolling ) {
32   -// auto unconst_S1 = const_cast<DynamicPhysObject<GMlib::PSphere<float>>&>(S1);
33 32 r1 = S1.adjustedTrajectory(new_dt);
34 33 }
35 34  
36 35 // If the sphere's state is Rolling, it should use an adjusted DS
37 36 if( S0._state == DynamicPSphere::States::Rolling ) {
38   -// auto unconst_S0 = const_cast<DynamicPhysObject<GMlib::PSphere<float>>&>(S0);
39 37 r2 = S0.adjustedTrajectory(new_dt);
40 38 }
41 39  
... ... @@ -121,7 +119,7 @@ namespace collision
121 119 const auto s_radius = S.getRadius();
122 120  
123 121 auto &plane = const_cast<StaticPhysObject<GMlib::PPlane<float>>&>(P);
124   - const auto p = plane.evaluateParent(0.5f, 0.5f, 1, 1); // plane.getMatrixToScene * plane.evaluateParent(0.5f, 0.5f, 1, 1)
  122 + const auto p = plane.evaluateParent(0.5f, 0.5f, 1, 1);
125 123 const auto plane_pos = p(0)(0);
126 124 const auto u = p(1)(0);
127 125 const auto v = p(0)(1);
... ... @@ -136,7 +134,6 @@ namespace collision
136 134  
137 135 // If the sphere's state is Rolling, it should use an adjusted DS
138 136 if( S._state == DynamicPSphere::States::Rolling ) {
139   -// auto unconst_S = const_cast<DynamicPhysObject<GMlib::PSphere<float>>&>(S);
140 137 ds = S.adjustedTrajectory(new_dt);
141 138 }
142 139  
... ... @@ -241,6 +238,7 @@ namespace collision
241 238  
242 239 }
243 240  
  241 + /*** Created in collaboration with Fatemeh Heidari and Ghada Bouzidi ***/
244 242 GMlib::Vector<float,3>
245 243 MyController::closestPoint(DynamicPSphere* sphere, seconds_type dt)
246 244 {
... ... @@ -262,14 +260,10 @@ namespace collision
262 260 GMlib::Vector<float, 2> b;
263 261 GMlib::Vector<float,3> d{0.0f,0.0f,0.0f};
264 262  
265   -
266 263 auto planes = _map[sphere];
267 264  
268   -
269 265 //iteration
270   -
271   - for ( int i=0; i<10;i++/* delta_u > epsilon && delta_v > epsilon*/){
272   - GMlib::Vector <float,3>Sn {0.0f,0.0f,0.0f};
  266 + for ( int i=0; i<10;i++){
273 267 GMlib::Vector <float,3>Su {0.0f,0.0f,0.0f};
274 268 GMlib::Vector <float,3>Sv {0.0f,0.0f,0.0f};
275 269 GMlib::Vector <float,3>Suu {0.0f,0.0f,0.0f};
... ... @@ -278,7 +272,6 @@ namespace collision
278 272 GMlib::APoint <float,3>q;
279 273  
280 274 for(auto it = planes.begin(); it != planes.end(); it++){
281   - GMlib::Vector<float,3> normal{0.0f,0.0f,0.0f};
282 275 GMlib::DMatrix<GMlib::Vector<float,3>> M = (*it)->evaluateParent(u,v,2,2);
283 276 q = M(0)(0);
284 277 Su += M(1)(0);
... ... @@ -321,7 +314,7 @@ namespace collision
321 314 DynamicPhysObject<GMlib::PSphere<float> >::simulateToTInDt(seconds_type t){
322 315  
323 316  
324   - if( this->_state == DynamicPSphere::States::AtRest or this->velocity <= 0.04 ) {
  317 + if( this->_state == DynamicPSphere::States::AtRest or this->velocity <= 0.1 ) {
325 318  
326 319 this->velocity = {0.0f, 0.0f, 0.0f };
327 320 this->environment = &_sphereController->_stillEnvironment;
... ... @@ -337,12 +330,6 @@ namespace collision
337 330 if( this->_state == DynamicPSphere::States::Rolling ) {
338 331 ds = adjustedTrajectory(dt0);
339 332  
340   - if(ds < 20 ) {
341   -
342   - this->velocity = {0.0f, 0.0f, 0.0f };
343   - this->environment = &_sphereController->_stillEnvironment;
344   - }
345   -
346 333 this->translateParent(Mi*ds);
347 334 this->curr_t_in_dt = t;
348 335  
... ... @@ -350,7 +337,7 @@ namespace collision
350 337 auto F = this->externalForces();
351 338 auto c = dt0.count();
352 339 auto a = F * c;
353   - this->velocity += a;
  340 + this->velocity -= a;
354 341 }
355 342 else {
356 343  
... ... @@ -367,10 +354,6 @@ namespace collision
367 354 this->velocity += a;
368 355 }
369 356 }
370   -
371   -// myFile.close();
372   -
373   -
374 357 }
375 358  
376 359  
... ... @@ -387,7 +370,7 @@ namespace collision
387 370 }
388 371  
389 372  
390   -
  373 + /*** Created in collaboration with Fatemeh Heidari and Ghada Bouzidi ***/
391 374 GMlib::Vector<double,3>
392 375 DynamicPhysObject<GMlib::PSphere<float> >::adjustedTrajectory(seconds_type dt) {
393 376  
... ... @@ -446,7 +429,6 @@ namespace collision
446 429 for( auto& sphere : _dynamic_spheres) {
447 430  
448 431 dynamicCollision(sphere, seconds_type(dt));
449   -
450 432 }
451 433 // Make Collision unique
452 434 sortAndMakeUnique(_collisions);
... ... @@ -507,6 +489,7 @@ namespace collision
507 489  
508 490 dynamicCollision(sphere, seconds_type(dt));
509 491  
  492 +
510 493 }
511 494  
512 495 detectStateChanges(dt);
... ... @@ -563,6 +546,7 @@ namespace collision
563 546 for( auto& sphere : _dynamic_spheres) {
564 547  
565 548 dynamicCollision(sphere, seconds_type(dt));
  549 +
566 550 }
567 551  
568 552 detectStateChanges(dt);
... ... @@ -597,7 +581,7 @@ namespace collision
597 581  
598 582 auto sphere = state.obj;
599 583 auto newState = state.state;
600   - auto Statetime = state.time;
  584 + auto StateTime = state.time;
601 585 auto planes = state.attachedPlanes;
602 586  
603 587 std::cout << "handleStates says the state is now " << int(newState) << " after being " << int(sphere->_state) << std::endl;
... ... @@ -606,14 +590,15 @@ namespace collision
606 590  
607 591 detachObjects(sphere);
608 592 sphere->_state = newState;
609   - sphere->simulateToTInDt(Statetime);
  593 + sphere->simulateToTInDt(seconds_type());
610 594 }
611 595  
612 596 else {
613 597  
614 598 setAttachedObjects(planes, sphere);
615 599 sphere->_state = newState;
616   - sphere->simulateToTInDt(Statetime);
  600 +
  601 + sphere->simulateToTInDt(seconds_type(dt));
617 602 }
618 603 }
619 604  
... ... @@ -668,18 +653,16 @@ namespace collision
668 653  
669 654 }
670 655  
671   - // Additional collisions for the dynamic objects in the collision_object
672   - // Not allowed: Same collision twice, cannot collide with itself
673   -
674   - // If the dynamic object (obj1) is a sphere
  656 + // Additional collisions are detected
  657 + // If the dynamic object (obj1) is a sphere
675 658 if( d_sphere_1) {
676 659  
677   - dynamicCollision(d_sphere_1, seconds_type(dt)); // Does it collide with any dynamic objects? Can't with same obj as last time
  660 + dynamicCollision(d_sphere_1, seconds_type(dt));
678 661  
679 662 // If sphere 1 collided with a dynamic sphere, check for that sphere's future collisions
680 663 if(d_sphere_2) {
681 664  
682   - dynamicCollision(d_sphere_2, seconds_type(dt)); // Does it collide with any dynamic objects? Can't with sphere 1
  665 + dynamicCollision(d_sphere_2, seconds_type(dt));
683 666 }
684 667  
685 668 }
... ... @@ -688,8 +671,6 @@ namespace collision
688 671 void
689 672 MyController::detectStateChanges(double dt) {
690 673  
691   - // predicate here to check if allready in _singularities
692   -
693 674 for( auto& sphere : _dynamic_spheres) {
694 675  
695 676 auto singularity = detectStateChange(sphere, dt);
... ... @@ -702,7 +683,7 @@ namespace collision
702 683  
703 684 }
704 685  
705   - //**** Code developed with help from Ghada Bouzidi and Fatemeh Heidari *****
  686 + //**** Created in collaboration with Ghada Bouzidi and Fatemeh Heidari *****
706 687  
707 688 StateChangeObj
708 689 MyController::detectStateChange(DynamicPSphere *sphere, double dt) {
... ...
include/collision_library.h
  1 +/***
  2 + * A library for detecting collisions between Dynamic and Static Phys Objects based on a collision interface and GMlib .
  3 + * Made by Bjørn-Richard Pedersen for STE6245 Advanced game and sim programming 2016
  4 + * with assistance from teachers Runo Dalmo and Jostein Bratli as well as fellow students Fatemeh Heidari and Ghada Bouzidi
  5 + *
  6 + * 05.03.2017
  7 + */
  8 +
1 9 #ifndef COLLISION_LIBRARY_H
2 10 #define COLLISION_LIBRARY_H
3 11  
... ... @@ -10,7 +18,6 @@
10 18 #include <iostream>
11 19 #include <fstream>
12 20  
13   -
14 21 namespace collision
15 22 {
16 23  
... ... @@ -65,6 +72,9 @@ private:
65 72 template <typename T_s>
66 73 void dynamicCollision(T_s* sphere, seconds_type dt);
67 74  
  75 + template <typename T_s>
  76 + void staticCollision(T_s* sphere, seconds_type dt);
  77 +
68 78 template <class Container>
69 79 void sortAndMakeUniqueStates(Container& c);
70 80  
... ... @@ -227,6 +237,7 @@ void MyController::crossUnique(Container_1 ColContainer, Container_2 stateContai
227 237  
228 238 }
229 239  
  240 + // Does the same thing for the Smaller container as the if(placed == false) check above does for the Larger container
230 241 for( auto& state : stateContainer) {
231 242  
232 243 if(amIinState(_newStateOjects, state) == false ) _newStateOjects.push_back(state);
... ... @@ -385,9 +396,6 @@ void MyController::dynamicCollision(T_s* dyn_obj, seconds_type dt) {
385 396 // Check only for collisions for the first dynamic sphere
386 397 auto col = detectCollision(*dyn_obj, **iter, dt);
387 398  
388   - // The check for if the second object is a dynamic sphere is done in the main algorithm
389   - // and the method is called again, only with the 1'st and 2'nd sphere swapped
390   -
391 399 if( col.CollisionState::flag == CollisionStateFlag::Collision ) {
392 400  
393 401 auto& first_sphere = dyn_obj;
... ...