Help with script?

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

Help with script?

Postby Rideg » Thu Jan 13, 2011 8:31 pm

Hello, can someone tell me why this script ain't working. It is supossed to spawn a box that is pointed towards the activated laser. Instead it spawnes with that angle - or + 90 degrees. Here's the code:

Code: Select all
(e)=>{
    Scene.addBox({
        color := [0.42, 0.42, 0.42, 1.0];
        onCollide := (e)=>{e.this.collideSet = 32};
        heteroCollide := true;
        friction := 1.0;
        drawBorder := true;
        airFrictionMult := 0.0;
        density := 10.0;
        collideSet := 64;
        zDepth := +inf;
        pos := e.pos;
        angle := math.atan(e.normal(1) / (e.normal(0) + 1)) * 2;
        size := [0.085, 0.6]
    })
}
Image
make sure to check out my work.
User avatar
Rideg
 
Posts: 948
Joined: Tue Dec 15, 2009 5:17 pm
Location: Östersund, Sweden

Re: Help with script?

Postby gradyfitz » Sat Jan 15, 2011 12:42 pm

Rideg wrote:Hello, can someone tell me why this script ain't working. It is supossed to spawn a box that is pointed towards the activated laser. Instead it spawnes with that angle - or + 90 degrees. Here's the code:

Code: Select all
(e)=>{
    Scene.addBox({
        color := [0.42, 0.42, 0.42, 1.0];
        onCollide := (e)=>{e.this.collideSet = 32};
        heteroCollide := true;
        friction := 1.0;
        drawBorder := true;
        airFrictionMult := 0.0;
        density := 10.0;
        collideSet := 64;
        zDepth := +inf;
        pos := e.pos;
        angle := math.atan(e.normal(1) / (e.normal(0) + 1)) * 2;
        size := [0.085, 0.6]
    })
}

This is a side effect of the fact that a/b = -a/-b, so when you call the inverse tangent function with the value you have given, it's difficult to differentiate two positive values from two negative values.

There is a thread with a function - atan2, which takes both points (in an array) and returns a value in radians, Some extra math functions.
(atan2 function by standardtoaster)
Code: Select all
Scene.my.atan2=(P)=>{2 * math.atan(P(1) / (((P(0) ^ 2 + P(1) ^ 2) ^ 0.5) + P(0)))}


The function can simply be used by putting in:
Code: Select all
Scene.my.atan2(e.normal)


Or if you need to modify the normal:
Code: Select all
Scene.my.atan2([e.normal(0) + x, e.normal(1) + y])

(where x and y are any numbers, just for demonstration)

To change this to point to the laser, you would probably be better off getting both points and working out the angle between them.

Hopefully this helped :).
Mechanisms: 18 Mechanisms.
Thyme: Tutorial - Variables/Commands List.
Thymechanic
gradyfitz
 
Posts: 174
Joined: Tue Sep 01, 2009 8:33 am
Location: Victoria, Australia

Re: Help with script?

Postby Rideg » Sat Jan 15, 2011 4:11 pm

:thumbup: That has to be the best answer I can get. Thank you alot, well explained
Image
make sure to check out my work.
User avatar
Rideg
 
Posts: 948
Joined: Tue Dec 15, 2009 5:17 pm
Location: Östersund, Sweden


Return to Thyme scripting

Who is online

Users browsing this forum: No registered users and 2 guests