Accname
2D-Graphics enthusiast
- Reaction score
- 1,462
Hi guys.
I was thinking alot about collision detection in a 2D game recently. More specifically the collision with a static terrain which doesnt change anymore after creation.
Lets say our game object has a position, defined by its X and Y coordinate, and it wants to travel to another position, also defined by X and Y, and we wanna know if this movement is possible or if it collides with the terrain.
I am basically looking at 2 different ways of implementing it. The first way is to have a big two dimensional array for the map. I can specify a cell size for flexible accuracy and i just check all points between the original position and the final destination.
Problem is, the faster the object is moving, that is, the further away the target location is from the original location, the more calculations need to be done.
But its very simple calculations. Just find the next point, check with the array, if there was a collision then stop, if not, continue until the target is reached.
The other way i was thinking about was to define all collision with the terrain by points in a coordinate system which represent line segments. Then, i could check if the line segment formed by the objects original position and target location intesects with any collision line segments in the map.
The calculation to determine whether two line segments collide is a little bit more complex, but its indepent on the length of the lines.
Now the performance would depend on the number of collision lines within the proximity of the object.
So the first solution will probably need more RAM in a conventional scenario whereas the second solution might need more processing power for slow moving objects and less processing power for very fast objects.
Any suggestions?
I was thinking alot about collision detection in a 2D game recently. More specifically the collision with a static terrain which doesnt change anymore after creation.
Lets say our game object has a position, defined by its X and Y coordinate, and it wants to travel to another position, also defined by X and Y, and we wanna know if this movement is possible or if it collides with the terrain.
I am basically looking at 2 different ways of implementing it. The first way is to have a big two dimensional array for the map. I can specify a cell size for flexible accuracy and i just check all points between the original position and the final destination.
Problem is, the faster the object is moving, that is, the further away the target location is from the original location, the more calculations need to be done.
But its very simple calculations. Just find the next point, check with the array, if there was a collision then stop, if not, continue until the target is reached.
The other way i was thinking about was to define all collision with the terrain by points in a coordinate system which represent line segments. Then, i could check if the line segment formed by the objects original position and target location intesects with any collision line segments in the map.
The calculation to determine whether two line segments collide is a little bit more complex, but its indepent on the length of the lines.
Now the performance would depend on the number of collision lines within the proximity of the object.
So the first solution will probably need more RAM in a conventional scenario whereas the second solution might need more processing power for slow moving objects and less processing power for very fast objects.
Any suggestions?