BabyGraph

Hi,

my latest project is a little math-game called BabyGraph.

Your task is easy. Build a way for the baby with math functions and collect all the sweets.

There are two versions-

  • First written with pure Java
  • Second written with libgdx to have the same code for desktop and android version

Next steps:

  • music
  • publishing

If someone wants to try the current version:
HTML version: Play
Android Libgdx version: Download
Desktop libgdx version: Download
old “pure Java” version: Download

Screenshots:

Now the solution will be saved online and you can compare to other players. Now you can see in every level what is the shortest solution and what the the lowest time to solve the level.
And now there are 49 levels and I started the editor.

Next steps:

  • finish the editor with upload function
  • better collision detection
  • android version

Wow this is the coolest thing I’ve seen in a while, great job!

Thx. =)

Now the editor is ready and I polished the game. I am very happy with the result.
You can create your own levels (then you have to solve them) and after solving the level you can upload the level and everybody can play it. =)

Next step is trying to make a little better collision detection. But it works good now. :wink:

A question:
I update the function with the time variable only every 100 ms because calculating the values for the polygon needs time (for longer, harder function like ‘((2^((14-x+t)/6))-e^(-(((x-96)/12)^2))32+2^((x-96)/2))(1-1/(1+20))’ ). Then I need ~15ms to calculate all new values. My plan is to update the function every 10 ms.
Do you know a simple libary to solve that (speed)-problem?
I have a string with a function and want the result for x >= -500 and x <= 500 in (0.1 steps). At the moment I tried exp4j, javaluator, my own solver and the javax.script.ScriptEngine . And all solutions needs too much time.

A new version is online. And if there are no bugs, it will be a release candidate.

new:

  • I fixed bugs in the editor.
  • You can start the game by pressing Enter
  • update the function every 10 ms

A workmate was a very good rubber duck. I reported my problem with the time and solved the problem on my own. I dont need to calculate from x = -500 to x = 500. My levels are small (and hard enough). The new borders are x = -105 to x = 105. Thats really enough to have fun and its fast enough to solve it every 10 ms. :wink:

The next step is the android version. It will be my first libGDX project. I’m looking forward.

I had to fix several small bugs and I polished some menus.

Now I am really happy with the result and I can say that is the first version that feels “complete” for me.

Download the game and have fun. :wink:

Now I can really start to port the game to android

I tried the libgdx library und what can I say … i love it. =)

I made a little libgdx project to try out different settings etc. If you want to see my “playground” with 3 different 4k games ported to libgdx, click here.

Next step is porting the BabyGraph game. =)

After making the android version of 5 different 4k games with libgdx, I tried to port BabyGraph.

And what I can say … I am frustrated. :’(

The “normal” port was easy for me (except some problems with drawing a filling polygon and other little problems) but I solved them all …
All Problems? No, a little problem “crashed” the whole port.
And that is calculating the values for the function. On a “normal” pc everything works finde but on a smartphone it is way too slow (only with the ‘t’ variable). :’(
I tried so much libs and the best for me is the JEP Java library (the last free version). With that the smartphone version is only 5 times slower than it should … with my solution or other libs the factor is 30 to 100 times slower. And that is frustrating.

Now I have to find another solution or I cancel the android port. :’(
If you want to try the current slow andoid version, you can get it here and see what I mean.

Tried it, it brought my phone to the knees weeping and it’s actually quite fast…

Did you try using less samples/bigger steps combined with interpolation?
You could also use some kind of level of detail approach where you do more samples of your function around the baby and way less where the baby cannot touch the function and interpolate between the samples to generate a smooth graph.

Also most phones have multiple processor cores and generating function values can be done in parallel really easy.

I agree with what VaTTeRGeR suggested. Definitely reduce the sampling rate at “uninteresting” points.

When you want to go full speed, and get everything out of the JVM/Android VM, read on…

On HotSpot/OpenJDK, my plan for this would be:

  • build a simple JavaCC or ANTLR grammar to recognize math expressions (for inspiration, look at the JavaCC example grammars or this stackoverflow thread)
  • define simple AST classes (Constant, BinaryOp, UnaryOp, FunctionCall, etc.)
  • use grammar actions to instantiate the AST
  • define a common “MathExpression” interface with an abstract “double evaluate(double x)” method
  • Use objectweb’s ASM library to generate a new class that implements MathExpression and evaluate(), by walking the AST and generating bytecode instructions for the math operations
  • Define the generated class in the JVM
    – preferably use sun.misc.Unsafe.defineAnonymousClass() when available (>= 1.7) (same is used to generated classes for Java 8 lambdas implementing the functional interface);
    – or use java.lang.ClassLoader.defineClass()

For Android Dalvik VM, code generation and defining in the VM looks a bit different. Here you can use:

What they do is basically generate a new .dex file at runtime and load it into the VM with a new DexClassLoader.

I however did not try the Dalvik code generation approach. It would be very interesting if that would work for you.

Here is a small project (73kb jar size) I assembled from code I had laying around, that should get you a fast math expression evaluation at least on Desktop (tested on HotSpot and OpenJDK). It uses JavaCC to parse and ASM to generate bytecode and HotSpot Anonymous Classes (also works on OpenJDK) to define new classes in the JVM with minimal overhead.
It compiles and evaluates your expression “((2^((14-x+t)/6))-e^(-(((x-96)/12)^2))32+2^((x-96)/2))(1-1/(1+20))” between -500.0 and +500.0 with 0.1 stepping in ~3.2 ms. on my machine (with making sure that HotSpot does not eliminate the evaluation altogether).
Have fun with it! :slight_smile:

EDIT: Results of this JMH benchmark between sec and exp4j:
`

Run complete. Total time: 00:00:12

Benchmark Mode Cnt Score Error Units
Bench.evalExp4j avgt 3 302,794 ± 9,214 ns/op
Bench.evalSec avgt 3 7,982 ± 0,610 ns/op
`

KaiHH - I am so grateful.

Now it works fine on my smartphone [onePlus 2].
If you want to try it, you can find it here.
If you want to try the desktop libgdx version, you can find it here.

Next steps:

  • sound
  • find a solution to draw a dashed line with libgdx
  • polishing (smartphone version)

WIP version:

Whats new

  • As you can see now I can draw a dashed line with libgdx. But my algorithm isnt perfect at the moment. I will fix it in the next version.
  • And now you can see a 3 seconds preview when the “t” variable is in the function.
  • I added the position information in the android version to “always on when the game is not running”. That should help to find the correct solution.

Next steps:

  • sound
  • more polishing
  • perhaps achievements

Today

  • I added a new start menu
  • now you can compare yourself with the world (check your best time against the world best time to beat that level)
  • I added some “sweet”-particles after solving a level.
  • the game looks after your system language, if its german the game is in german else english :wink:

Yes, I am on the right way. =)

current Alpha-Download
Android Libgdx version:Download
Desktop libgdx version: Download

Today I implemented

  • an options menu, now you can choose the language and everything is prepared to switch music and sound on and off
  • an achievement screen, you can get up to nine achievements
  • removed some bugs in the editor

Next steps:

  • sound and music
  • publish the game to the playstore

current Alpha-Download
Android Libgdx version: Download
Desktop libgdx version: Download

Today

  • I added three new achievements
  • some “flying” functions in the background in the menu.
  • I removed serveral bugs

Next steps:

  • sound/music
  • publish it to the playstore

current Alpha-Download
Android Libgdx version: Download
Desktop libgdx version: Download

Nice!

Small suggestion: I was wondering about moving the “Congratulations” panel down a bit to allow the player to see the source of the sweets-geyser?

Thanks for the nice word phil.
The sweet geyser starts in the middle of the game. It has no “source”. It shows only the joy of winning the level. =)

Today I started with a new game mode: Find the function. In that mode you have to find the correct function and only the function, no baby and no sweets. While playing the game I found so many nice looking functions that I have to add that game mode.

Next steps:

  • more find the function levels
  • sound/music
  • publish it to the playstore

current Alpha-Download
Android Libgdx version: Download
Desktop libgdx version: Download

Just one tiny thing: I have the impression that somehow when entering an expression fast on a Desktop keyboard, the game does not recognize all key presses and oftentimes on an Android device, key presses are recognized multiple times in succession, even though the press was just very quick. Do you use event-driven input or do you query the key states on each render cycle?

Thanks for the feedback. It was event driven input (but with a mapping for the desktop key input). Now it is evaluated every render/update cycle and now the problems are gone. =)

And I implemented a new babysprite with a “crawl” animation and some more “find the function” levels (at the moment 20 levels).

Next steps:

  • more find the function levels (I want 40 or 50 levels)
  • sound/music
  • publish it to the playstore

current Alpha-Download
Android Libgdx version: Download
Desktop libgdx version: Download