Object Variables?

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

Object Variables?

Postby mold999 » Sat Jan 12, 2013 4:11 am

I've heard of these "object variables" or something of the sort, but I can't quite understand how they work.

I copied a box (Lets call it box1), and pasted it into the console with the variable name, "scene.my.box".

I then went to another box (Lets call it box2), and tried to set the text to the position of box1. I tried:

Text = {"" + scene.my.box.pos}
Text = {"" + (scene.my.box).pos}

And also

Text = {"" + readable(scene.my.box).pos}

I don't have a good idea of how readable works, but I tried it anyway. I don't even know what readable is used for.

Is there any way I can inform box2 of box1's properties?

Thanks,

~~mold999
Phundementalism?

Ain't nobody got
thyme fo dat.
User avatar
mold999
 
Posts: 225
Joined: Sun Jan 02, 2011 1:35 am
Location: [176436, 3763234]

Re: Object Variables?

Postby Kilinich » Sat Jan 12, 2013 3:25 pm

copy-paste will not work.
You need to create object like that: scene.my.box = scene.addbox({...}).
or
You can assign object via e.geom/e.this/e.other in onLaserHit/onHitBylaser/onCollide event:
onCollide = (e) => {scene.my.box = e.other}

after that you can read/modify object using scene.my.box
scene.my.box.color = [1,1,1,1]
etc.
:thumbup:
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: Object Variables?

Postby mold999 » Mon Jan 21, 2013 4:10 am

Ok, I made my box like this:

scene.my.box = scene.addbox({size := [1, 1]; color := [1, 0, 0, 1]; pos := [-2, 1]})

And it made a red box on the screen. Now, is there any way to edit the box's properties? I made a circle, and in the onCollide, typed this:

onCollide = (e)=>{scene.my.box.color = [1, 1, 1, 1]}

But nothing happens to the box.

--mold999
Phundementalism?

Ain't nobody got
thyme fo dat.
User avatar
mold999
 
Posts: 225
Joined: Sun Jan 02, 2011 1:35 am
Location: [176436, 3763234]

Re: Object Variables?

Postby Kilinich » Mon Jan 21, 2013 8:12 am

That works perfectly. I've just did exactly what you said to check. :thumbup:
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: Object Variables?

Postby Xray » Sun Jan 27, 2013 4:16 am

I tried it too, and it works as expected. Are you still having trouble with it, mold999?
User avatar
Xray
 
Posts: 501
Joined: Sun Jun 17, 2012 6:12 am
Location: USA

Re: Object Variables?

Postby mold999 » Sun Jan 27, 2013 7:02 am

No, I got it. :D

I made a syntax mistake. :mrgreen:
Phundementalism?

Ain't nobody got
thyme fo dat.
User avatar
mold999
 
Posts: 225
Joined: Sun Jan 02, 2011 1:35 am
Location: [176436, 3763234]

Re: Object Variables?

Postby ulfwil » Sat Mar 16, 2013 8:56 am

Hi all there,

I have tried to follow this thread. It works for me to, but just once. When I undo and runs again the effect with the changing color is not there. Is there an explanation why? (I have version 2.0.1)

.... some test done .... continue my story.....

I guess that the restart of simulation, also includes my creation of the box and my retrieved reference, which then could explain way it doesn't work the second time, but the box itself doesn't disappear which seems a little bit strange. (I just upgraded to 2.1.0 b4, but it behaves the same way ). So does someone have an explanation?
ulfwil
 
Posts: 4
Joined: Wed Mar 06, 2013 12:08 am

Re: Object Variables?

Postby Xray » Sat Mar 16, 2013 5:11 pm

ulfwil -- I confirmed your observation. I believe what's happening is when you UNDO, the function "scene.my.box" that was created in the console was also UNDO'd as if it never happened. So, after you undo the scene, and are now technically back to the point before you created the function scene.my.box, you need to re-enter it into the console. After doing that, the collide worked as expected. Now, I don't know whether or not it's supposed to work that way, so maybe Kilinich or one of the other Algodoo experts can comment about that.
User avatar
Xray
 
Posts: 501
Joined: Sun Jun 17, 2012 6:12 am
Location: USA

Re: Object Variables?

Postby ulfwil » Thu Mar 21, 2013 10:14 pm

My real problem for the moment, I have for some time an idea I want to test. But the scripting "language" is not clear to me. Nothing like what I'm used to, and the tutorials doesn't give me much (maybe I am too old :-( )
Anyway, this is what I want to achieve:
I want a circle (eventually whith a connected thruster), that is affected by a force depending of its velocity and direction. i.e. simulating an electron in a magnetic field, so the force will depend on direction and speed. And then I want a lots of electrons at the same time having this behaviour.

Anyone any ideas?
ulfwil
 
Posts: 4
Joined: Wed Mar 06, 2013 12:08 am

Re: Object Variables?

Postby carl00s01 » Fri Mar 22, 2013 10:55 pm

ulfwil wrote:I want a circle (eventually whith a connected thruster), that is affected by a force depending of its velocity and direction. i.e. simulating an electron in a magnetic field, so the force will depend on direction and speed. And then I want a lots of electrons at the same time having this behaviour.

Anyone any ideas?


Well, I don't understand what you want to do, could you explain it a little better? But I can try to help you.
First, you can get the velocity's direction (angle) using the inverse tangent of the tangent.
For example:
Code: Select all
math.atan(tan)

The tangent in this case is vel(1)/vel(0)
So:
Code: Select all
math.atan(vel(1)/vel(0))

However it will give you only -pi/2 to pi/2 values - the angle is not accurate when x vel is negative.
You need to put an If:
Code: Select all
math.atan(vel(1)/vel(0)) - (vel(0) < 0 ? math.pi : 0)

But the thruster does not have vel value. You'll need to access using Scene.entityByGeomID:
Code: Select all
me := (readable(entity));
v := (Scene.entityByGeomID(me.geom)).vel;
math.atan(v(1) / v(0)) - (v(0) < 0 ? math.pi : 0)

The first 2 lines access the geom that is glued to the thruster.

And to get the velocity's vector:
Code: Select all
math.vec.len(vel)

"vel" is used only when the object has this value on script menu.

Scene example:
Rating: rated 5
Filesize: 20.69 kB
Comments: 0
Ratings: 1
download


I hope helped you.

Sry for any misspelling
User avatar
carl00s01
 
Posts: 24
Joined: Fri Nov 26, 2010 7:03 pm
Location: Brazil

Re: Object Variables?

Postby ulfwil » Sat Mar 23, 2013 11:53 pm

Many thanks for this.
The clue were the following code for the force property for the thruster:
Code: Select all
  {
        me := Scene.entityByGeomID((readable(entity)).geom);
        v := me.vel;
        a := math.atan(v(1) / v(0)) - (v(0) < 0 ? math.pi : 0);
        rotation = a - math.pi;
        math.vec.len(v)
    };

As far as I understand so will th eforce get the value of the last setting in the rutine, in this case math.vec.len(v). Right?
Seems to work and I will go further from here before I reach my goal. But during that time thanks alot.
ulfwil
 
Posts: 4
Joined: Wed Mar 06, 2013 12:08 am

Re: Object Variables?

Postby carl00s01 » Sun Mar 24, 2013 6:13 am

Yes. You're right.
User avatar
carl00s01
 
Posts: 24
Joined: Fri Nov 26, 2010 7:03 pm
Location: Brazil

Re: Object Variables?

Postby ulfwil » Wed Apr 03, 2013 11:20 pm

Hi again,
Now do I have another question. Is there a way to set the restitution for each collide? What I want is a plane or wall that depending on the hitting object speed change the collisions restitution. A fast object will have a less value and a slow object a value of 1.0. Is it possible to use the onCollide routine to change the property or will the new value been set after the hit is already calculated? And what happens if there are two collisions "at the same time" in such case.
ulfwil
 
Posts: 4
Joined: Wed Mar 06, 2013 12:08 am

Re: Object Variables?

Postby electronicboy » Thu Apr 04, 2013 1:35 am

In real-time physics, it's best to say that 'on-collide' is actually done AFTER the collision.

In the new beta in the announcement section, it is possible to run a calculation per step, or in the current stable release, place your calculation inside '{}' in the restitution section and it should use the result of the formula for the value.
When asking for help, READ THE STICKIES!
electronicboy
 
Posts: 1694
Joined: Mon Aug 31, 2009 6:18 pm


Return to Thyme scripting

Who is online

Users browsing this forum: No registered users and 4 guests