We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOpenGL and 3D Libraries › LOGO Turtle 3D - how rotate 3D
Pages: 1 2 
LOGO Turtle 3D - how rotate 3D (Read 5675 times)
LOGO Turtle 3D - how rotate 3D
Jan 5th, 2009, 9:47pm
 

Hello!

Remember the good old Turtle-Graphic as in LOGO?
It is a 2D tool to make a virtual turtle paint rectangles and stuff. It carries a pen and you tell it to go straight ahead or to turn on the spot. Since it carries a pen, it draws a line if it moves.
To draw a rectangle you tell it to repeat 4 times: go forward 100, turn right 90°.
That's it. Very convenient. I did this in processing (see http://www.openprocessing.org/visuals/?visualID=774).  

I now want to build something similar in processing, but in 3D. Imagine a water turtle; it can turn left and right, swim forward and now also can turn its nose up and down and paint lines leading up or down by certain degree. It should paint a cube by combining 4 rectangles with 90° nose-angle.

But my math fails here. I can’t work out the implementation.  

I have a certain position of the turtle (TurtleX,TurtleY,TurtleZ) and two angles, the traditional angle in the plane of the turtle (right/left) and the new 3D-angle, showing the nose-up-down-angle.
If the turtle now goes forward, I want to take into account the given length and the two angles.  
I calculate the new position by transforming the old position to a new position, draw a line between the old position and the new position and set the new position as the new Turtle-position (TurtleX,TurtleY,TurtleZ).
For the transformation I use translate, rotateX and rotateY. But it doesn’t work at all!

Besides: I draw a point just to be able to retrieve its position (by modelX). In fact, I don't need the point at all.

Here is a sniplet of the code; full code was too long, forum software rejected it  Sad   .

 // translate and turn
 translate (OldX,OldY,OldZ);
 rotateX(WinkelVertikal);     // xrot
 rotateZ(WinkelHorizontal);   // zrot

 point (OldX,OldY,OldZ);

 // it was drawn at ( .., .., ..), store that location
 float x = modelX(OldX,OldY,OldZ);
 float y = modelY(OldX,OldY,OldZ);
 float z = modelZ(OldX,OldY,OldZ);

 // clear out all the transformations (read out Matrix)
 popMatrix();

 // get the (x, y, z) coordinate
 TurtleX = x; //  
 TurtleY = y; //  
 TurtleZ = z; //  

</snip>

How can I do it right?  

All help appreciate!

Thanks a lot!

Greetings, Chris

Re: LOGO Turtle 3D - how rotate 3D
Reply #1 - Jan 7th, 2009, 5:45pm
 
Logo3D, cool idea!.. I Love logo..

I do not have a working solution. Here's a proposal: there's a lot of things that you could do with PMatrix3D. I think that it might be possible to implement something similar to LOGO in 3D by using PMatrix3D.

In this example, you see that it is working correctly with translation but it fails for scale or rotate transformation as they are applied form an absolute referencial system (aka [0,0,0]).



Code:
  PMatrix3D turtle = new PMatrix3D();

public void setup(){
size(1000,500,OPENGL);

stroke(0);

turtle.print();
turtle.translate(10,100);
turtle.print();

line(turtle);

turtle.translate(200,100);
line(turtle);

turtle.translate(0,0,100);
line(turtle);

turtle.translate(10,0);
line(turtle);

turtle.scale(.97f,.97f,1f);
line(turtle);

turtle.rotateZ(1f);
turtle.translate(10,0);
line(turtle);

}


PVector lastTurtlePosition = new PVector(); //By default, it start at (0,0,0)

//Automatically update (lastTurtlePosition) when no position is provided
PVector turtlePosition(PMatrix3D matrix){
PVector newTurtlePosition = turtlePosition(lastTurtlePosition,matrix);

lastTurtlePosition = newTurtlePosition;
turtle.reset();

return newTurtlePosition;
}


//This method is used when you have a different position than (lastTurtlePosition)
PVector turtlePosition(PVector oldVect, PMatrix3D matrix){

PVector newTurtlePosition = new PVector();

newTurtlePosition.x = (matrix.m00*oldVect.x+matrix.m01*oldVect.y+matrix.m02*oldVect.z+matrix.m03);
newTurtlePosition.y = (matrix.m10*oldVect.x+matrix.m11*oldVect.y+matrix.m12*oldVect.z+matrix.m13);
newTurtlePosition.z = (matrix.m20*oldVect.x+matrix.m21*oldVect.y+matrix.m22*oldVect.z+matrix.m23);

return newTurtlePosition;
}


void line(PMatrix3D turtle){
PVector originPosition = lastTurtlePosition;
PVector newTurtlePosition = turtlePosition(turtle);

line(originPosition.x,originPosition.y,originPosition.z,newTurtlePosition.x,newTurtlePosition.y,newTurtlePosition.z);

println("Turtle from: "+originPosition.toString()+" --> to: "+newTurtlePosition.toString());
}

void line(PVector a, PVector b){
line(a.x,a.y,a.z,b.x,b.y,b.z);
}


This is cool for translation, but it's not logo yet. It's just an example how to use PMatrix3D for your transformations.

To have a local referential coordinate system, you might track inverse transformations (set the nose of the turtle on the x axis for example using translation and inverse rotations) then apply your new transformation (ex move turtle forward) and then reapply the transformation at the beginning.

Code:
1- move the turtle back to 0,0,0
2- orient (rotate) the head of the turtle back on the x axis
3- orient (rotate) the head of the turtle back on the y axis
4- apply your transformation (rotate or translate)
5- undo step 3 (rerotate from y axis)
6- undo step 4 (rerotate from x axis)
7- move the turtle back to his intial position (translate)


Re: LOGO Turtle 3D - how rotate 3D
Reply #2 - Jan 7th, 2009, 8:35pm
 
Hello,
see: http://www.processing.org/exhibition/network_page_4.html
-->  Alcys 13 May '04
Re: LOGO Turtle 3D - how rotate 3D
Reply #3 - Jan 8th, 2009, 2:32am
 
this is my old sample.
http://www.geocities.jp/classiclll_newweb/KAMEPainter3D/applet/index.html
2D on a curved surface
http://www.geocities.jp/classiclll_newweb/CanvasesforKAME/applet/index.html
and non-Euclidean geometry version
http://www.geocities.jp/classiclll_newweb/KAMEonPlanet/applet/index.html

hope these help you.
Re: LOGO Turtle 3D - how rotate 3D
Reply #4 - Jan 8th, 2009, 4:56pm
 

Hello !

A big thank you to everybody!

Obviously not a new idea...

Thanks & Greetings,

Chris
Re: LOGO Turtle 3D - how rotate 3D
Reply #5 - May 5th, 2009, 7:26am
 
Finally, I had time to implement LOGO syntax to a Library (http://ANAR.ch). I just added tutrle.up(float); and tutrle.down(float); using something near of the original syntax.

http://anar.ch/examples/p5.php?p=Test05aTurtle
http://anar.ch/examples/p5.php?p=Test04zTurtleMulti

example:
Code:
home 
repeat 10 [
  fd 100
  repeat 5 [
     dw 21
     rt 10      fd 10
  ]
  pu
  fd 10
  pd
  fd 10
]

(Translated into JAVA)

myTurtle.LOGO("home repeat 10 [fd 100 repeat 5 [dw 21 rt 10 fd 10] pu fd 10 pd fd 10]");


(Documentation)
http://anar.ch/doc/anar/Turtle.html

(The French version)
http://anar.ch/doc/anar/Tortue.html
Code:
LEVECRAYON
ORIGINE
BAISSECRAYON
AVANCE 100
Gauche 20
AT 20
Droite 40
RE 20
REPETE 3 [
  AT 10
  TD 10
RE 3   TD 10
]

(Traduction en JAVA)

maTortue.LOGO("LEVECRAYON ORIGINE BAISSECRAYON AVANCE 100 Gauche 20 AT 20 Droite 40 RE 20 REPETE 3 [AT 10 TD 10 RE 3 TD 10]")


It's so good to program in French. The first version of LOGO I knew was in french, I was 8 years old.
Re: LOGO Turtle 3D - how rotate 3D
Reply #6 - Aug 7th, 2009, 4:12am
 
Hi,

Anar looks fantastic.

I was trying to do some work with a 3D turtle a while back and ended up putting together this very basic 3D turtle library.

simonblackmore.net/turtle3d/index.htm

Its nowhere near as powerful as Anar looks but it maybe useful to anyone still following this thread.

Cheers

Simon
Re: LOGO Turtle 3D - how rotate 3D
Reply #7 - Aug 17th, 2009, 1:17pm
 

Hello all!

It is so good that you keep coming back to this thread!

I am grateful and thank you both!

I tried gll code with anar but to know avail.

Can you help me with this?

Did I forget to init something?

Thank you so much!

Greetings, Chris



/*
* Example for Anar library by Guillaume LaBelle + Julien Nembrini
* http://anar.ch
*/

import anar.*;
import processing.opengl.*;

void setup(){
 size(1200,800,OPENGL);
 Anar.init(this);
 // Turtle.debug = true;
 // Anar.drawAxis(true);
}

void draw(){

 Turtle myTurtle = new Turtle();
 myTurtle.PD();
 myTurtle.SETPENCOLOR(255,0,0);
 myTurtle.PD();
 myTurtle.draw();

 int MeineWeite = 350;

 for (int i = 0; i < 4; i = i+1) {

   for (int j = 0; j < 4; j = j+1) {

     myTurtle.FORWARD(MeineWeite);
     myTurtle.RIGHT(90);

   }

   myTurtle.FORWARD(MeineWeite);
   myTurtle.DOWN (90);
 }

 noLoop();

}


Re: LOGO Turtle 3D - how rotate 3D
Reply #8 - Aug 18th, 2009, 11:16am
 
Why are you using noLoop();? Even the Apple][e version was very slow, with the turtle moving slowly, really fun to watch... Wink

Try to remove noLoop() from your code and define a background();

This should work:

Code:
import anar.*;
import processing.opengl.*;

void setup(){
 size(1200,800,OPENGL);
 Anar.init(this);
 // Turtle.debug = true;
 Anar.drawAxis(true);
}

void draw(){
 background(255);

 Turtle myTurtle = new Turtle();
 myTurtle.PD();
 myTurtle.SETPENCOLOR(255,0,0);
 myTurtle.PD();
 myTurtle.draw();

 int MeineWeite = 350;

 for (int i = 0; i < 4; i = i+1) {
   for (int j = 0; j < 4; j = j+1) {
     myTurtle.FORWARD(MeineWeite);
     myTurtle.RIGHT(90);
   }
   myTurtle.FORWARD(MeineWeite);
   myTurtle.DOWN (90);
 }
 //noLoop();
}


Viel Spaß!
Re: LOGO Turtle 3D - how rotate 3D
Reply #9 - Aug 18th, 2009, 12:24pm
 
Dear Guillaume,

did you try it?
It doesn't draw anything except the turtle itself...
Please help...

Thank you for comingt back to me so quickly!

Greetings,

Chris from Hanover
Re: LOGO Turtle 3D - how rotate 3D
Reply #10 - Aug 18th, 2009, 3:13pm
 
Yes, you should draw the turtle only at the end. Also, it's better if you reuse the same turtle (not recreate a new turtle on each frames).

Quote:
import anar.*;
import processing.opengl.*;

Turtle myTurtle = new Turtle(); //Should be outside of the loop

void setup(){
  size(1200,800,OPENGL);
  Anar.init(this);
  // Turtle.debug = true;
  Anar.drawAxis(true);
  
  ////////////////////////////////
  //--(INIT the turtle)---
  myTurtle.PD();
  myTurtle.SETPENCOLOR(255,0,0);
  myTurtle.PD();
  //myTurtle.draw(); //you should draw the turtle only at the end.
                     //Within the loop is a better place.

  int MeineWeite = 350;

  //Here you do have some functions
  for (int i = 0; i < 4; i = i+1) {
    for (int j = 0; j < 4; j = j+1) {
      myTurtle.FORWARD(MeineWeite); 
      myTurtle.RIGHT(90);
    }
    myTurtle.FORWARD(MeineWeite); 
    myTurtle.DOWN (90);
  } 
}

void draw(){
  background(255);
  myTurtle.draw();
}

//You get a lovely red cube.



I like your example. I never taught to draw a cube with LOGO.
Hey, this is a good way to produce some sorts of regular polyhedrons...

Quote:
  for (int i = 0; i < 8; i = i+1) {
    for (int j = 0; j < 8; j = j+1) {
      myTurtle.FORWARD(MeineWeite); 
      myTurtle.RIGHT(45);
    }
    myTurtle.FORWARD(MeineWeite); 
    myTurtle.DOWN(45);
  }
  Anar.setCenter(myTurtle.trace); //set the center of the camera to the center of the geometry
Re: LOGO Turtle 3D - how rotate 3D
Reply #11 - Aug 18th, 2009, 3:19pm
 
Next week we are running a small workshop that will be based on LOGO (this implementation).

Processing for Architecture
workshop by MxD LAB [EPFL]
24-28 August, 09h00-17h00
Computer Science Building
Lausanne, Switzerland+

Turtles Lovers are very welcome to join us...
Re: LOGO Turtle 3D - how rotate 3D
Reply #12 - Aug 19th, 2009, 10:41am
 
Hello Guillaume,

I was so glad to see you wrote on the forum today!

I now have a beautiful cube!

Gorgeous!

But when I set the ForwardMargin MeineWeite
to 250 it produces no cube any more as it should.

More a weired long fgure of some sort.

Sorry for coming back to you again and again...

Wink

Thank you!


Gretings, Chris


import anar.*;
import processing.opengl.*;

Turtle myTurtle = new Turtle(); //Should be outside of the loop

void setup(){
 size(1200,800,OPENGL);
 Anar.init(this);
 // Turtle.debug = true;
 Anar.drawAxis(true);

 ////////////////////////////////
 //--(INIT the turtle)---
 myTurtle.PD();
 myTurtle.SETPENCOLOR(255,0,0);

 int MeineWeite = 250;

 //Here you do have some functions
 for (int i = 0; i < 4; i = i+1) {
   for (int j = 0; j < 4; j = j+1) {
     myTurtle.FORWARD(MeineWeite);
     myTurtle.RIGHT(90);
   }
   myTurtle.FORWARD(MeineWeite);
   myTurtle.UP(90);
 }

}

void draw(){
 background(255);
 myTurtle.draw();
}

//You get a lovely red cube.


Re: LOGO Turtle 3D - how rotate 3D
Reply #13 - Aug 22nd, 2009, 10:35am
 
for more info on how to implement 3d turtle drawing (including animation) see

hanan's dissertation "parametric l-systems and their application to the modelling and visualization of plants". pdf available online.

jon mccormack's thesis and generative art work, "Turbulence". google for his web site. email him for his thesis. he has some pdfs of specific chapters of his thesis on his site.

algorithmic beauty of plants by prusinkiewicz; pdf available online via google.

also, check out william latham's 'evolutionary art and computers' for a non L-systems based technique of 3d turtle-based drawing. its available used on amazon. nothing really available online - its like he dropped off the face of the earth...

good luck;
Re: LOGO Turtle 3D - how rotate 3D
Reply #14 - Aug 24th, 2009, 4:23am
 
A little bit of a different twist on this topic, but close enough to be relevant I think.  I have been working on something very similar to a 3D logo turtle, but inside a 3D immersive space.  Using an Opensim platform, and variations of a visual programming environment known as Scratch (by MIT media labs -the originators of Logo back in the day).  A grad student created a version of Scratch for Second Life (a kind of translator) that takes the visual program elements and creates an LSL script that can be placed into an object (the turtle).  Opensim is very similar and the same code structure works very well with only minor tweaks.  It is possible to do pretty much everything you have mentioned... left,right,up down,pitch,roll,align to specific direction, travel point,to point,Pen up, Pen down.   etc. and includes access to all the normal lsl functions.  The only thing that may be different from the kind of application you are trying to build is that to work with the turtle, you have to be in world with it using a special client browser to access an opensim server.  This may not appeal to all, but this mode of interaction also allows the turtle to interact with your avatar as well,respond to your presence, etc.    I do most of my work on a hosted opensim grid reactiongrid.com , but as opensim is opensource, you can download both server and client software and run it as a standalone as well.  May be worth checking into...
Pages: 1 2