Read information from a spring's endpoint

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

Read information from a spring's endpoint

Postby UnityDogGaming04 » Fri Oct 08, 2021 5:08 am

I'm aware of a way to get information from the body a spring is attached to, but is there a way to, inside the postStep of a spring, read information from the endpoint itself, e.g. endpoint's size?
ALL HAIL THYME!
UnityDogGaming04
 
Posts: 17
Joined: Tue Oct 05, 2021 8:35 pm
Location: Buckeye, AZ

Re: Read information from a spring's endpoint

Postby Xray » Fri Oct 08, 2021 6:01 am

UnityDogGaming04 -- I started thinking about what you are asking and I even played around with a spring in one of my scenes, and I now have an answer for you!

You can read many of the parameters in both endpoints (endpoint0 and endpoint1) from the body of the spring this way:

First of all you must select the spring body by itself. If you simply click on a spring, all three parts of the spring (body and 2 endpoints) will be selected. The way you prevent that is by holding down the Alt key while you select the body of the spring, if that's what you want. You can also select either endpoint by itself the same way. Select with left click and bring up the Script Menu with right click.

Okay, in the Script Menu for the body you will see postStep. That's were you put the following code:

endPoint0 = (entityByID((readable(owner)).lineEndPoint0));
_size = endPoint0.size

I created a local variable called _size to store the size of endpoint0, but you can create a different local variable or even a global (scene.my) variable. If you wanted to get the endpoint0 pos, then the code is the same except instead of size, you would put pos in the script. Experiment with it, and you will be amazed what you can do with this!
User avatar
Xray
 
Posts: 500
Joined: Sun Jun 17, 2012 6:12 am
Location: USA

Re: Read information from a spring's endpoint

Postby UnityDogGaming04 » Fri Oct 08, 2021 6:19 am

Xray - Thanks! I'll try this out and see if it does what I need.

I'm using this to make a one way transmitter or "axon" from neuron to neuron, instead of lasers, to see if I can get more ergonomic with neural net design. Aiming lasers at neurons is tricky, especially considering line of sight issues. Having springs removes the need for aiming lasers, and also voids the need for empty space between the two connected neurons! The reason I want to measure the size of the endpoint is because I haven't been able to reliably assert that one endpoint of the spring is endpoint 0 or endpoint 1. Comparing the size of the neurons will allow me to create temporary variables that treat the bigger endpoint as the input of the axon and the smaller as the output, regardless of which endpoint actually is endpoint 0.
ALL HAIL THYME!
UnityDogGaming04
 
Posts: 17
Joined: Tue Oct 05, 2021 8:35 pm
Location: Buckeye, AZ

Re: Read information from a spring's endpoint

Postby UnityDogGaming04 » Fri Oct 08, 2021 6:53 am

[EDIT: Nevermind, using a variable like you did was necessary, when I thought it was optional. I got it working now!]
I'm failing to get anything to happen! I have this code in a spring (and not in the endpoints) that is meant to change the color of the spring based on which endpoint is bigger. This isn't for me to figure out which is endpoint 0, but just to debug, to test that something is happening, and it fails to do that. (This is in postStep)
Code: Select all
(e)=>{
    {
        (entityByID((readable(owner)).lineEndPoint0)).size > (entityByID((readable(owner)).lineEndPoint1)).size
    } ? {
        color = [1, 0, 0, 1]
    } : {
        color = [0, 1, 0, 1]
    }
}

The spring body should turn red or green, it doesn't matter to me which, as long as it does one or the other, but it doesn't, which tells me something isn't right with the phrase "(entityByID((readable(owner)).lineEndPoint0))",
ALL HAIL THYME!
UnityDogGaming04
 
Posts: 17
Joined: Tue Oct 05, 2021 8:35 pm
Location: Buckeye, AZ

Re: Read information from a spring's endpoint

Postby UnityDogGaming04 » Fri Oct 08, 2021 6:00 pm

Ok, well I'm stuck again. I changed my code to use the stand-in variables:
Code: Select all
(e)=>{
    e0 = (entityByID((readable(owner)).lineEndPoint0));
    e1 = (entityByID((readable(owner)).lineEndPoint1));
    {
        e0.size > e1.size
    } ? {
        color = (Scene.entityByGeomID((Scene.entityByID((readable(e.this)).lineEndPoint0))._ID)).color
    } : {
        color = (Scene.entityByGeomID((Scene.entityByID((readable(e.this)).lineEndPoint1))._ID)).color
    }
}

as well as this code in both endpoints:
Code: Select all
(e)=>{
    _ID = (readable(e.this)).geom
}

The _ID code was copied from a scene by JakubKubo (Scene ID: 202191), I don't know if it's necessary, but it seems to work for his scene.
What the code is meant to do is simply change the spring's color to the color of the geom that the bigger endpoint is on, but it fails.
ALL HAIL THYME!
UnityDogGaming04
 
Posts: 17
Joined: Tue Oct 05, 2021 8:35 pm
Location: Buckeye, AZ

Re: Read information from a spring's endpoint

Postby Xray » Fri Oct 08, 2021 10:18 pm

Try this. It works for me....


Code: Select all
(e)=>{
    e0 = (entityByID((readable(owner)).lineEndPoint0)).size;
    e1 = (entityByID((readable(owner)).lineEndPoint1)).size;
    {e0 > e1} ? {
        color = [1.0, 0.0, 0.0, 1.0]
    } : {
        color = [0.0, 1.0, 0.0, 1.0]
    }
}
User avatar
Xray
 
Posts: 500
Joined: Sun Jun 17, 2012 6:12 am
Location: USA

Re: Read information from a spring's endpoint

Postby UnityDogGaming04 » Fri Oct 08, 2021 11:04 pm

Xray - This changes color based on which endpoint is bigger, which is what I was using to debug, but now that I can do that, I want to have the spring turn the same color as the rigid body to which the larger endpoint is connected. This should give me everything I need to make an axon.
ALL HAIL THYME!
UnityDogGaming04
 
Posts: 17
Joined: Tue Oct 05, 2021 8:35 pm
Location: Buckeye, AZ

Re: Read information from a spring's endpoint

Postby UnityDogGaming04 » Fri Oct 08, 2021 11:54 pm

Okay, I have this arrangement, a spring with unequal size endpoints, the bigger on a blue circle, the smaller on a white circle. I just want the spring to turn blue, because that's the color of the bigger endpoint's attached geom.
Code: Select all
(e)=>{
    {(entityByID((readable(owner)).lineEndPoint0)).size > (entityByID((readable(owner)).lineEndPoint1)).size} ? {
     
    e0 = (entityByID((readable(owner)).lineEndPoint0));
    e1 = (entityByID((readable(owner)).lineEndPoint1));
    } : {
     
    e0 = (entityByID((readable(owner)).lineEndPoint1));
    e1 = (entityByID((readable(owner)).lineEndPoint0));
    };
   color = (entityByGeomID((readable(e0)).geom)).color
}

First the code compares the sizes of the endpoints, and sets the temporary variables e0 to the bigger one, and e1 to the smaller one. Then, it sets its color to the color of e0's attached geom. After all this, the spring should be blue, but it isn't. Are there certain ways that these "entity phrases" can't be used?
ALL HAIL THYME!
UnityDogGaming04
 
Posts: 17
Joined: Tue Oct 05, 2021 8:35 pm
Location: Buckeye, AZ

Re: Read information from a spring's endpoint

Postby Xray » Sat Oct 09, 2021 12:47 am

Take a look at this sample scene to see if you can use the script:

http://www.algodoo.com/algobox/details.php?id=244311
User avatar
Xray
 
Posts: 500
Joined: Sun Jun 17, 2012 6:12 am
Location: USA

Re: Read information from a spring's endpoint

Postby UnityDogGaming04 » Sat Oct 09, 2021 12:50 am

I'll see what I can do, thanks Xray!
ALL HAIL THYME!
UnityDogGaming04
 
Posts: 17
Joined: Tue Oct 05, 2021 8:35 pm
Location: Buckeye, AZ

Re: Read information from a spring's endpoint

Postby UnityDogGaming04 » Sat Oct 09, 2021 1:10 am

It worked in reading the values, I could get the spring to turn the color of a specific endpoint's attached geom, however writing to the values does not work. This is what I tried (I renamed the values a bit):
Code: Select all
(e)=>{
    endPoint0 = (entityByID((readable(owner)).lineEndPoint0));
    end0 = (entityByGeomID((readable(endPoint0)).geom));
    end0acc = end0._acc;
    end1acc = end1acc + end0acc;
    end1._acc = end1acc;
    (entityByID((readable(owner)).lineEndPoint1)) = endPoint1;
    (entityByGeomID((readable(endPoint1)).geom)) = end1;
}

It takes an _acc value from one geom and is meant to add it to the other geom, such that if I have the script menu of end1 open, the _acc value should change by 1 each frame. I imagine, since the scene is titled Spring Reads Data, that the data is read-only, but surely there's a way to write to it that I just don't know?
ALL HAIL THYME!
UnityDogGaming04
 
Posts: 17
Joined: Tue Oct 05, 2021 8:35 pm
Location: Buckeye, AZ

Re: Read information from a spring's endpoint

Postby Xray » Sat Oct 09, 2021 1:17 am

I was able to change the color of a box simply by reversing one of the last lines in the script. I changed the box color to white like this:

Code: Select all
Box1.color = [1, 1, 1, 1]
User avatar
Xray
 
Posts: 500
Joined: Sun Jun 17, 2012 6:12 am
Location: USA

Re: Read information from a spring's endpoint

Postby UnityDogGaming04 » Sat Oct 09, 2021 1:29 am

Strange. I added a line that set one box to add the position of the first box to its own position (don't really need to understand what this means, just that it does something) and it worked! By all means, the code doesn't look like it SHOULD work, but it does. Neat! now, time to try to actually use this stuff... There was a second application I had in mind, that being a rocket fuel or battery system, for an upcoming game of mine!
ALL HAIL THYME!
UnityDogGaming04
 
Posts: 17
Joined: Tue Oct 05, 2021 8:35 pm
Location: Buckeye, AZ

Re: Read information from a spring's endpoint

Postby UnityDogGaming04 » Sat Oct 09, 2021 5:31 am

My tester scene is up! It's a simple rocket building game! Hoorah for springs!
http://www.algodoo.com/algobox/details.php?id=244318
ALL HAIL THYME!
UnityDogGaming04
 
Posts: 17
Joined: Tue Oct 05, 2021 8:35 pm
Location: Buckeye, AZ

Re: Read information from a spring's endpoint

Postby Xray » Sat Oct 09, 2021 6:57 am

I'm glad I was able to help you. :thumbup:
User avatar
Xray
 
Posts: 500
Joined: Sun Jun 17, 2012 6:12 am
Location: USA


Return to Thyme scripting

Who is online

Users browsing this forum: No registered users and 2 guests

cron