In my game world I have walls represented as a rectangle with a rectangular bounding box. I also have guns shooting laser beams. These laser beams are also represented by rectangles with a rectangular bounding box, although they are long and slim with typical aspect ratios of 40 - 50.
Now I want to compute whether these laser beams intersect the wall, and if they do I want to damage the wall. The problem is that it seems the calculations become correct in some instances and erroneous in other cases.
I have added some images to show some correct hits and some erroneous ones. The two first are hits even though they should not be, while the third is correctly NOT a hit. Never mind that they are super ugly (graphics will be great later) 😃
It seems that the distance from the laser beam matters in some instances, though I'm not sure. Currently the idea is to check if any of the line segments of the laser beam intersects the wall by using the intersectsSegment method of the SkeletonBounds class. My current take looks like this:
/**Method to check if two ActorBaseClasses intersects each other. This method first
* performs simple hit detection to see if they are within the axis aligned bounding
* boxes of one another and, if so, performs advanced collision detection.*/
public ActorBaseClass intersects (ActorBaseClass item) {
/*Updates skeleton bounds if not already done at this time step*/
this.updateSkeletonBounds();
item.updateSkeletonBounds();
/*Doing basic intersection test.*/
boolean aabbIntersects = false;
if ( this.skeletonBounds.aabbIntersectsSkeleton(item.getSkeletonBounds()) )
aabbIntersects = true;
/*Basic intersection test failed.*/
if ( !aabbIntersects )
return null;
/*Doing advanced intersection test.*/
for ( int i = 0, n = item.getSkeletonBounds().getBoundingBoxes().size; i < n; i++ ){ // For all BBs of THIS
BoundingBoxAttachment itemBoundingBox = item.getSkeletonBounds().getBoundingBoxes().get(i);
FloatArray itemPolygon = item.getSkeletonBounds().getPolygon(itemBoundingBox);
float[] itemVertices = itemPolygon.items;
float x1, y1, x2, y2;
for ( int ii = 0, nn = itemVertices.length; ii < nn; ii += 2 ){
x1 = itemVertices[ii];
y1 = itemVertices[ii+1];
if ( ii == itemVertices.length - 2 ){ // Out of array bounds, check last line segment
x2 = itemVertices[0];
y2 = itemVertices[1];
}
else{
x2 = itemVertices[ii+2];
y2 = itemVertices[ii+3];
}
if ( this.getSkeletonBounds().intersectsSegment(x1, y1, x2, y2) != null )
return this;
}
}
/*Advanced intersection test failed.*/
return null;
}
Can anyone tell if I have some logical failures anywhere? I don't think that using the containsPoint() methods will help in this case.