Additional interface functionality and some art

The player is now able to switch between creatures using the keyboard arrows or the by clicking on the two arrows on the top right of the screen.

After choosing some creatures to parent the next generation, the player can now remove them from the “selected” panel by dragging their icon to the box labelled “BIN”.Version0.8

Also, now the creature has images attached to each part of its body, so it can look more like it is supposed to.

creature3

Creature layout

The creature’s model has been redesigned to it a bit more interesting (hopefully) and also to make the physics behind its movement a bit more realistic.

The way it was before, with the creature jumping in the way that it did, the effect was as if the creature had been hit from below (a single upward impulse was applied to its body).

c1c2

some of the earlier designs

We can see that the creature is basically made out of circles, and on the first example there is also a trapezoid at the bottom. This trapezoid is much more dense than the other parts of the creature’s body and its purpose is to make the creature stand up like a tumbler toy.

Inspired by an idea given in the section Other Methods of the chapter Jumping on the iforce2d web tutorial (link), I decided to give the creature a hip connected to its main part and two feet connected to the hip.

nc1

new design

This way I was able to make it jump by stretching its legs really fast so that the ground’s resistance would cause the creature to be propelled into the air.

The reason it has two feet instead of one single platform is that in this way different stretching speeds can be applied to each leg, so that the creature can jump to the sides, and not only straight upwards.

The problem with having the creature jumping sideways is that it will easily fall over and not be able to jump properly any more. Here is where the function of the hip comes in. The hip is pinned to the main part (the big circle) and can rotate a limited amount of degrees in relation to it. By locking the rotation of the main part I was able to prevent the creature from falling while still preserving its ability to jump to the sides.

nc2

maximum rotation of the hip
(the creature doesn’t fall)  

Selecting multiple creatures and allowing mutation

The current version of the game allows the player to select more than one creature to be the parent of the next generation. The player also has the possibility of picking two creatures to mate, so that their offspring’s jumps will be determined by the Dna resulting from the crossover between both parents’ Dna.

These additions were accomplished through the implementation of and interface allowing the user to drag boxes containing the selected creature’s number to a panel on the right. If the user drags a box on top of another box already selected they will combine signalling that both selected creatures will mate when the next generation is created.

Screen Shot 2013-02-07 at 23.09.20

The player should still switch between creatures by using the number keys and create the new generation by pressing Enter.

Another addition to the current version is that the game now responds with a message congratulating the player in case some creature touches the rotating blue square.

This version still needs to have some bugs fixed, but it will be posted here soon.

Implementaion of the genetic algorithm – mutation

Here I will give some details on how the current implementation of the genetic algorithm works.
The genetic code for each creature is structured as a Dna object, this object contains an array called chromosomes where each position holds a 2D vector, and it also contains an integer value representing how long the chromosomes array is. As mentioned on a previous post, this vectors are used to determine each of the creature’s jumps.
When a new creature is created their Dna is also created and initialized. For the initialization the amount of vectors is the only information required. After that the vectors on the array will be filled with random values within a predetermined range. This range is determined by a global variable that will be referred to as maximum initial impulse.
Given that the maximum initial impulse is 3, one possible Dna could look like this:

Amount of Dna code:

3

Chromosomes:

Pos. 0 Pos. 1 Pos. 2
x = 2.3 x = 1.2 x = -2.8
y = -1.2 y = -2.6 y = -0.5

As these values are intended to compose the impulse that makes the creature jump when applied to its body, the Y value of every vector has to be always negative (a positive value would make the creature launch itself onto the ground and the resulting “jump” would actually be due to the creature’s bounce on the floor. It would not look very physically realistic and also most of the impulse would be lost).

Mutation

When mutation is applied to a Dna a chance of mutation and an amount of mutation has to be provided. In the case of version 0.3, the chance is 50%, and the amount is also 50%. What this means is that there is a 50% chance of each jump (vector) being mutated and when it is, the resulting value will be the previous plus a random value between -50% and 50% of the maximum initial impulse.

A possible mutated version of the previous Dna example, considering the chance of mutating and the amount of mutation both equals to 50%, could be this:

Pos. 0 Pos. 1 Pos. 2
x = 2.3 (not changed) x = 1.8 (mutated: 1.2 + 0.6) x = -2.8 (not changed)
y = -1.2 (not changed) y = -3.5 (mutated: -2.6 + (-0.9)) y = -0.5 (not changed)

On the example only the second position (jump) was mutated (there was a 50% chance of this happening).
This is what happened to X:
Mutated X = previous X + (maximum initial impulse * random value between -50% and 50%)
Mutated X = 1.2 + (3 * 20%)
Mutated X = 1.8
And to Y:
Mutated Y = previous Y + (maximum initial impulse * random value between -50% and 50%)
Mutated Y = -2.6 + (3 * (-30%))
Mutated Y = -3.5

First playable version

First playable version

Version 0.3 can be sort of played here.

Version0_3

The point is to reach the rotating blue square, but if you manage to do that, nothing will happen, actually…

You can switch between creatures using the number keys and then hit Space to see them jump. If you think those were good jumps, hit Enter and nine new creatures will be created from the one you chose. By repeating this process you should, after some generations, shape the creatures’ jumps so that at least one of them is able to reach the blue square.

A bit about the implementation

The creatures

The shape of a creature in this game is intended to be oval, so to the way used to create a creature’s body, so that is can collide with the static objects in the level, was giving the creature two shapes: one big circle at the bottom and one small one at the top. Later I will be able to put an oval sprite with the art representing the creature’s appearance over this body made of two circles. They will overlap nicely enough so that the sprite will appear to be colliding with the other objects in the level.

After doing this initial setup for a creature’s body I noticed that it was taking a very long time for the creature to stop moving after a jump. As I was allowing a creature to jump only after it stopped moving completely, this became a problem. What I did then was add a trapezoidal shape to the bottom of the creature and make it a hundred times more dense than the other circular shapes. On top of giving the creature’s body a straight base it also gave it a tumbler toy-like behavior, meaning that creature would most of the times and up standing up straight after a jump.

As for the way the creature’s jumps, it was done by applying an impulse to the creature’s body. The impulse is given in the form of a 2D vector with the Y value being negative, so that the impulse is always upwards. This brings us to the next topic which is about what these jump-determining vectors really represent in the game.

The DNA code

For now the only thing that differentiates one creature from the other is how they jump, that is, what are the amplitude and direction of each jump. As we know, these jumps are determined by 2D vectors. Having that in mind it appeared logical to make these vectors the creature’s DNA code. This way, whenever a creature has to be mutated, their vectors’ values are changed somehow, and whenever two creatures mate, some crossover method is applied to their vectors.

Details about what happens to the DNA code when creatures mutate and mate are subject to another topic.

Obs.: Even though mating is mentioned here (for it is implemented already), it cannot be observed on version 0.3 of the game.