Complex scripts

About advanced scenes, and the Thyme scripting language used in Algodoo.

Complex scripts

Postby koskok » Thu Jul 07, 2011 1:11 pm

Hi!
Many times I have seen complex scripts.Most times they are huge and I can't undderstand them.Here's an example:
Code: Select all
(e)=>{
    scene.my.mspd3 = -12 * (e.normal(0) * (math.cos(friction + 0.5 * math.pi)) + e.normal(1) * (math.sin(friction + 0.5 * math.pi))) / (((math.cos(friction + 0.5 * math.pi)) ^ 2 + (math.sin(friction + 0.5 * math.pi)) ^ 2) ^ 0.5);
    {(texturematrix(4)) > 0 && (texturematrix(5) + 30 * restitution) >= 0} ? {friction = math.atan((texturematrix(5) + 30 * restitution) / (texturematrix(4)))} : {};
    {(texturematrix(4)) > 0 && (texturematrix(5) + 30 * restitution) < 0} ? {friction = math.atan((texturematrix(5) + 30 * restitution) / (texturematrix(4))) + 2 * math.pi} : {};
    {(texturematrix(4)) < 0} ? {friction = math.atan((texturematrix(5) + 30 * restitution) / (texturematrix(4))) + math.pi} : {};
    {(texturematrix(4)) == 0 && (texturematrix(5) + 30 * restitution) > 0} ? {friction = 0.5 * math.pi} : {};
    {(texturematrix(4)) == 0 && (texturematrix(5) + 30 * restitution) < 0} ? {friction = 1.5 * math.pi} : {};
    {(texturematrix(4)) == 0 && (texturematrix(5) + 30 * restitution) == 0} ? {friction = 0} : {};
    {controlleracc == 4} ? {
        Scene.addcircle({
            pos := e.pos;
            buttonDestroy = "l";
            vel := ((e.pos - [texturematrix(0), texturematrix(1)]) / (sim.time - texturematrix(2))) + ([(e.normal(0)), (e.normal(1))] * 10);
            radius := 0.125;
            collideset := 32;
            drawcake := false;
            oncollide := (e)=>{density = 0};
            heteroCollide := true;
            drawborder := false;
            color := [0, 0, 0, 1];
            density := density * 13;
            friction := 0.0;
            restitution := 0
        })
    } : {};
    textscale = ((scene.my.pos(0) - e.pos(0)) ^ 2 + (scene.my.pos(1) - e.pos(1)) ^ 2) ^ 0.5;
    texturematrix = [e.pos(0), e.pos(1), sim.time, ((math.pow(((texturematrix(0) - e.pos(0)) * (texturematrix(0) - e.pos(0))) + ((texturematrix(1) - e.pos(1)) * (texturematrix(1) - e.pos(1))), 0.5)) / (sim.time - texturematrix(2))), (35 * (scene.my.pos(0) - e.pos(0)) / ((scene.my.pos(0) - e.pos(0)) ^ 2 + (scene.my.pos(1) - e.pos(1)) ^ 2) ^ 0.5), (35 * (scene.my.pos(1) - e.pos(1)) / ((scene.my.pos(0) - e.pos(0)) ^ 2 + (scene.my.pos(1) - e.pos(1)) ^ 2) ^ 0.5), (((scene.my.pos(0) - e.pos(0)) ^ 2 + (scene.my.pos(1) - e.pos(1)) ^ 2) ^ 0.5), 0, 0];
    airfrictionmult = (texturematrix(3) * 10 + 20000 / textscale);
    {controlleracc > 4} ? {controlleracc = 0} : {controlleracc = controlleracc + 1};
    scene.my.force = e.pos(0) * 10
}

Could you please explain them to me?
koskok
 
Posts: 13
Joined: Thu Jul 07, 2011 12:46 pm

Re: Complex scripts

Postby Kilinich » Thu Jul 07, 2011 1:23 pm

don't waste time - try start from simple one ;-)
Dream of Algodoo as game development engine...
User avatar
Kilinich
[Best bug reporter 2010]
 
Posts: 2098
Joined: Mon Aug 31, 2009 8:27 pm
Location: South Russia

Re: Complex scripts

Postby koskok » Thu Jul 07, 2011 1:34 pm

But I just know simple scripts and a bit more complex
koskok
 
Posts: 13
Joined: Thu Jul 07, 2011 12:46 pm

Re: Complex scripts

Postby hiltropper » Thu Jul 07, 2011 1:48 pm

you get into long comlex scripts while you are experimentating with them and some scripts seem comlex but they are not (i remember a color script i wrote to get into scripting it filled the whole window but when i look at it now it was damn easy )
try to combinate all scripts you now jet and make them work together...and youll have a complex script
yup yup
yuuuuuuup
yupyupyupyupyupyupyupyupyyyuuuup
hm... signatures...
hiltropper
 
Posts: 85
Joined: Mon Dec 20, 2010 12:02 pm
Location: Germany

Re: Complex scripts

Postby koskok » Thu Jul 07, 2011 1:55 pm

hiltropper wrote:you get into long comlex scripts while you are experimentating with them and some scripts seem comlex but they are not (i remember a color script i wrote to get into scripting it filled the whole window but when i look at it now it was damn easy )
try to combinate all scripts you now jet and make them work together...and youll have a complex script

Right,but I also mean some characters and bunches of characters I don't understand like: ^,*,/,\.
koskok
 
Posts: 13
Joined: Thu Jul 07, 2011 12:46 pm

Re: Complex scripts

Postby TC42 » Thu Jul 07, 2011 2:18 pm

Those are regular math characters...
^ means "to the power of" it is the same as math.pow() ex.. 2^3 = 8
* is just a multiplication operator ex.. 2 * 2 * 2 = 8
/ is used for division, similar * ex.. 8/2 = 4; // is used for comments, ex..
scene.my.sandwich = 13; //sets the value of the variable "sandwich" to 13

I think \ has some use with using text in geval, but I never found out, since I used a workaround.

Math.atan() is exactly what it says it is... Arc tangent()
Math.pi is also... 3.1415926535...
e.normal is the angle of contact, I think... I'm not really sure about that though.
Controlleracc and texturematrix are used to store values.
{} ? {} : {} is the structure of an if statement ex.. {condition} ? {true} : {false}
E.pos is the position of an object, stored in an x/y array ex.. [3, -2]
sim.time is the elapsed time since the simulation was started.

I would recommend putting this code into another editor, such as the arduino IDE, because editors like those highlight grouping symbols, so if you have the cursor near one parenthesis, it will highlight the matching, close-parenthesis one.

Where did you come across this code? If you know what it does (and you know some trigonometry) you should be able to read it and understand it...
Image
Spambot wrote:Nowadays, memes usually are studied less in the field of biology, but more with the arena of psychology along with sociology. But, the major area of a meme, getting embraced and imitated all the time is what’s at any core connected with any meme.
User avatar
TC42
 
Posts: 984
Joined: Sun Jan 09, 2011 1:53 pm
Location: $ sudo apt get-install sandwich_

Re: Complex scripts

Postby koskok » Thu Jul 07, 2011 2:30 pm

TC42 wrote:Those are regular math characters...
^ means "to the power of" it is the same as math.pow() ex.. 2^3 = 8
* is just a multiplication operator ex.. 2 * 2 * 2 = 8
/ is used for division, similar * ex.. 8/2 = 4; // is used for comments, ex..
scene.my.sandwich = 13; //sets the value of the variable "sandwich" to 13

I think \ has some use with using text in geval, but I never found out, since I used a workaround.

Math.atan() is exactly what it says it is... Arc tangent()
Math.pi is also... 3.1415926535...
e.normal is the angle of contact, I think... I'm not really sure about that though.
Controlleracc and texturematrix are used to store values.
{} ? {} : {} is the structure of an if statement ex.. {condition} ? {true} : {false}
E.pos is the position of an object, stored in an x/y array ex.. [3, -2]
sim.time is the elapsed time since the simulation was started.

I would recommend putting this code into another editor, such as the arduino IDE, because editors like those highlight grouping symbols, so if you have the cursor near one parenthesis, it will highlight the matching, close-parenthesis one.

Where did you come across this code? If you know what it does (and you know some trigonometry) you should be able to read it and understand it...
I came across it in a missile.That script above is onHit by laser on a box.It is for the propulsion of the missile.
koskok
 
Posts: 13
Joined: Thu Jul 07, 2011 12:46 pm

Re: Complex scripts

Postby daniels220 » Thu Jul 07, 2011 6:02 pm

\ should "escape" a character—e.g. a quote, something like (contrived, no reason to actually use eval here, but w/e):

Code: Select all
geval("scene.my.someText = \"test\"")


If statements should have the form () ? {} : {}, not {} ? {} : {}—the condition doesn't need the braces, although it is a good idea to surround it with parens for ease of reading. Technically it will work as just cond ? {true} : {false}, but if your condition has &&, ||, function calls, etc in it, it's easier to read if you know where it starts and stops.

e.normal isn't the angle of contact, it's a unit vector (an [x,y] array, also, but with a different meaning than e.pos, and with a scalar "length" of 1, i.e. x^2 + y^2 = 1) in the direction of the angle of contact. You have to use atan and some trickery to get the actual angle. It's good for spawning, though, because you can say something like pos := e.pos + e.normal (spawns 1 in front of the spawner), vel := e.normal * 100 (projectile starts at 100 m/s).
daniels220
 
Posts: 95
Joined: Mon Aug 31, 2009 11:30 pm

Re: Complex scripts

Postby electronicboy » Thu Jul 07, 2011 6:27 pm

Also, / is the divide operator :)
When asking for help, READ THE STICKIES!
electronicboy
 
Posts: 1694
Joined: Mon Aug 31, 2009 6:18 pm

Re: Complex scripts

Postby Someone Else » Fri Jul 08, 2011 4:45 am

I actually use sine and cosine to get an angle out of e.normal, instead of tangent.

Code: Select all
//From my Portals series, which currently has a length of 1 because Algodoo spazzed and forgot my progress on level 2:
(normal)=>{math.acos(normal(1)) * ({math.asin(normal(0)) > 0} ? {1} : {-1})}

Stick this into the console (F11). You can then use, for example, scene.my.normal2angle(e.normal) to derive the angle from this.

Note that I use what is probably an odd coordinate system- 0 rads is up, positive numbers progress clockwise from there, negative numbers progress counterclockwise, math.pi rads is down.

Also, \n is a new line, \t is a tab, \\ is a backslash. There may be more, I don't know. These go into strings, to cause special characters to appear in some cases.

Try this:
Code: Select all
text = "a\\\nb\tc"

Should result in:
Code: Select all
a\
b      c
Matthias Wandel is epic, in my humble opinion.
I love my brain...
TC42 wrote:Also, your sig is too big, please change it.

ARE YA HAPPY NOW?????

Thymechanic/Phundamentalist

Recently, I discovered something a lot of you probably already knew: Minecraft is awesome.
Due to this, I may not be as active as usual for a while.
User avatar
Someone Else
 
Posts: 1147
Joined: Sun Nov 21, 2010 10:53 pm
Location: The Milky Way Galaxy


Return to Thyme scripting

Who is online

Users browsing this forum: No registered users and 5 guests