-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollision.cpp
More file actions
50 lines (40 loc) · 3.01 KB
/
Copy pathCollision.cpp
File metadata and controls
50 lines (40 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//Ivaylo Ivanov
//This cpp should always include Collision.h header file
#include "Collision.h"
/*========================================================Sphere Collision=================================================================*/
bool Collision::sphereToSphereCollision(float sphereXPositionOne, float sphereZPositionOne, float sphereRadiusOne, float sphereXPositionTwo, float sphereZPositionTwo, float sphereRadiusTwo)
{
//Its used to calculate the collision between two object like spheres using the sum of the two radii and checking them against the coordinates
float distanceX = sphereXPositionTwo - sphereXPositionOne;
float distanceZ = sphereZPositionTwo - sphereZPositionOne;
float distance = sqrt(distanceX * distanceX + distanceZ * distanceZ);
return(distance < (sphereRadiusOne + sphereRadiusTwo));
}
/*========================================================Point to Sphere Collision=================================================================*/
bool Collision::pointToSphereCollision(float sphereXPositionOne, float sphereZPositionOne, float sphereRadiusOne, float sphereXPositionTwo, float sphereZPositionTwo)
{
//Its used to determine if an object has passed through a set of coordinates checking them against the object radius
float distanceX = sphereXPositionTwo - sphereXPositionOne;
float distanceZ = sphereZPositionTwo - sphereZPositionOne;
float distance = sqrt(distanceX * distanceX + distanceZ * distanceZ);
return(distance < (sphereRadiusOne));
}
/*========================================================Box Collision=================================================================*/
Collision::boxSides Collision::sphereToBoxCollision(float sphereXPosition, float sphereZPosition, float sphereRadius, float oldSphereXPosition, float oldSphereZPosition,
float boxObjectXPosition, float boxObjectZPosition, float boxObjectWidth, float boxObjectDepth)
{/*Calculation of the object boundaries to check for collision between the two objects*/
float minimumXBoundary = boxObjectXPosition - (boxObjectWidth / 2) - sphereRadius;
float maximumXBoundary = boxObjectXPosition + (boxObjectWidth / 2) + sphereRadius;
float minimumZBoundary = boxObjectZPosition - (boxObjectDepth / 2) - sphereRadius;
float maximumZBoundary = boxObjectZPosition + (boxObjectDepth / 2) + sphereRadius;
Collision::boxSides result = Collision::noSide;
if (sphereXPosition > minimumXBoundary && sphereXPosition<maximumXBoundary && sphereZPosition>minimumZBoundary && sphereZPosition < maximumZBoundary)//checks if sphere has collided with the box
{
//checks with a statement to decide which side of the object is hit
if (oldSphereXPosition < minimumXBoundary) result = Collision::leftSide;
else if (oldSphereXPosition > maximumXBoundary) result = Collision::rightSide;
else if (oldSphereZPosition < minimumZBoundary) result = Collision::frontSide;
else result = Collision::backSide;
}
return(result);//returns the result to loop where the function is called
}