Greenspace, a park or landscape design toy

Since a while I’ve been thinking about developing a sort of game or toy around the idea of building a garden, park or more general, design a landscape.

[url=http://www.java-gaming.org/user-generated-content/members/132627/greenspace.jpg]

click for large version
[/url]

While I have some programming and also graphics design skills, my game design skills are bad. Therefore it is unlikely that this really will become a game, but a sort of map making toy I surely can make.

[url=http://www.java-gaming.org/user-generated-content/members/132627/four-seasons.jpg]

four seasons, click for large version
[/url]

Technically a landscape is made of several elements in this project:

  • a backdrop image, which defines the “canvas” for the design
  • a map structure which allows to position items onto the backdrop

At the moment, the backdrops are fixed at 800x600, but it shouldn’t be too hard to levitate this limit.

The current incarnation of the project can be found here:

http://sourceforge.net/projects/freemapper/files/Greenspace/

I hope I can include some more “gamey” features later in the project, but at the moment I am working just on the sandbox mode, and to include more graphics. I want to include three backdrops, “grassy plains”, “desert landscape” and “another planet in space” with mathching plants and decorations if I can. So far I have a bit of the “grassy plains” scenario, the rest is just in my head.

I call it “alpha” because even if it works somewhat stable, many elements are supposed to change, particularly the way maps and the graphics are stored. I can’t even promise good compatibility of map data between versions currently, but it should stabilize at some point.

It’s pure Java/Swing, no external libraries needed, so there is some hope that it will run on many systems. But experience shows that it usually doesnt. I’ll try though to have it run on as many systems as possible.

Did you make, or where did you get those sprites? They look really nice. Perhaps this would make an awesome iso map maker if you could save it in some easy to read form?

The maps are saved as a sort of XML, so it should be fairly easy to read them again. I wanted to get rid of the restrictions of a tiled/gridded map structure in this project though, and see if performance will be good enough with a “object list + depth sorting” display approach.

But it’s an offspring of a map editor, indeed.

Some of the bitmaps I made myself, some I have borrowed from Simutrans:

http://simutrans.svn.sourceforge.net/viewvc/simutrans/

Having some basic raytracing features together now, my code can produce a bit better quality of graphics. Maybe good enough to actually be used. At the moment I have only spheres and cylinders as basic objects though, but with better shapes it should be possible to make rather interesting plants.

This was a first test:

The code can render this in a few steps of growing (it’s a recursive structure), and it should be able to add snow, too, for the winter version.

The plant recipe looks like this right now:

    private void tree2(Turtle3D t, double dist, int n)
    {
        if(dist > 20.0)
        {
            t.setLineWidth(dist/35);
            t.setColor(trunk);
            t.forward(dist/3);
            t.up(6);
            t.setColor(leaf);
            t.fillOval(4, 4);
            t.up(2);
            t.fillOval(3, 3);
            t.stopPaint();
            t.up(-16);
            t.startPaint();
            t.fillOval(2, 2);
            t.up(2);
            t.fillOval(4, 4);
            t.setColor(trunk);
            t.up(6);

            t.setColor(trunk);
            t.forward(dist/3);
            t.up(6);
            t.setColor(leaf);
            t.fillOval(4, 4);
            t.up(2);
            t.fillOval(2, 2);
            t.stopPaint();
            t.up(-16);
            t.startPaint();
            t.fillOval(2, 2);
            t.up(2);
            t.fillOval(4, 4);
            t.setColor(trunk);
            t.up(6);

            t.forward(dist/3);

            Turtle3D t1 = new Turtle3D(t);
            t1.roll(0);
            t1.turn(30);
            tree2(t1, dist*0.8, ++n);

            Turtle3D t2 = new Turtle3D(t);
            t2.roll(120);
            t2.turn(31);
            tree2(t2, dist*0.8, ++n);

            Turtle3D t3 = new Turtle3D(t);
            t3.roll(240);
            t3.turn(32);
            tree2(t3, dist*0.8, ++n);
        }
        else
        {
            t.setLineWidth(1);
            t.setColor(trunk);
            t.forward(dist/2);
            t.fillOval(2, 2);
            t.forward(-1);
            t.setColor(flower);
            t.fillOval(5, 5);
        }
    }

I need to make this accessible to non-programmers too, since I assume the typical audience for a gradening simulation will include many people who are not used to such abstract methods of designing plants.

Well, at least, progress :slight_smile:

It looks good from this angle. I am not sure whether I would prefer 3D or 2D.

At the moment it’s all open, 2D or 3D. And if the raytracing approach really works, different players can render their own views from the “recipes” to their liking.

I need a better plant example, but I wanted to show some states of growth. Recursions 1 to 3, and a forth level with fruits.

The raytracer still has gotten bugs, and bugs are the bane of each garden :persecutioncomplex:

A few bugs less in the tracer, the bush refined and a second plant “recipe”:

The leaves were quite tricky, there just isn’t a simple geometric shape that resembles a leave. I decided to use texture patches, and it worked quite well:

I think I can call this part good enough now, and focus on a better input method for the plant recipes next. I assume I need some sort of interpreter for a syntax like this:

    private void tree2(Turtle3D t, double dist, int n)
    {
        if(n < 0)
        {
            return;
        }
        
        if(dist > 20.0)
        {
            t.setColor(trunk);

            t.roll(25);
            t.setLineWidth(dist/35);
            t.forward(dist/2);
            
            t.up(6);
            leaf(t, 7, 15);
            t.roll(180);
            t.up(12);
            leaf(t, 8, 18);
            t.up(-6);

            t.setColor(trunk);            
            t.forward(dist/4);
            t.roll(90);

            t.up(6);
            leaf(t, 7, 17);
            t.roll(180);
            t.up(12);
            leaf(t, 8, 19);
            t.up(-6);
            
            t.forward(dist/4);

            n--;
            
            Turtle3D t1 = new Turtle3D(t);
            t1.roll(0);
            t1.turn(33);
            tree2(t1, dist*0.8, n);

            Turtle3D t2 = new Turtle3D(t);
            t2.roll(119);
            t2.turn(35);
            tree2(t2, dist*0.8, n);

            Turtle3D t3 = new Turtle3D(t);
            t3.roll(242);
            t3.turn(37);
            tree2(t3, dist*0.8, n);
        }
        else
        {
            t.turn(0 + Math.random()*2);
            t.setLineWidth(1);
            t.setColor(trunk);
            t.forward(dist*0.4);
            t.setColor(leaf);
            t.sphere(2.2);
            t.forward(-1);
            t.setColor(flower);
            t.sphere(5.5);
        }
    }

This produces the plant shown above. Turtle graphics were once well known when Logo as a programming language for children and beginners was invented, but how is that nowadays? Could you think of something simpler than the listing above?

Is an ellipse simple? What is a texture patch?

An ellipse is good, but it didn’t have the pointy tip that many sorts of leaves have.

A texture patch is a rectangle, which is transparent in all places which are not covered by non-transparent texture pixels. It’s inifinitely thin, but good for leaves as it seems. I assume I can use if for petals and such too. It seems to be as a quite versatile element.

L-system.

You mean, use a grammer-like syntax? The plants are loosely based on the l-system idea already, the script just defines a stem build, and how to add more stems. But it can do more, since it’s a stack machine.

I’m looking for something that a non-programmer who is sufficiently interested in the topic can learn in 10 to 30 minutes to the point when they have some sort of success experience. I can’t imagine many people will be willing to spend more time than 30 minutes before giving up in frustration.

The scripting labguage is complete enough now to run one of the example plants shown above. I’ve started to work on a small plant designer application:

The scripting language needs a loop construct and more math function calls, but it’s working surprisingly well already.

you know xtext?
http://www.eclipse.org/Xtext/7languagesDoc.html#tortoise

Ack. No, I had no idea that such a thing already exists …
I must admit I’ve largely been ignoring Eclipse and everything around it.

The scripting language supports a few math functions now and it’s possible to define subroutines. Both of the example plants can now be created from scripts.

I’ve uploaded a demo version, to gather some early feedback (190k):

http://www.java-gaming.org/user-generated-content/members/132627/gdsi-plant-designer-r2.jar

You need to unzip this jar file - it will create a folder named “gdsi-plant-designer-r2”. In this folder you will find two text files, which are the scripts for the plants and another “gdsi-plant-designer-r2.jar” file, which is an executable jar file. On windows you can run it with a double click from the explorer.

The lexer is somewhat limited at the moment and requires whitespace between all tokens. I.e. “5+2*PI” will not work, but “5 + 2 * PI” will. PI is a predefined variable which you can use.

You need to assign a value to a variable before you can use the variable, otherwise you get an error when acessing the variables value.

Most likely there are a zillion of more bugs. Maybe it’s even too early to have this tested, but if you are interested and have some time, please try it and let me know what you think about it.

A brief function reference:

Turtle control functions

double getScale()
void setScale(double scale)
void startPainting()
void stopPainting()
void forward(double distance)
void up(double distance)
void move(V3 direction, double distance)
void turn(double angle)
void roll(double angle)
void setColor(int argb)
void setColor(double red, double green, double blue)
void setLineWidth(double width)
void placeSphere(double size)
void placeLeaf(double length, double width)

Turtle state functions

The turtle has a memory. You can make it remember the current position and state by calling “push()”. To make the turtle return to the last pushed spot, call “rewind”. “pop()” rewinds the turtle and also forgets about the pushed state.

void push()
void rewind()
void pop()

Math functions

double random()
double sin(double rad)
double cos(double rad)
double sqrt(double r)

Preset variables

PI

Flow Control

if
{

}

while
{

}

Operators

      • /

This book about plants/flowers and L-systems is really great:
http://algorithmicbotany.org/papers/#abop

I read that before I implemented my own L-system(s)

Wow. I didn’t expect such a quality work to be freely available. Thanks for the link, krasse!

Some progress:

  • Expressions without spaces like 2+2*PI parse correctly now
  • Subexpressions like 2 * (PI - 2) get correct precedence now

Made a new script for a group of grasses:

I don’t think I’ve bought a CS book in the last 15 years.

It seems like you’re converging to Java-like syntax.

Maybe you can use Janino as a runtime compiler.

If you really want your own syntax, it’s probably easier to use a few regex patterns (or plain string operations) to convert your own language to java, compile it with janino, and have immediate results with nearly zero effort.