This is a program I designed that implements a little bit of vector-math to determine the collision between 2 vectors.

Hopefully this program will be useful to those out there designing 2D graphics programs and need a better solution to determining graphical object-to-object collisions when two objects meet, without having to rely on the famous "bounding box" that many graphical objects are defined by. One can simply implement this class and define their object to consist of these vectors and call upon the Set command for each vector whenever the graphical object is moved.

This is still experimental code, but the tests show that if the vectors meet at a point or intersect then there will be an Intersection met notice when the vectors are compared.

This class does NOT deal with the difference of shapes within shapes. If a shape is fully inscribed within another shape then a vector collision may fail. However, this may be useful if programs want to implement objects bouncing within other objects.

137 Views
``````#include <cstdlib>
#include <iostream>
#include <math.h>
#include <iomanip>

/*
*A Simple collision-detecting class
*Made by Alex Edwards, modified by Ancient Dragon
**/
using namespace std;

class Point;
class Triangle;
class P2DVector;

class Point{
private:
double xP;
double yP;

public:
Point(double x, double y){
xP = x;
yP = y;
};
Point() {xP = 0; yP = 0;}
void Set(double x,double y) {xP = x; yP = y;}
double getX(){return xP;};
double getY(){return yP;};
void showCoord(){
cout << "(" << getX() << ", " << getY() << ")" << endl;
};
};

/*
*class P2DVector
*Named P2DVector to differentiate between the vector class and 3D vectors.
**/
class P2DVector{
private:
Point points;

public:
P2DVector(Point& first, Point& second){
points = first;
points = second;
};
P2DVector() {points = Point(0,0); points = Point(0,0); }
void Set(Point& first, Point& second){
points = first;
points = second;
};
double getXDir(){
return (points.getX() - points.getX());
};
double getYDir(){
return (points.getY() - points.getY());
};
double magnitude(){
return sqrt( (pow( points.getX() - points.getX() ,2)
+pow(points.getY() - points.getY() ,2)));
};
Point startPoint(){
Point p(points.getX(), points.getY());
return p;
};
Point endPoint(){
Point p(points.getX(), points.getY());
return p;
};
P2DVector unitP2DVector(){
Point unitPoint;
unitPoint.Set(points.getX() / magnitude(), points.getY() / magnitude());
unitPoint.Set(points.getX() / magnitude(), points.getY() / magnitude());
P2DVector temp (unitPoint, unitPoint);
return temp;
};
void displayLocation(){
cout << "This P2DVector Starts-> " << "" << points.getX() << ", " << points.getY();
cout << "\nEnds-] " << "\t\t\t" << points.getX() << ", " << points.getY();
cout << "\nDirection: "<< "\t\t" << "<" << getXDir() << ", " << getYDir() <<">";
cout << "\nContains magnitude: \t" << magnitude() << "\n" << endl;
};
bool operator==(P2DVector &other){
double otherXDirection = other.getXDir();
double otherYDirection = other.getYDir();
double xDirection = getXDir();
double yDirection = getYDir();

//The statements below are a solution to a system of equations for vector-to-vector collisions
double time2 = ((other.startPoint().getY()-startPoint().getY())-
(((other.getYDir())*((other.startPoint().getX())-(startPoint().getX())))
/(other.getXDir())))/((getYDir())-((other.getYDir())*((getXDir())/(other.getXDir()))));

double time1 =  ((startPoint().getX() - other.startPoint().getX())
+ ((getXDir()) * (time2)))/(other.getXDir());

return time1 >= 0 && time1 <=1 && time2 >= 0 && time2 <=1;
};
};

/*
*A Sample shape that utilizes the vectors
**/
class Triangle{
private:
Point coordinates, shortestCollision;
P2DVector innerVectors, outterVectors;

public:
Triangle(double xLoc, double yLoc, Point points){
Set(xLoc, yLoc, points);
};
void Set(double xLoc, double yLoc, Point points){
for(int i = 0; i < 3; i++)
coordinates[i].Set( xLoc + points[i].getX(), yLoc + points[i].getY());

outterVectors.Set(coordinates, coordinates);
outterVectors.Set(coordinates, coordinates);
outterVectors.Set(coordinates, coordinates);
outterVectors.Set(coordinates, coordinates);
outterVectors.Set(coordinates, coordinates);
outterVectors.Set(coordinates, coordinates);

shortestCollision.Set(outterVectors.startPoint().getX()
+ ((outterVectors.getXDir() ) / 2),outterVectors.startPoint().getY()
+ ((outterVectors.getYDir() ) / 2));
innerVectors.Set(coordinates, shortestCollision);

shortestCollision.Set(outterVectors.startPoint().getX()
+ ((outterVectors.getXDir() ) / 2), outterVectors.startPoint().getY()
+ ((outterVectors.getYDir() ) / 2));
innerVectors.Set(coordinates, shortestCollision);

shortestCollision.Set(outterVectors.startPoint().getX()
+ ((outterVectors.getXDir() ) / 2), outterVectors.startPoint().getY()
+ ((outterVectors.getYDir() ) / 2));
innerVectors.Set(coordinates, shortestCollision);
};

void displayParameters(){
cout << "This triangle is defined by the points/vectors (in this order):\n" << flush;
for(int i = 0; i < 3; i++){
coordinates[i].showCoord();
outterVectors[i].displayLocation();
cout << "\n" << flush;
}
};
Point *getPoints(){
return coordinates;
};
P2DVector *getInnerVectors(){
return innerVectors;
}
P2DVector *getOutterVectors(){
return outterVectors;
}
bool operator==(Triangle &other){
P2DVector myVectors[] = {getOutterVectors(), getOutterVectors(),
getOutterVectors(), getOutterVectors(),
getOutterVectors(), getOutterVectors()};

P2DVector rogueVectors[] = {other.getOutterVectors(), other.getOutterVectors(),
other.getOutterVectors(), other.getOutterVectors(),
other.getOutterVectors(), other.getOutterVectors()};

for(int i = 0; i < 6; i++){
for(int j = 0; j < 6; j++){
if(myVectors[i] == rogueVectors[j]){
cout << "Intersection met!" << endl;
return true;
}
}
}
cout << "No intersection met!" << endl;
return false;
};
};

/*
*Tests the shapes, which are triangles in this case, for a collision.
**/
int main(int argc, char *argv[]){
Point myPoints, otherPoints, finalPoints;
myPoints.Set(0, 0), myPoints.Set(2, 2), myPoints.Set(2, 1);
otherPoints.Set(0, 0), otherPoints.Set(0, 1), otherPoints.Set(1, 0);
finalPoints.Set(0, 0), finalPoints.Set(1, 1), finalPoints.Set(1, 0);
Triangle tri(0, 2, myPoints), otherTri(1.5, 2, otherPoints), finalTri(2, 2, finalPoints);

tri.displayParameters();
otherTri.displayParameters();
finalTri.displayParameters();

(tri == otherTri); //Checking for intersection between the first triangle with the second
(tri == finalTri); //Checking for intersection between the first triangle with the third
(otherTri == finalTri); //Checking for intersection between the secondtriangle with the third

cout << "\n" << endl;
finalTri.Set(1.5, 2, otherPoints);
cout << "Setting the 3rd triangle to same coords as 2nd...\n" << endl;

(tri == otherTri); //Checking for intersection between the first triangle with the second
(tri == finalTri); //Checking for intersection between the first triangle with the third
(otherTri == finalTri); //Checking for intersection between the secondtriangle with the third

cin.get();
return 0;
}``````

I think I finally understand what makes an individual a great programmer.

It's not the degree, and it's not the job...

...it's the discipline that makes an individual a great programmer.

I know of many people at Daniweb that did not go to college and still became great programmers. This is because they had the [u]discipline[/u] to improve their ability and familiarity with their understanding of programming technologies.

I'd like to thank these individuals, and many others who helped me since I came here:

[Note: Individuals with Professor next to their name were a huge influence to my way of thinking.]

Professor Narue (Truly a great, and very inspirational Software Engineer! )
Professor AD [Ancient Dragon] ( An Expert Programmer who knows how to turn complex problems into practical solutions. /salute )
Professor Ezzaral (Another very inspirational person! )
Professor Radical Edward ( Inspirational and absolutely Admirable! You rock!!! Whoo! XD)
Salem (Though strict, very knowledgeable programmer)
~s.o.s~ (Respectable Designer)
Masijade (Backdoor programmer /salute)
stephen84
sciwizeh (Programming Prodigy! =) )
williamhemsworth (Programming Prodigy/ Lil Bro! =P )
VernonDozier (Excellent Mathematician/Algorithm Designer )
Duoas (Very analytical Algorithmic Individual with in-depth knowledge of assembly/C++ languages )
ArkM (Bright thinker and experienced programmer)

More to come! O_O