Writing and reading geometry variables

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

Writing and reading geometry variables

Postby Xray » Sun Aug 12, 2012 3:33 am

I have two separate questions, and would appreciate help for either or both.

1. For my first question, here is what I am trying to do: I want to spawn a circle from an OnCollide script, and have the color of the spawned circle be taken from an array element with contains the color. For example: {color=scene.my.colorarray(17)}. The array element 17 contains the value: [1,1,0,1] which colors the circle yellow. That script works just fine. But I want to arrive at the desired array element by calculating the element index. For example, if the result of my calculation is 17, and it is in the variable N, then I could set the circle color by: {color=scene.my.colorarray(N)}. That works Okay too, but it's not exactly what I want because the color of the circle will change each time N changes to a different value. What I want is to have the VALUE of N get placed into the script that sets the color. That is, I want to end up with: color=scene.my.colorarray(17)} so that the color of the spawned circle will change when I change that particular array element, and NOT the index, N. How can I tell Algodoo that I want the VALUE of N to be placed in the spawned script, rather than the name of the variable, N? I tried things like: {color=scene.my.colorarray(math.tostring(N))}, but that, and many other variations did not work.

2. My second question involves somehow sensing the color (or other attributes) of a geometry without having to use OnCollide. For example, I want to be able to slide a circle across my scene, and have it change to the colors of other objects that it happnes to be in contact with, but not in collision with. As I drag it around, I want it to be continuously sensing for other objects. If my circle is stopped over another object, and if the other object color changes, I want the color of the circle to immediately change to the other geometry without requiring a collision. Is this even possible?

THANKS!
User avatar
Xray
 
Posts: 500
Joined: Sun Jun 17, 2012 6:12 am
Location: USA

Re: Writing and reading geometry variables

Postby tatt61880 » Sun Aug 12, 2012 5:35 am

1. Your method works for me.
Rating: rated 5
Filesize: 20.01 kB
Comments: 0
Ratings: 1
download


2.
For example, define "Scene.my.color" and set attraction = {Scene.my.color = owner.color; 0}.
Then, Scene.my.color will return the value of the geom color.
NOTE: I'm not an Algoryx member.
Hi, Algodoo lovers. Have you read next topic? Featured scenes suggestions
To translators: English.cfg changelog will be useful (even for me).
User avatar
tatt61880
[Most Helpful Person 2010]
 
Posts: 1150
Joined: Mon Aug 31, 2009 5:45 pm
Location: Tokyo, Japan

Re: Writing and reading geometry variables

Postby Xray » Sun Aug 12, 2012 6:46 am

tatt61880 wrote:1. Your method works for me.
Rating: rated 5
Filesize: 20.01 kB
Comments: 0
Ratings: 1
download


2.
For example, define "Scene.my.color" and set attraction = {Scene.my.color = owner.color; 0}.
Then, Scene.my.color will return the value of the geom color.


Tatt - Thanks very much for your reply (you have helped me many times in the past, and I appreciate it very much!) but you do not understand my question. In the OnCollide parameter of the circle to be spawned, you need to put curly brackets around the script so that the actual code gets written to the color parameter, like this: {Scene.my.colorarray(Scene.my.colorarrayIndex)}. This way, I could change the color of the array element, and the color of that particular circle will then change along with it. But I do not want the NAME of the index to be written to the script, but rather the VALUE of the index. That way, if the index value should change (which it will) then the color of the circle will not change because the script has a constant for the array index instead of a variable. Do you get it now? In other words, I want the script that's written to the spawned circle to be: {Scene.my.colorarray(21)}. I need some way to write the VALUE of the array index to that script so that the color can change when the array element changes, but NOT when the array index changes.

Concerning your answer number 2, you apparently do not understand that one either! I do not want the color of of the circle being dragged by me (owner) but rather the color of other geometries that the circle comes in contact with as it's moving across the screen. I want to do that without using OnCollide because I want to detect the color at any point along the surface of the geometry, and not only when it collides. Do you get it now?

Thanks again!
User avatar
Xray
 
Posts: 500
Joined: Sun Jun 17, 2012 6:12 am
Location: USA

Re: Writing and reading geometry variables

Postby Ivan » Sun Aug 12, 2012 1:29 pm

1. For the first question, I have modified the onCollide in Tatt's scene a bit.
for xray (forum topic 7134).phz
(21.91 KiB) Downloaded 44 times


2. It should be possible. The underlying circles would have to send their positions and colors to an array and the circle that is flying over should test if it's within some radius to the center of any of these circles and change its color to the color of the circle that it is enough close to.
I might try to make something later.
Image
User avatar
Ivan
 
Posts: 202
Joined: Tue Oct 06, 2009 2:01 pm
Location: Croatia

Re: Writing and reading geometry variables

Postby Xray » Sun Aug 12, 2012 5:50 pm

Ivan wrote:1. For the first question, I have modified the onCollide in Tatt's scene a bit.
for xray (forum topic 7134).phz


YES, THAT WORKS EXACTLY HOW I WANT IT TO!!! THANKS, IVAN!! :clap:

2. It should be possible. The underlying circles would have to send their positions and colors to an array and the circle that is flying over should test if it's within some radius to the center of any of these circles and change its color to the color of the circle that it is enough close to.
I might try to make something later.


Yes, that should work, but it sounds a little more complex than I was hoping it would be, and it's a little beyond my expertise with Thyme script, so I hope that you could find the time to write the script for me. :D

Thanks again!!
User avatar
Xray
 
Posts: 500
Joined: Sun Jun 17, 2012 6:12 am
Location: USA

Re: Writing and reading geometry variables

Postby Ivan » Sun Aug 12, 2012 10:27 pm

I don't know if this is satisfying, but I made something that works and I will try to improve it tommorow if you or someone else like Tatt don't figure out something better.
Here it is. Just move the small circle around. If its center is within the radius of 1 meter from the center of a large circle, it will change its color respectively.
Attachments
Color sensing.phz
(18.47 KiB) Downloaded 46 times
Image
User avatar
Ivan
 
Posts: 202
Joined: Tue Oct 06, 2009 2:01 pm
Location: Croatia

Re: Writing and reading geometry variables

Postby tatt61880 » Mon Aug 13, 2012 12:39 am

Xray wrote:but you do not understand my question.

That is because your question is incorrect. :D
{color=scene.my.colorarray(N)} isn't same with color={scene.my.colorarray(N)}
NOTE: I'm not an Algoryx member.
Hi, Algodoo lovers. Have you read next topic? Featured scenes suggestions
To translators: English.cfg changelog will be useful (even for me).
User avatar
tatt61880
[Most Helpful Person 2010]
 
Posts: 1150
Joined: Mon Aug 31, 2009 5:45 pm
Location: Tokyo, Japan

Re: Writing and reading geometry variables

Postby Xray » Mon Aug 13, 2012 12:44 am

Ivan wrote:I don't know if this is satisfying, but I made something that works and I will try to improve it tommorow if you or someone else like Tatt don't figure out something better.
Here it is. Just move the small circle around. If its center is within the radius of 1 meter from the center of a large circle, it will change its color respectively.


Hey, that works pretty well! GOOD JOB! Now, I would like you to modify it just a bit so that the color changes back to some fixed base color (such as black) when the small circle moves OUT of the radius of the large circle. Also, I was wonding if it's possible to sense the color of the background, and have that become the base color? I will try to figure this stuff out myself, if you or other people do not have much time to devote to this. In other words, I don't expect people to do all of my work for me! I will learn much better if I have to try and figure things out for myself. And I could usually do that once I'm given the basics. You (and some other people) have helped me a great deal, and I appreciate it! :thumbup:
User avatar
Xray
 
Posts: 500
Joined: Sun Jun 17, 2012 6:12 am
Location: USA

Re: Writing and reading geometry variables

Postby Xray » Mon Aug 13, 2012 12:53 am

tatt61880 wrote:
Xray wrote:but you do not understand my question.

That is because your question is incorrect. :D
{color=scene.my.colorarray(N)} isn't same with color={scene.my.colorarray(N)}


Tatt - The thing that made Ivan's scene work is he created a local variable "_N" for each spawned circle which cannot be changed by global scripts. The only way to change the color of a specific spawned circle is to change the value of its associated array element in scene.my.colorarray. It works exactly as I need it to.

Thanks for your help!
User avatar
Xray
 
Posts: 500
Joined: Sun Jun 17, 2012 6:12 am
Location: USA

Re: Writing and reading geometry variables

Postby Xray » Mon Aug 13, 2012 7:47 am

Ivan wrote:I don't know if this is satisfying, but I made something that works and I will try to improve it tommorow if you or someone else like Tatt don't figure out something better.
Here it is. Just move the small circle around. If its center is within the radius of 1 meter from the center of a large circle, it will change its color respectively.


Ivan - I made a slight mod to your scene so that the small circle senses the other circles at their radius, rather than at a fixed distance. I did this by saving the radius of each large circle in the array. The array elements for each circle now looks like this: scene.my.array = [color, pos, radius]. I figured this out by myself, and I'm pretty proud of myself right now! :clap:

The only issue with this mod is, it can only work with circles, which is Okay for my current purpose. I may mod it again later so that it will detect other geomitry shapes too.
User avatar
Xray
 
Posts: 500
Joined: Sun Jun 17, 2012 6:12 am
Location: USA

Re: Writing and reading geometry variables

Postby Ivan » Mon Aug 13, 2012 12:33 pm

Here is an improved version of my scene without errors like "Stack overflow" and "List index out of bounds".
The circle now has background color if it's not close to any of the big circles. I'm pretty proud of myself right now too! :clap:

EDIT: corrected typo
Attachments
Color sensing.phz
(19.76 KiB) Downloaded 48 times
Last edited by Ivan on Mon Aug 13, 2012 9:08 pm, edited 1 time in total.
Image
User avatar
Ivan
 
Posts: 202
Joined: Tue Oct 06, 2009 2:01 pm
Location: Croatia

Re: Writing and reading geometry variables

Postby Xray » Mon Aug 13, 2012 4:53 pm

Ivan wrote:Here is an improved version of my scene which has errors like "Stack overflow" and "List index out of bounds".
The circle now has background color if it's not close to any of the big circles. I'm pretty proud of myself right now too! :clap:


NICE JOB! :clap: That's just what I'm looking for! Now, I'll combine your mod with my mod, and I'll be able to sense the color of circles at their edges regardless of their size, and the sensing circle will switch back to the background color when it leaves the outside edges of the circles. PERFECT! :thumbup:

Thanks again! (We make a great team. :D )
User avatar
Xray
 
Posts: 500
Joined: Sun Jun 17, 2012 6:12 am
Location: USA

Re: Writing and reading geometry variables

Postby tatt61880 » Tue Aug 14, 2012 5:10 pm

_N isn't needed if you used eval();
I don't know which is better.
Code: Select all
(e)=>{
    eval(
    @"
        Scene.addBox({
            color := { Scene.my.colorarray(" + Scene.my.N + @")};
            pos := e.pos;
            collideSet := 0;
        });
    ");
}
NOTE: I'm not an Algoryx member.
Hi, Algodoo lovers. Have you read next topic? Featured scenes suggestions
To translators: English.cfg changelog will be useful (even for me).
User avatar
tatt61880
[Most Helpful Person 2010]
 
Posts: 1150
Joined: Mon Aug 31, 2009 5:45 pm
Location: Tokyo, Japan

Re: Writing and reading geometry variables

Postby Xray » Tue Aug 14, 2012 5:45 pm

tatt61880 wrote:_N isn't needed if you used eval();
I don't know which is better.
Code: Select all
(e)=>{
    eval(
    @"
        Scene.addBox({
            color := { Scene.my.colorarray(" + Scene.my.N + @")};
            pos := e.pos;
            collideSet := 0;
        });
    ");
}


Tatt - What does eval do? I found the definition in the Thyme commands and variables list, but all it says is, "evaluates a function", which doesn't explain how to use it. Could you please explain exactly what your script shown above does, and how it works?

Thanks very much!
User avatar
Xray
 
Posts: 500
Joined: Sun Jun 17, 2012 6:12 am
Location: USA

Re: Writing and reading geometry variables

Postby electronicboy » Tue Aug 14, 2012 9:55 pm

The oncollide is EVAL'd everytime the object collides with something.

Eval, essentially just means the code is run, if I remember correctly :)
When asking for help, READ THE STICKIES!
electronicboy
 
Posts: 1694
Joined: Mon Aug 31, 2009 6:18 pm

Re: Writing and reading geometry variables

Postby Xray » Tue Aug 14, 2012 10:59 pm

electronicboy wrote:The oncollide is EVAL'd everytime the object collides with something.

Eval, essentially just means the code is run, if I remember correctly :)


Huh? :crazy: The script in OnCollide gets run every time the object collides with another object, so how is what you said any different?
User avatar
Xray
 
Posts: 500
Joined: Sun Jun 17, 2012 6:12 am
Location: USA

Re: Writing and reading geometry variables

Postby tatt61880 » Wed Aug 15, 2012 6:33 pm

eval function is commonly used function, not only in Thyme but also in Perl, Python, Lisp, ....
So, just googling the word will help you.

By the way, I think you may don't know what @" " is about.
Just google "verbatim string literal" for that. This is C# style string literals that starts with @".
README.txt wrote:=== Algodoo 2.0.0 RC1, 2011-09-23 ===
* NEW: Thyme: Added support for C# style string literals that starts with @" and ends with ".
NOTE: I'm not an Algoryx member.
Hi, Algodoo lovers. Have you read next topic? Featured scenes suggestions
To translators: English.cfg changelog will be useful (even for me).
User avatar
tatt61880
[Most Helpful Person 2010]
 
Posts: 1150
Joined: Mon Aug 31, 2009 5:45 pm
Location: Tokyo, Japan

Re: Writing and reading geometry variables

Postby electronicboy » Wed Aug 15, 2012 11:31 pm

Xray wrote:
electronicboy wrote:The oncollide is EVAL'd everytime the object collides with something.

Eval, essentially just means the code is run, if I remember correctly :)


Huh? :crazy: The script in OnCollide gets run every time the object collides with another object, so how is what you said any different?


In short, when the code is "evaluated", it is processed/ran.
EVAL'd = ran.
When asking for help, READ THE STICKIES!
electronicboy
 
Posts: 1694
Joined: Mon Aug 31, 2009 6:18 pm

Re: Writing and reading geometry variables

Postby Ivan » Thu Aug 16, 2012 12:26 pm

I was googling and this is how I understand it.
eval() evaulates a string, i.e. it runs the code contained by the string. It seems to be very useful. Thanks Tatt!
Code: Select all
string := "2+3";

//input:
print(string);
print(eval(string));

//output:
  2+3
  5

And here I have found information about the string literal.
Image
User avatar
Ivan
 
Posts: 202
Joined: Tue Oct 06, 2009 2:01 pm
Location: Croatia

Re: Writing and reading geometry variables

Postby Xray » Sat Nov 17, 2012 8:59 am

tatt61880 wrote:By the way, I think you may don't know what @" " is about.
Just google "verbatim string literal" for that. This is C# style string literals that starts with @".
README.txt wrote:=== Algodoo 2.0.0 RC1, 2011-09-23 ===
* NEW: Thyme: Added support for C# style string literals that starts with @" and ends with ".


tatt - I was reading over this thread and I was wondering where you got that "README.txt" file from? Is there documentation on-line somewhere that describes new additions to Thyme? If so, where can I find it?

Thanks!
User avatar
Xray
 
Posts: 500
Joined: Sun Jun 17, 2012 6:12 am
Location: USA

Re: Writing and reading geometry variables

Postby tatt61880 » Sat Nov 17, 2012 10:02 am

Xray wrote:
tatt61880 wrote:By the way, I think you may don't know what @" " is about.
Just google "verbatim string literal" for that. This is C# style string literals that starts with @".
README.txt wrote:=== Algodoo 2.0.0 RC1, 2011-09-23 ===
* NEW: Thyme: Added support for C# style string literals that starts with @" and ends with ".


tatt - I was reading over this thread and I was wondering where you got that "README.txt" file from? Is there documentation on-line somewhere that describes new additions to Thyme? If so, where can I find it?

Thanks!


C:\Program Files\Algodoo\README.txt
NOTE: I'm not an Algoryx member.
Hi, Algodoo lovers. Have you read next topic? Featured scenes suggestions
To translators: English.cfg changelog will be useful (even for me).
User avatar
tatt61880
[Most Helpful Person 2010]
 
Posts: 1150
Joined: Mon Aug 31, 2009 5:45 pm
Location: Tokyo, Japan

Re: Writing and reading geometry variables

Postby Xray » Sat Nov 17, 2012 7:25 pm

tatt61880 wrote:C:\Program Files\Algodoo\README.txt


I did not know that was there. :oops: THANKS! :clap:
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 7 guests

cron