directing a mass of enemies at once - python

I am working on a simple 2d game where many enemies continually spawn and chase the player or players in python + pygame. A problem I ran into, and one many people that have programmed this type of game have run into is that the enemies converge very quickly. I have made a temporary solution to this problem with a function that pushes any two enemies randomly apart if they are too close to each other. This works well but is about an O(n^2) algorithm which is run every frame and at high enemies the program starts to slow down.
When my program runs with this function the enemies seem to form round object I nicknamed a "clump". The clump seems to usually ecliptic but may actually be more complex (not symmetrical) because as the player moves the enemies are being pulled in different directions. I do like the way this clump behaves, however I am wondering if there is a more efficient way to calculate it. Currently every enemy in the clump (often >100) is first moved in the direction of the player, and then pushed apart. If there was instead a way to calculate the figure that the clump creates, and how it moves it would save a lot of computation.
I am not exactly sure how to approach the problem. It may be possible to calculate where the border of the figure moves, and then expand it to make sure the area stays the same.
Also my two functions currently being used to move enemies:
def moveEnemy(enemy, player, speed):
a = player.left-enemy.left
b = player.top-enemy.top
r = speed/math.hypot(a,b)
return enemy.move(r*a, r*b)
def clump(enemys):
for p in range(len(enemys)):
for q in range(len(enemys)-p-1):
a = enemys[p]
b = enemys[p+q+1]
if abs(a.left-b.left)+abs(a.top-b.top)<CLUMP:
xChange = (random.random()-.5)*CLUMP
yChange = ((CLUMP/2)**2-xChange**2)**.5
enemys[p] = enemys[p].move(int(xChange+.5), int(yChange + .5))
enemys[p+q+1] = enemys[p+q+1].move(-int(xChange+.5),-int(yChange+.5))
return enemys
Edit: some screen shots of how the clump looks:
http://imageshack.us/photo/my-images/651/elip.png/
http://imageshack.us/photo/my-images/832/newfni.png/
http://imageshack.us/photo/my-images/836/gamewk.png/
The clump seems to be mostly a round object just stretched (like an eclipse but may be stretched in multiple directions), however it currently has straight edges due to the rectangular enemies.

There are several ways to go about this, depending on your game. Here are some ideas for improving the performance:
Allow for some overlap.
Reduce your distance checking to be done after a fixed number of frames.
Improve your distance checking formula. If you are using the standard distance formula, this can be optimized in many ways. For one, get rid of the square root. Precision doesn't matter, only the relative distance.
Each unit can keep track of a list of nearby units. Only perform your calculations between the units in that list. Every so often, update that list by checking against all units.
Depending on how your game is setup, you can split the field up into areas, such as quadrants or cells. Units only get tested against other units in that cell.
EDIT: When the units get close to their target, it might not behave correctly. I would suggest rather than having them home-in on the exact target from far away, that they actually seek a randomized nearby target. Like an offset from their real target.
I'm sure there are many other ways to improve this, it is pretty open ended after all. I should also point out Boids and flocking which could be of interest.

You could define a clump as a separate object with a fixed number of spacial "slots" for each enemy unit in the clump. Each slot would have a set of coordinates relative to the clump center and would either be empty or would hold a reference to one unit.
A new unit trying to join the clump would move towards the innermost free slot, and once it got there it would "stay in formation", its position always being the position of the slot it occupied. Clumps would have a radius much larger than a single unit, would adjust position to avoid overlapping other clumps or loose units that weren't trying to join the clump, etc.
At some point, though, you'd need to deal with interactions for the individual units in the clump, though, so I'm not sure it's worthwhile. I think Austin Henley's suggestion of splitting the field up into cells/regions and only testing against units in nearby cells is the most practical approach.

I think you're looking for flocking.

The best intro to flocking / steering behavior movement: http://www.red3d.com/cwr/steer/ . See the attached paper red3d paper. And the related OpenSteer

Related

How do I determine correct wall pieces in a random 2D dungeon I made?

I've made a random 2D tile-based dungeon generator in Python (and PyGame), which is a basic 2D array of wall and floor tiles, and it works great, filled with rooms and corridors. But now I want to go around the rooms and corridors with nicer wall sprites (top-left corner, top-right corner, etc).
EDIT: To expand a bit without going into the whole process, an empty 2D array is created (as the level map) consisting of 0 values (empty). Rooms of random size are "placed" at random locations within the 2D array, and linked by corridors. Rooms and corridors are given a value of 1 (floor). It's easy to just outline the rooms and corridors with a single square wall sprite, but I want to have dynamic walls that have shape to them (corners, ends, that sort of thing).
I have tried a few different ways - generating the dungeon then checking edges and replacing wall parts, or creating the wall parts around each room as it's generated, but there's always problems where the corridors cross into rooms, and the order in which the wall parts are determined.
I've searched Google, Roguebasin, etc but I can't find anything about this. Every random generator I find just uses 2 simple sprites (floor and wall). I don't want my "walls" to look generic. I also wanted to steer clear of prefab rooms because I want it to be completely random.
I've been stuck on this for 2 weeks. I'm making my own engine in Python, and there are lots of examples of random dungeon generation, but I want to go a step further and make the dungeon look great with nice corners, horizontal and vertical walls, etc.
Do I incorporate the wall parts into the generation? Or do I go over the generated map afterwards with an algorithm for choosing the correct parts, or is there something else I've not thought of? Or am I punching too high, should I stick with a basic roguelike setup?
Here's a mock-up of how I want it to look (or similar). As you can see, I'm not a stranger to pixel art and sprites, but I want the computer to be able to randomly generate the rooms and corridors, AND populate the correct wall parts:
I found a video on Unity's Tile Rule feature, and setting up rules for tiles so that the tile reacts to the tiles around it.
Here is a link to the video I watched that gave me the idea:
Tilemap: Rule Tile
I have successfully now coded tile rules into my game, and it works really well - the map is randomly generated with rooms connected by corridors, and then works around the floor tiles, assigning the correct wall part based on my rules.
Hopefully this will serve as a signpost for any devs doing the same kind of work.
Here is a screenshot of a randomly-generated level with the walls now auto-generated:
And here are the rules I designed using Excel:
Ticks on a black background mean a wall is required in that space, a blue background means a floor is required in that space. I'm not sure if there is a quicker or better way of doing this, but it's worked for me.
I don't exactly understand you but I can say you need to define which "Tile" going to pair with which tile, like you don't want to pair ground tile with wall tile.
if you make that logic work with code you can make corner pieces pair with normal wall
or door pieces pair with walls.
and there is a lot of videos you can make a map an AI trained by a grayscale image that has understood your wall, ground, door placement and generates maps like your style.

Separating an unknown amount of coordinates in a list into seperate coordinates

I am trying to create a program that will let a simulated robot move around a space and go around obstacles in the most efficient way possible. I have a list of obstacles hard coded into the code and I have to separate those points into separate variables, but there can be an unlimited amount of obstacles in the list. Each obstacle will have a row and column number, which is how I am determining how the robot will move.
I have tried to create a list and then taken the length of that list and used that to separate the different coordinates in the list, but since there is supposed to be a random amount of obstacles, the code would have to be changed every time another obstacle is added.
obstacles = [(2,3),(3,5),(5,2)]
#list of the obstacle coordinates
length = range(0,len(obstacles))
obs1,obs2,obs3,obs4=[obstacles[i] for i in length]
#Seperation of the coordinates into groups
obsRow,obsCol=obs1
#Seperation into the row and column of the coordinates
This function is giving an output, but the output only works when the number of obstacles in the list is equal to the amount of obstacles with the number after them (ex: obs1). I want this function to work no matter how many obstacles are in the list without changing the function.
If you are dealing with a potentially infinite number of spaces and obstacles you may want to look into A* pathfinding. It's a way to find the most efficient route without exhaustively trying every possible combination (see. The Travelling Salesman problem).
The following article provides an example of how you would solve a similar problem in Python alongside the relevant theory to understand the concepts behind the solution.

Arms motion with coordinate argument with NAO?

I am beginner with NAO programming and I am now working on a project involving arms motion.
I must program a game in which NAO would first stand and point out one among three squares with different colors which would be displayed on the ground.
I think that I can "simply" make Nao move its arm so he would point towards one of three different pre-defined coordinates.
However, animation mode and motion widget do not seem usable for movements with parameters, like one out of the three coordinates.
How do I perform such a move ?
Have you look at the ALMotion.setPositions type of method ?
There are methods working in cartesian space. It means that you just positionnate some end effector (eg the hand) to be at a specific positions compared to the origin of the chest (for instance).
You can see that as a vector pointing to a direction...
The solver used for that could be enhanced, but it's a nice way to achieve what you need to do.
More info there:
http://doc.aldebaran.com/2-1/naoqi/motion/control-cartesian-api.html#ALMotionProxy::setPositions__AL::ALValueCR.AL::ALValueCR.AL::ALValueCR.floatCR.AL::ALValueCR
You could take a look at the pointAt method which takes in parameters the position that you would like to point. If the position of your three objects are known in advance, that would do the job. You can find more here:
http://doc.aldebaran.com/2-1/naoqi/trackers/altracker-api.html#ALTrackerProxy::pointAt__ssCR.std::vector:float:CR.iCR.floatCR

Separating Axis Theorem in Python/Pyglet

I'm trying to make a (sort-of) clone of Asteroids in Python using Pyglet. I figured I'd try to get a little fancy and implement the separating axis theorem to do collision. I got it to work, but the problem is that it's miserably slow. I test collision between bullets that the player shoots and the asteroids on the screen in a double for-loop, which I believe is quadratic time, but the frame rate drops from about 60 to 30 fps by the time there's about 6 asteroids and 6 bullets on the screen, which seems incredibly slow, even for a non-optimized way of detecting collision.
So I ran a profiler to determine where, exactly, in the code the program is getting hung up. It seems to be hung up in the method where I transform shape vertices into world space (I define the shapes around the origin and use OpenGL code to transform to world space for drawing, which I believe is the right way to do it). I grab the transformation matrix from OpenGL, turn it into a NumPy array, and then multiply each vertex by this matrix to get the transformed vertices. It's worth noting that I do this every collision check: I used to use XNA, and when I implemented the SAT in that (I made an asteroids clone there, too), the vertices were also defined around the origin and then you had to transform them using a world matrix.
Is it best to store the vertices around (0, 0) and transform each call, or just store the transformed vertices? I feel like the algorithm shouldn't be THIS slow, so I'm willing to bet I screwed up implementing something. If I was better at profiling (I'm pretty unfamiliar with it) I might be able to get a more complete picture, but I was hoping you guys might have some idea.
Here's a direct link to the file with the Shape class in it, where all the collision logic happens: shape.py. The specific method that the profiler seemed to mark as the bottleneck was __get_transformed_verts. Obviously you can get to the entire repo from there too, but just be aware that there's still a good deal not commented.
As Nico suggests in comments, a quick way to get a good speed-up would be to check simpler geometry first. For an Asteroids clone I guess a circle will be a good fit (or sphere for 3D). If the circles (at least large enough to cover your actual shape) don't overlap, then there is no need to do the more expensive geometry test.
If you have many objects, you will probably want to avoid doing n*n tests every frame. Take a look at space partitioning structures/algorithms. The simplest scheme with a lot of moving objects in 2D would be a grid. Then you only need to test objects belonging to the same - or neighbouring - grid cells for collision.
Another thing I noticed: You generate the transformed vertices every time you test for collision. It would be quicker to generate them only once per timestep (frame) for each object that fails the circle-circle test.

Collide detection and masks in python 3.2.2

Me and my friend are currently trying to make a game. In this game we wish to have nice collision detection. But we cannot seem to find the way to update the masks in the definiton while loop. :S We have tested it alot of times, with different values and even some really unlogical stuff.
Each time we move the player, the game crashes when it collides with the mask. Its like the loop goes on forever, even though the player and the bush should be going away from eachother when we move the mask and the background.
def check_collision():
while pygame.sprite.collide_mask(player, rock):
bg.x -= 1 #the x-value of the background
rock.x -=1 #the x-value of the object
This is just collision when going left using the button "a".
We also move the background and all the sprites instead of the player.
We need to use masks.
We know that it checks the collision, but the mask wont update after bg.x and rock.x are changed.
We assume that this is because the images aren't moved on the screen. And therefore we have tried putting a blit inside the while loop. It still would not work.
Thank you for helping, and a final question, is there any way to manually move a mask/object?
First of all, collision detection using masks is very time-intensive. Whether or not your game has entered an infinite loop, the processing requirements of a bitmask-bitmask overlap check will make your game run far too slowly.
A simple optimization exists, however:
Any object which is able to collide with things must have some maximum size -- that is, you can find a rectangle which will always contain your player, and your boulder can fit inside another. Therefore, if your player's box doesn't collide with the boulder's box, they can't possibly overlap. Since you insist on collision masking, (which can add some realism to any pixelart game), you can compute the per-pixel collision whenever (and only whenever) the bounding boxes collide.
Now, on to your coding style: >:O
It is never a good idea to put a potentially infinite loop within a function which should ideally compute an instant collision check. In the best-case scenario (which is certainly achievable), you would have one function to check whether two objects collide, and tell you some more useful information (the position of one relative to the other, etcetera); while a separate method of every moving object would fix the collisions.
This would translate to something like:
class CollidingObject:
#This class controls an object which can move and fix collisions
def __init__(self):
self.x = 0 #add relevant initialization code here
self.y = 0
self.xVel = 0 # X and Y velocity (for movement)
self.yVel = 0
self.xSize = 0 # the width of the object in pixels
self.ySize = 0 # the height of the object in pixels
def iscolliding(self, other):
# using x and y as the center of the object,
# this returns an empty tuple if they don't collide
if ((self.xSize + other.xSize) / 2 <= abs(self.x - other.x)) and
((self.ySize + other.ySize) / 2 <= abs(self.y - other.y)): return ()
"""
use pygame collidemask here to compute whether they collide.
if they do, return a vector of 'other's' position relative to self.
(this can be used to decide how to separate the objects)
"""
def restitute(self, overlaps_with, distances):
"""
Given some objects which overlap, and a list of 2D vectors of their
relative distances, this separates them however much you like.
"""
pass
As to where your colision checking is done, that depends upon your implementation of object management basics. I will heretofore assume that all of your in-game objects are contained within an iterable; and that on every frame you iterate through your objects, once to render, once to move them -- a structure something like this:
while NOT_QUIT:
for object in objects:
object.draw_to_screen()
object.move() # moves the object -- collisions, etc in here
event_handling_stuff() # handles events
In this case, every object can compute collision checking for anything which follows it in objects. In doing so, each object can collect how far it has to move from each. Afterwards, each object can move to be as far from each collider as possible.
In a few games I've written, I'd make objects move farther apart the more overlapped they are, giving collisions an elastic quality which makes even very rough restitution algorithms look very sexy. Generally, you can tinker with constants once you have a working check going and that would be the least of your worries.
Hopefully this will have helped you two a little (I realize now I went off a bit on a tangent, but you were asking about how to do the wrong things more efficiently :P).
tl;dr: Don't try to fix collisions within your collision check function. Instead, separate it into one which finds all collisions to other objects, and another which fixes all collisions for an object at the same time.
Add other questions and I'll update (:.
UPDATE 1
Expanding here on the "vector of other to self" bit (which was explained a tad crudely:/)
Generally when two objects collide in real life, they bounce back somewhat in the direction they came from (when you drop a rubber bouncy ball on the floor, it bounces back from whence it came -- it doesn't just phaze through the floor). In most programming applications, you'd want to make bouncy balls (and other colliding things) behave in the same way (well, sometimes you might want the ball to phaze though the floor, but that's even easier than bouncing IMHO :P).
To know which way an object must bounce back, you have to know the direction from which it came. More strictly, you have to know the angle at which it collided. This is very easily found if you compare the distance and direction between the centers of each object during the collision. This will provide a pretty accurate representation of two objects bouncing, if the centers you are using are close enough to their centers of mass (in most games the middle of an object is an easy and good approximation).
So, since we don't need to worry about center of mass and all that, we just measure the vector distance between object positions:
#Continuing the previous example, we put some code like this in 'iscolliding' :)
if they collide (pygame mask stuff) :
x_distance = self.x - other.x
y_distance = self.y - other.y
return (x_distance, y_distance)
This code can give you the line along which each object should move to resolve the collision as fast as possible. The rest is a matter of accelerating each object along this line, making sure they don't go closer together (pay attention to signs), and tweaking constants to create a realistic effect.

Categories