-
-
Save evieluvsrainbows/4578458 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Project Name: CollisionDetection | |
* Solution Name: MandelbrotCalc | |
* Original creation date: 25/03/2011 | |
* Edit date: 19/01/2013 | |
* Programmer name: Jamie Taylor (aka "GaProgMan") | |
* File name: CollisionDetection.cpp | |
* | |
* Purpose of the project: | |
* To re-write the psuedo-code found in a previous | |
* project relating to 2D collision detection. | |
* Original code can be found here: http://bit.ly/ibfsLJ | |
* Description can be found here: http://wp.me/p19MGD-eu | |
* | |
* GNU Copyright information | |
* Copyright 2011 Jamie Taylor <[email protected]> | |
* | |
* This program is free software; you can redistribute | |
* it and/or modify it under the terms of the GNU General | |
* Public License as published by the Free Software | |
* Foundation; either version 2 of the License, or (at | |
* your option) any later version. | |
* | |
* This program is distributed in the hope that it will | |
* be useful, but WITHOUT ANY WARRANTY; without even the | |
* implied warranty of MERCHANTABILITY or FITNESS FOR A | |
* PARTICULAR PURPOSE. See the GNU General Public | |
* License for more details. | |
* | |
* You should have received a copy of the GNU General | |
* Public License along with this program; if not, write | |
* to the Free Software Foundation, Inc., 51 Franklin | |
* Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
*/ | |
#include <math.h> | |
#include <stdio.h> | |
#include <iostream> | |
using namespace std; | |
float _lineOne[4] = {0, 1, 4, 3}; //represents a line of (0, 1) to (4, 3) | |
float _lineTwo[4] = {1, 2, 3, 2}; //represents a line of (1, 2) to (3, 2) | |
float denominator, ua, ub = 0; //represents the values of the denominator, ua and ub respecitvely | |
float collisionPoints[2]; //represents the values of the collision, if there is one | |
float _sectionOne, _sectionTwo; //both are used to calculate the denominator in ua and ub | |
bool calculateDenominator(); | |
float calculateUAValue(); | |
float calculateUBValue(); | |
float calculateXCollision(); | |
float calculateYCollision(); | |
int main () { | |
char ch; | |
if (calculateDenominator()) { | |
/* | |
* If the denominator is NOT 0, then we have a collision. Work out the denominator | |
*/ | |
denominator = _sectionOne - _sectionTwo; | |
cout << "section 1 = " << _sectionOne << endl; | |
cout << "section 2 = " << _sectionTwo << endl; | |
cout << "denominator = " << denominator << endl; | |
ua = calculateUAValue(); | |
ub = calculateUBValue(); | |
cout << "ua = " << ua << endl; | |
cout << "ub = " << ub << endl; | |
if ( ( ua > 0.0f ) && ( ub > 0.0f ) ) { | |
/* | |
* We've definately got a collision, so we need to work out | |
* where the collision is | |
*/ | |
collisionPoints[0] = calculateXCollision(); | |
collisionPoints[1] = calculateYCollision(); | |
cout << "collision point x = " << collisionPoints[0] << endl; | |
cout << "collision point y = " << collisionPoints[1] << endl; | |
} | |
} | |
//code used to halt execution at end of program | |
cin >> ch; | |
return 0; | |
} | |
bool calculateDenominator(){ | |
/* | |
* This code calculates the value of the denominators used | |
* informulae 3 and 4 (above). These forumlae are used to | |
* calculate the values of ua and ub, respectively. | |
* | |
* This function will return TRUE if the denominator is | |
* NOT 0, and FALSE if it is 0. | |
* | |
* The denominator takes this form: | |
* (y4 - y3)(x2 - x1) - (x4 - x3)(y2 - y1) | |
* | |
* x1, y1, x2 and y2 are related to _lineOne | |
* x3, y3, x4 and y4 are related to _lineTwo | |
*/ | |
_sectionOne = (_lineTwo[3] - _lineTwo[1]) * (_lineOne[2] - _lineOne[0]); | |
_sectionTwo = (_lineTwo[2] - _lineTwo[0]) * (_lineOne[3] - _lineOne[1]); | |
/* | |
* if (y4 - y3)(x2 - x1) - (x4 - x3)(y2 - y1) is anything BUT | |
* 0, then we return TRUE, as there is a collision. | |
* However, if not there is not collision. So return FALSE. | |
*/ | |
if ( ( _sectionOne - _sectionTwo ) != 0 ) | |
return true; | |
else | |
return false; | |
} | |
float calculateUAValue(){ | |
/* | |
* This code calculates the value of ua (formulae 3, above). | |
* | |
* This function will return the value of ua. | |
* | |
* ua takes the following form: | |
* ua = (x4 - x3)(y1 - y3) - (y4 - y3)(x1 - x3) | |
* (y4 - y3)(x2 - x1) - (x4 - x3)(y2 - y1) | |
* | |
* x1, y1, x2 and y2 are related to _lineOne | |
* x3, y3, x4 and y4 are related to _lineTwo | |
* | |
* We already know the value of the denominator, so we only | |
* have to calcuate the value of the enumerator | |
*/ | |
float enumeratorOne = (_lineTwo[2] - _lineTwo[0]) * (_lineOne[1] - _lineTwo[1]); | |
float enumeratorTwo = (_lineTwo[3] - _lineTwo[1]) * (_lineOne[0] - _lineTwo[0]); | |
return (enumeratorOne - enumeratorTwo) / denominator; | |
} | |
float calculateUBValue(){ | |
/* | |
* This code calculates the value of ub (formulae 4, above). | |
* | |
* This function will return the value of ua. | |
* | |
* ub takes the following form: | |
* ub = (x2 - x1)(y1 - y3) - (y2 - y1)(x1 - x3) | |
(y4 - y3)(x2 - x1) - (x4 - x3)(y2 - y1) | |
* | |
* x1, y1, x2 and y2 are related to _lineOne | |
* x3, y3, x4 and y4 are related to _lineTwo | |
* | |
* We already know the value of the denominator, so we only | |
* have to calcuate the value of the enumerator | |
*/ | |
float enumeratorOne = (_lineOne[2] - _lineOne[0]) * (_lineOne[1] - _lineTwo[1]); | |
float enumeratorTwo = (_lineOne[3] - _lineOne[1]) * (_lineOne[0] - _lineTwo[0]); | |
return (enumeratorOne - enumeratorTwo) / denominator; | |
} | |
float calculateXCollision() { | |
/* | |
* This code calculates the x value for a collision between | |
* 2 lines using formula 1 (above) | |
* | |
* This function will return the x value fo the collision. | |
* | |
* The formula to calculate the value of x takes the following | |
* form: | |
* x1 + ua (x2 - x1) = x3 + ub (x4 - x3) | |
* | |
* x1, y1, x2 and y2 are related to _lineOne | |
* x3, y3, x4 and y4 are related to _lineTwo | |
* | |
*/ | |
return (_lineOne[0] + (ua * (_lineOne[2] - _lineOne[0]))); | |
} | |
float calculateYCollision() { | |
/* | |
* This code calculates the y value for a collision between | |
* 2 lines using formula 2 (above) | |
* | |
* This function will return the y value fo the collision. | |
* | |
* The formula to calculate the value of y takes the following | |
* form: | |
* y1 + ua (y2 - y1) = y3 + ub (y4 - y3) | |
* | |
* x1, y1, x2 and y2 are related to _lineOne | |
* x3, y3, x4 and y4 are related to _lineTwo | |
* | |
*/ | |
return (_lineOne[1] + (ua * (_lineOne[3] - _lineOne[1]))); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment