Created
March 1, 2015 12:43
-
-
Save Zerophase/7469832bc914ce6ab3aa to your computer and use it in GitHub Desktop.
Sweep
This file contains hidden or 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
public class Sweep | |
{ | |
AABBIntersection aabbIntersection = new AABBIntersection(); | |
private AABB3D b = new AABB3D(); | |
public bool TestMovingAABB(AABB3D b0, Vector3 d, float time0, float time1, | |
AABB3D b1, ref float time) | |
{ | |
float mid = (time0 + time1) * 0.5f; | |
float midTest = mid - time0; | |
Vector3 adjustedD = d * mid; | |
b.Center = b0.Center + adjustedD; | |
b.HalfWidths[0] = midTest * d.magnitude + b0.HalfWidth; | |
b.HalfWidths[1] = midTest * d.magnitude + b0.HalfHeight; | |
b.HalfWidths[2] = midTest * d.magnitude + b0.HalfDepth; | |
if (!aabbIntersection.Intersect(b, b1)) | |
return false; | |
if (time1 - time0 < 0.1f) | |
{ | |
normalCollision(b, b0, b1); | |
time = time0; | |
return true; | |
} | |
if (TestMovingAABB(b0, d, time0, mid, b1, ref time)) | |
return true; | |
return TestMovingAABB(b0, d, mid, time1, b1, ref time); | |
} | |
private float wy; | |
private float hx; | |
private void normalCollision(AABB3D movingBox, AABB3D b0, AABB3D b1) | |
{ | |
resetNormals(b0); | |
wy = (movingBox.HalfWidth * 2 + b1.HalfWidth * 2) * (movingBox.Center.y - b1.Center.y); | |
hx = (movingBox.HalfHeight * 2 + b1.HalfHeight * 2) * (movingBox.Center.x - b1.Center.x); | |
if (wy > hx) | |
{ | |
if (wy > -hx) | |
{ | |
b0.NormalCollision[1] = Vector3.down; | |
} | |
else | |
{ | |
b0.NormalCollision[0] = Vector3.right; | |
} | |
} | |
else | |
{ | |
if (wy > -hx) | |
{ | |
b0.NormalCollision[0] = Vector3.left; | |
} | |
else | |
{ | |
b0.NormalCollision[1] = Vector3.up; | |
} | |
} | |
} | |
public void NormalCollision(AABB3D a, AABB3D b) | |
{ | |
resetNormals(a); | |
wy = (a.HalfWidth * 2 + b.HalfWidth * 2) * (a.Center.y - b.Center.y); | |
hx = (a.HalfHeight * 2 + b.HalfHeight * 2) * (a.Center.x - b.Center.x); | |
if (wy > hx) | |
{ | |
if (wy > -hx) | |
{ | |
a.NormalCollision[1] = Vector3.down; | |
} | |
else | |
{ | |
a.NormalCollision[0] = Vector3.right; | |
} | |
} | |
else | |
{ | |
if (wy > -hx) | |
{ | |
a.NormalCollision[0] = Vector3.left; | |
} | |
else | |
{ | |
a.NormalCollision[1] = Vector3.up; | |
} | |
} | |
} | |
private void resetNormals(AABB3D checkedObject) | |
{ | |
for (int i = 0; i < checkedObject.NormalCollision.Length; i++) | |
{ | |
checkedObject.NormalCollision[i] = Vector3.zero; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment