The lines are made up of 200 straight lines connected.
And I stored the coordinates in a deque so I can pop the first one and generate another coordinate and push it at the back.
How should I approach the collision detection?
Posted 21 October 2012 - 08:26 AM
Posted 21 October 2012 - 09:36 AM
Posted 21 October 2012 - 09:45 AM
Posted 21 October 2012 - 10:07 AM
Since the lines are in 2D and have a cardinal direction in moving from left to right, one direction to take is the binary tree approach, where at each node of the tree, you store a horizontal range [minX, maxX] and the extent how far the border reaches inside that interval. So, for the ceiling line, you'd store the lowest position of the ceiling between [minX, maxX]. Then you subdivide to two child nodes [minX, (minX+maxX)/2] and [(minX+maxX)/2, maxX] and also for those, store the lowest position of the ceiling between those intervals. Keep subdividing until you have only a few lines in the range of the node, at which point you'll do full line-AABB of the ship search.
Another option is to simply subdivide the horizontal screen into, say, 100 buckets with distinct ranges [minX, maxX], and in each bucket store the lines that fall into those buckets. Then when doing a collision check, test the ship only against the buckets which the ship AABB overlaps.
The point of both of these methods is to avoid having to test the ship against each line on the screen.
//note 1: assuming we're in screen space with an inverted y-axis
//note 2: this is pseudo code to illustrate the idea, not for copy/paste -> working!
float[] TopLine = new float[200]; //array index is the X value, array value is the Y value
float[] BottomLine = new float[200];
if(Ship.Y > TopLine[Ship.X] || Ship.Y < BottomLine[Ship.X])
{
return false;
}
return true;
Posted 21 October 2012 - 10:31 AM
Nah, that's too complicated. Just look at the X coordinate of the ship (left and right) and find the X coordinates for the line segment (ideally in O(1) time)) and see if the Y coordinates of the terrain overlap the ship position. The line segments are close enough together that this could work
Posted 21 October 2012 - 10:49 AM
Saying something is too complicated and replacing it with a broken algorithm is not a particularly good advice.
What you are proposing in the pseudo works only if the level/screen width is equal to 200 pixels, meaning that the i'th line is only one pixel wide so that each line has its unique position. If the lines are wider, you cannot unfortunately do the lookup "ideally in O(1) time" without preprocessing. If OP wants to go this route, it requires either a line rasterization algorithm so that the height values can be tracked for each X coordinates, or if line-AABB test is used, a pre-process filler loop that fills an array that contains the associated line for each horizontal pixel. Sure, it's a feasible method as well, but the complexity is then dependent on the screen pixel width in both time and memory consumption.
Edited by slayemin, 21 October 2012 - 10:50 AM.