Category Archives: Evolve Gold

Programming Motion

When making the animals in the environment the problem was encountered of how to give them motion which is; natural looking, random (to start with) but with purpose, e.g. moving in order to find food.


Deleting Objects in C++

 

One of the key parts to simulating evolution is organisms need to die, this is because in nature animals that have disadvantageous mutations will not survive and only the fittest organisms that are best adapted to their environment will.

 

There are two aspects to this: Firstly the visual aspect this is what the user will see in a game an animation of the player dying is usually triggered but on the simplest level this is to just make the dead plant no longer visible. Now the project thus far was just doing this, ceasing to draw the plants when they where dead, but there is still one problem with this the object its self needs to be deleted or there will be a memory leak, that and that death is a key part of evolution.

 

First it was attempted to kill the plants in the environment  as once the logic for this was implemented it could be easily used for the animals.

 

So just to recap the plants are kept in a linked list, this is a data structure made of objects linked by pointers each one points to the node in the list and the plants currently have a boolean variable which becomes true if they are dead. As each node is an instance of an object which is defined using struct it is possible to implement member functions which will be accessible by every node. It was first attempted to delete the plant within one of these functions if it was dead and then make another boolean true. Then in the function that went through each node and called the functions of the plants they held it could be tested if any node held a dead plant. This approach was incorrect as everything needs to be deleted in one go or the program would through the error BAD ACCESS (INSERT REASON WHY).

 

So to delete everything in one go when the node holds a dead plant delete needs to be called on that node this calls it’s deconstructor. A deconstructer is used to delete everything that is no longer needed once an object goes out of scope. So in the deconstructor for the node the delete must be called on the plant it holds and anything else that is not needed. Now it was thought that any pointers made needed to be deleted in order to be deleted in the decontructors so as they where not just left once the object was out of scope. So this is the deconstructor for the node:

 

plantNode :: ~plantNode

 

{

 

delete Bill;

 

delete firstG1;

 

delete newG1;

 

}

 

and for the plant:

 

Plant :: ~Plant

 

{

 

delete tempEvo;

 

delete newGene;

 

delete theGene;

 

}

 



Basic Reproduction Working

Fig 1

Fig 2

This is a very basic level of reproduction when one plant can make a copy of itself, similar to mitosis. At this stage the Plant class (displayed as a sphere) outputs a boolean which becomes true when the plant is able to breed. When this becomes true a function is called which creates a new node on the linked list (described in Log 09/03/2012) imagine this as a box which holds a plant, each node carries a new plant. There is a boolean for if each plant is a child if it is moves in a random direction away from it’s parent.

On improvement needed here is to make each plant only able to have one child in the time it is able to bread because the program runs so fast that all the spheres in Fig1 are the children of one parent.