more fixes for collision detection
authorinsilmaril
Wed, 09 Sep 2009 12:57:06 +0000
changeset 793cac93797c580
parent 792 7d67be709091
child 794 d922fb6ea482
more fixes for collision detection
branchobj.cpp
branchobj.h
mainwindow.cpp
mapeditor.cpp
treemodel.cpp
treemodel.h
version.h
     1.1 --- a/branchobj.cpp	Tue Sep 08 12:15:39 2009 +0000
     1.2 +++ b/branchobj.cpp	Wed Sep 09 12:57:06 2009 +0000
     1.3 @@ -713,6 +713,19 @@
     1.4  	return bboxTotal;
     1.5  }
     1.6  
     1.7 +ConvexPolygon BranchObj::getBoundingPolygon()
     1.8 +{
     1.9 +	if (treeItem->branchCount()==0)
    1.10 +		return MapObj::getBoundingPolygon();
    1.11 +
    1.12 +	QPolygonF p;
    1.13 +	p<<bboxTotal.topLeft();
    1.14 +	p<<bboxTotal.topRight();
    1.15 +	p<<bboxTotal.bottomRight();
    1.16 +	p<<bboxTotal.bottomLeft();
    1.17 +	return p;
    1.18 +}
    1.19 +
    1.20  void BranchObj::calcBBoxSizeWithChildren()
    1.21  {	
    1.22  	// This is initially called only from reposition and
     2.1 --- a/branchobj.h	Tue Sep 08 12:15:39 2009 +0000
     2.2 +++ b/branchobj.h	Wed Sep 09 12:57:06 2009 +0000
     2.3 @@ -56,6 +56,7 @@
     2.4  	virtual QPolygonF shape();				//!< Returns arbitrary bounding polygon
     2.5  	virtual QRectF getTotalBBox();			// return BBox including children			
     2.6  	virtual QRectF getBBoxSizeWithChildren();	// return size of BBox including children  
     2.7 +	virtual ConvexPolygon getBoundingPolygon();
     2.8  	virtual void calcBBoxSizeWithChildren();	// calc size of  BBox including children recursivly
     2.9  
    2.10  	virtual QString getSelectString();
     3.1 --- a/mainwindow.cpp	Tue Sep 08 12:15:39 2009 +0000
     3.2 +++ b/mainwindow.cpp	Wed Sep 09 12:57:06 2009 +0000
     3.3 @@ -1478,6 +1478,7 @@
     3.4  
     3.5      a = new QAction( "Test function 2" , this);
     3.6      a->setStatusTip( "Call test function 2" );
     3.7 +	a->setShortcut (Qt::SHIFT + Qt::Key_T);
     3.8  	testMenu->addAction (a);
     3.9      connect( a, SIGNAL( triggered() ), this, SLOT( testFunction2() ) );
    3.10  
     4.1 --- a/mapeditor.cpp	Tue Sep 08 12:15:39 2009 +0000
     4.2 +++ b/mapeditor.cpp	Wed Sep 09 12:57:06 2009 +0000
     4.3 @@ -562,11 +562,15 @@
     4.4  	// Create list with all bounding polygons
     4.5  	QList <MapObj*> mapobjects;
     4.6  	QList <ConvexPolygon> polys; 
     4.7 +	ConvexPolygon p;
     4.8  	QList <Vector> vectors;
     4.9  	QList <Vector> orgpos;
    4.10 +	QStringList headings;	//FIXME-3 testing only
    4.11 +	Vector v;
    4.12  	BranchItem *bi;
    4.13 +	BranchItem *bi2;
    4.14  	BranchObj *bo;
    4.15 -	TreeItem *ri=model->getRootItem();
    4.16 +	BranchItem *ri=model->getRootItem();
    4.17  	for (int i=0;i<ri->branchCount();++i)
    4.18  	{
    4.19  		bi=ri->getBranchNum (i);
    4.20 @@ -574,11 +578,28 @@
    4.21  		if (bo)
    4.22  		{
    4.23  			mapobjects.append (bo);
    4.24 -			polys.append(bo->getBoundingPolygon());
    4.25 -			polys[i].calcCentroid();
    4.26 +			p=bo->getBoundingPolygon();
    4.27 +			p.calcCentroid();
    4.28 +			polys.append(p);
    4.29  			vectors.append (QPointF(0,0));
    4.30 -			orgpos.append (polys[i].at(0));
    4.31 -		}	
    4.32 +			orgpos.append (p.at(0));
    4.33 +			headings.append (bi->getHeading());
    4.34 +		}
    4.35 +		for (int j=0;j<bi->branchCount();++j)
    4.36 +		{
    4.37 +			bi2=bi->getBranchNum (j);
    4.38 +			bo=(BranchObj*)bi2->getLMO();
    4.39 +			if (bo)
    4.40 +			{
    4.41 +				mapobjects.append (bo);
    4.42 +				p=bo->getBoundingPolygon();
    4.43 +				p.calcCentroid();
    4.44 +				polys.append(p);
    4.45 +				vectors.append (QPointF(0,0));
    4.46 +				orgpos.append (p.at(0));
    4.47 +				headings.append (bi2->getHeading());
    4.48 +			}	
    4.49 +		}
    4.50  	}
    4.51  
    4.52  	// Iterate moving bounding polygons until we have no more collisions
    4.53 @@ -593,25 +614,31 @@
    4.54  				if (polygonCollision (polys.at(i),polys.at(j), QPointF(0,0)).intersect )
    4.55  				{
    4.56  					collisions++;
    4.57 -					Vector v=polys.at(j).centroid()-polys.at(i).centroid();
    4.58 +					v=polys.at(j).centroid()-polys.at(i).centroid();
    4.59  					// Move also away if centroids are identical
    4.60  					if (v.isNull()) 
    4.61  					{
    4.62 +						//cout << "v==0="<<polys[i].centroid()<<polys[j].centroid()<<" "<<v<<"  "<<headings[i].toStdString()<<" - "<<headings[j].toStdString()<<"  ";
    4.63  						v.setX (rand()%200 -100);
    4.64  						v.setY (rand()%200 -100);
    4.65 +						//cout << v;
    4.66  					}
    4.67  					v.normalize();
    4.68 -					cout << "v="<<v<<endl;
    4.69  					v.scale (2);
    4.70 +					//cout <<  "  "<<v<<endl;
    4.71  					vectors[j]=v;
    4.72  					vectors[i]=v;
    4.73  					vectors[i].invert();
    4.74 -				} 
    4.75 +				}  
    4.76  			}
    4.77  		}
    4.78  		for (int i=0;i<vectors.size();i++)
    4.79 +		{
    4.80 +			//cout << " v="<<vectors[i]<<" "<<headings[i].toStdString()<<endl;
    4.81  			polys[i].translate (vectors[i]);
    4.82 +		}
    4.83  		//cout << "Collisions: "<<collisions<<endl;
    4.84 +		//collisions=0;
    4.85  	}	
    4.86  
    4.87  	// Finally move the real objects and update 
     5.1 --- a/treemodel.cpp	Tue Sep 08 12:15:39 2009 +0000
     5.2 +++ b/treemodel.cpp	Wed Sep 09 12:57:06 2009 +0000
     5.3 @@ -277,7 +277,7 @@
     5.4      return NULL;
     5.5  }
     5.6  
     5.7 -TreeItem *TreeModel::getRootItem()
     5.8 +BranchItem *TreeModel::getRootItem()
     5.9  {
    5.10  	return rootItem;
    5.11  }
     6.1 --- a/treemodel.h	Tue Sep 08 12:15:39 2009 +0000
     6.2 +++ b/treemodel.h	Wed Sep 09 12:57:06 2009 +0000
     6.3 @@ -35,11 +35,11 @@
     6.4  				const QModelIndex & parent = QModelIndex() ); 
     6.5  
     6.6  	TreeItem* getItem (const QModelIndex &index) const;
     6.7 -	TreeItem* getRootItem();
     6.8 +	BranchItem* getRootItem();
     6.9  
    6.10  
    6.11  protected:
    6.12 -    TreeItem *rootItem;
    6.13 +    BranchItem *rootItem;
    6.14  };
    6.15  
    6.16  #endif
     7.1 --- a/version.h	Tue Sep 08 12:15:39 2009 +0000
     7.2 +++ b/version.h	Wed Sep 09 12:57:06 2009 +0000
     7.3 @@ -7,7 +7,7 @@
     7.4  #define __VYM_VERSION "1.13.0"
     7.5  //#define __VYM_CODENAME "Codename: RC-1"
     7.6  #define __VYM_CODENAME "Codename: development version, not for production!"
     7.7 -#define __VYM_BUILD_DATE "2009-09-08"
     7.8 +#define __VYM_BUILD_DATE "2009-09-09"
     7.9  
    7.10  
    7.11  bool checkVersion(const QString &);