Question

I'm making a game, and I have to store some datas of sprites (e.g. position, Image etc) in a collection. And the question is which collection should I use?

I, My main problem is the collision detection:

1, First time I used a simple ArrayList, but when I have to check whether the actual sprite collide with other sprite I have to loop over all the other sprites to find out it. I thought its a bit too much of effort.

2, Second time I thought maybe ConcurrentHashMap would be the ideal. So when I try to move a sprite to an other place I can detect if it is a reserved place or not.

II, And then comes the other problem:

1, When I'm updating the ArrayList I can just loop over all the sprites and overwrite the old positions.

2, But when I'm updating the ConcurrentHashMap I have to remove the actual entry from the map and put a new instead of it (I can't just change the key, cause the value won't follow it). So here we got the performance problem with remove and put.

Thanks in advance.

Was it helpful?

Solution

It seems unusual to me to base your collision on the collection type, unless that collection is a 2D/3D grid with one object per position, represented as an array. If it is, you can check a precise point on that array in O(1).

If you're doing collision precisely, in pixels or vectors, you probably will have to iterate.

I'd use a LinkedList of some kind in that case, because you'll probably be removing things on collisions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top