Created
July 17, 2009 02:08
-
-
Save kirbysayshi/148810 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
package com.kirbySaysHi.collisions | |
{ | |
import org.papervision3d.core.math.AxisAlignedBoundingBox; | |
import org.papervision3d.core.math.Number3D; | |
import org.papervision3d.core.math.Plane3D; | |
public class PV3DSegmentIntersectDetector | |
{ | |
private static var _lastIntersect:Number3D = new Number3D(); | |
private static const EPSILON:Number = 0.000001; | |
private static const ALPHA:Number = 1.5; | |
public function PV3DSegmentIntersectDetector() | |
{ | |
// does nothing | |
} | |
public static function check(start:Number3D, end:Number3D, bodyPos:Number3D, box:AxisAlignedBoundingBox):Boolean | |
{ | |
// initialize stuff | |
var plane:Plane3D = new Plane3D(); | |
// we don't want to accidentally work on the actual position of the body! | |
var object:Number3D = bodyPos.clone(); | |
// this makes no sense, but the values all need to be opposites | |
// otherwise, the plane.setNormalAndPoint will literally give the opposite | |
// intersection | |
object.multiplyEq(-1); | |
// define our plane, it should be facing the start point of the ray, | |
// and located at the position of the object to test against | |
plane.setNormalAndPoint(start, object); | |
var intersect:Number3D = plane.getIntersectionLineNumbers(start, end); | |
// flip them back so we can test if the intersection occurred in a valid place | |
object.multiplyEq(-1); | |
// determine the length of the segment/vector | |
var segmentLength:Number = (start.x - end.x) * (start.x - end.x) + | |
(start.y - end.y) * (start.y - end.y) + | |
(start.z - end.z) * (start.z - end.z); | |
// find the distance from the intersect to the segment, | |
// then grab a ratio of the intersect vs segment length | |
// < 0 means 'behind', > 1 means 'ahead', else is ON SEGMENT | |
var t:Number = Number3D.dot( | |
Number3D.sub(end, start), Number3D.sub(intersect, start) ) | |
/ segmentLength; | |
// intersection line numbers returns where, relative to the body, the intersection will or has occurred | |
if( intersect.x >= box.minX + object.x && intersect.x <= box.maxX + object.x && | |
intersect.y >= box.minY + object.y && intersect.y <= box.maxY + object.y && | |
intersect.z >= box.minZ + object.z && intersect.z <= box.maxZ + object.z && | |
t >= 0 && t <= 1) | |
{ | |
_lastIntersect.copyFrom(intersect); | |
return true; | |
} | |
else | |
{ | |
_lastIntersect.reset(); | |
return false; | |
} | |
} | |
public static function get lastIntersection():Number3D | |
{ | |
return _lastIntersect; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment