While working on the project, it was really just for fun, I ran into some problems.
There is a two-dimensional world inhabited by round balls, pointed triangles, and skinny lines (and possibly other wildlife). All of them are subclasses of WorldCreatures. They can move in this world. When they meet each other, a clash occurs.
What I would like to do is make a way to detect collisions between them. Here's what I'm standing on right now:
- For me, Ball Ball is simple, I just calculate their distance from their positions and compare it with the sum of their "sizes".
- The collision between the ball and the edge of the world is simple - I just check the distance from it, which is simple in Cartesian coordinates.
- More common problems are how to detect a collision between the Line (beginning and end at some points) or other objects that I might be there. The distance between the line and the point can be easily calculated, but I would like to have
Some general way to tell if an object collides Awith an object B. The code, as of now, looks something like this:
class WorldCreature:
def detectCollision(self, otherObject):
if collision:
self.onCollision(otherObject)
otherObject.onCollision(self)
class Ball(WorldCreature):
class Line(WorldCreature):
Now the collision detection mechanism should depend on which objects can collide. So there will be an effect.
Should I just keep a list of all the objects in memory and iterate over all of them at every step? Or is there a better way to improve this task?