Physion

  • Increase font size
  • Default font size
  • Decrease font size
Welcome, Guest
Please Login or Register.    Lost Password?

'Fun' with timing !
(1 viewing) (1) Guest
Go to bottomPage: 12
TOPIC: 'Fun' with timing !
#621
'Fun' with timing ! 3 Years, 1 Month ago  
Just some general info, for those interested. Things found, & working on.
Had wondered for a while if I could do things over time, in Physion.
Sounds strange you say, as things happen with physics in real time!

Let's say you want to change the '.Opacity' of an object, but NOT immediately, in
one step, but to gradually fade over a set time, in a number of steps?!!
I have a (work in progress) demo of that, you can find at....
workingfiles.netii.net/PhysionStuff/TimingDemo/
(I've not cluttered the Physion-Site Scenes with these sort of demo's).

In normal Javascript, there are timer functions you can set up, and leave running,
and stop them, which can be set up & initiated once, in an event, and they just keep
working by themselves in the background, until you call them to stop.
The event handlers are... setTimeout(), setInterval(), clearTimeout(), clearInterval().
They would be good! as they keep working away by themselves, outside other events.
However, I can not get them to work in the Physion/Qt environment ??

If they did work, (and if so, I'd like to know how), you COULD do something like....
var timer = setInterval("Box1.Opacity -= .05", 50);
setTimeout("clearInterval(timer)", 1000);
That WOULD have started an automatically repeating timer, that decreased the
opacity by .05, every 50 milliseconds, but stopped doing it after 1 second.
But it doesn't work. (For me, anyway). So I've done this, for now, and it involves
using the 'OnUpdate' event of another object, to keep doing things!....

There are many date/time functions, but the most simple, and useful, to me in this
example, is a 'world' property, called... 'world.Seconds'
This returns the number of seconds since running, to 15 decimal places !
IF you JUST put the following line in an 'OnUpdate' event....
... if (Box1.Opacity > .05) { Box1.Opacity -= .05; }
it would happen way too fast! as the event is called & updated very rapidly!
So although the event is called rapidly, in the demo, I've only allowed it to make
the change if 50 milliseconds has elapsed since the last time. (Set what you want).
The same demo also changes the 'Box1.X' value too, but NOTICE that this is NOT
the same as setting it in motion, with a linear-velocity & leaving it.

I've then done something similar with another kinematic box, with varying steps
of movement, whether going up or down. This may seem pointless, as you can
start a kinematic object moving with a code-line of... Box2.LinearVelocityX = 1;
and it will keep moving, unhindered. But this is to demonstrate how to 'animate'
things, by changing properties OVER TIME. This could have many uses, apart
from changing opacity, for eg.. at a certain time/event, animate a Dynamic-Body
into a certain position, above something particular, external to the physics.

Interestingly, Opacity change is fine, but changing the position of an object MUST
be done using ANOTHER objects 'OnUpdate' event, NOT it's own, as changing
any positions, immediately invokes another update for that objects 'OnUpdate'
event, so it screws itself up, mid-procedure! (Just don't do it!)

It looks complicated, but you can just use the 'Opacity' part for now, if you want.
What i propose to do, (later, as I've prommised myself to put it on hold until i
finish the Gravity/Magnet coding upload, very soon, illness!), and the new gears),
is to create a special function that will be installed with 'Evaluate-Script', that will
actually create a small sub-language! to use in scripts/events. Will look like this....

// Clear all previously defined steps. Maybe in an 'OnStart' event.
Animate(CLEARSTEPS);
// Set up new steps, in this example, change the 'Ball2' 'Y' property
// by steps of .1, every 50 milliseconds, and do it 10 times
app.STEP1 = "CHANGE(Ball2, Y, .1, 50, 10)";
app.STEP2 = "WAIT(1.25)"; // do nothing for 1250 milliseconds after that
app.STEP3 = "CHANGE(Ball2, Opacity, -.05, 50, 20);
// now call the main function...
Animate(1); // Process the steps, but only loop through them once!

(Animate(0) means continuous loop of steps, but not in the above example).
Then, some event, if it was looping, may call....
Animate(STOP);

But that's on hold for now. Have fun.

NOTE:
If you write some code in an event, like 'OnUpdate' or what ever, if you write
anything that is wrong, or mis-spell something, or wrong Javascript syntax, then
the 'WHOLE' event will be ignored. Not just the line. And here's the funny (??) part...
If the block of code in an event DID work, and then you accidentally changed it to
something that DID/WOULD NOT work, all of that event's code is ignored, BUT,
the last GOOD WORKING version in memory is working! Can cause confusion.
And always finish your blocks of code with an empty carriage-return.
Click on curly-brackets, (braces, { }), to see where the complimentary one is!
And indent all the code, to see better what is hapening!
Glenn555
Moderator
Posts: 110
graphgraph
User Offline Click here to see the profile of this user
Gender: Male Location: Bunbury, Western Australia Birthday: 05/01
Last Edit: 2011/10/05 10:27 By Glenn555.
1x1 square diag = root-2. Cube diag = root-3. Beautiful !
The administrator has disabled public write access.
 
#624
Re: 'Fun' with timing ! 3 Years, 1 Month ago  
You can use QTimer example code:
Code:

mytimer = new QTimer();
mytimer.interval = 1000; // Execute after 1000ms
mytimer.singleShot = true; // Execute only once (default is call repeatingly)
mytimer.timeout.connect(mytimer, MyQTimer);
mytimer.start();
...

function MyQTimer(event) {
 ...
}



Edit: Wtf .. code wont display ..

Dimitris what have you done?! The above code can't be displayed, look here instead: timer.script
Lynxx
Java/C#-Developer
Moderator
Posts: 198
graphgraph
User Offline Click here to see the profile of this user
Gender: Male Lynxx Homepage Location: Germany Birthday: 03/20
Last Edit: 2011/10/05 19:55 By Lynxx.
Infinity is vast, especially near end.
bye, Lynxx
The administrator has disabled public write access.
 
#632
Re: 'Fun' with timing ! 3 Years, 1 Month ago  
However, I can not get them (timers) to work in the Physion/Qt environment ??
The solution, as Lynxx shows in his example, is to use a QTimer with a callback function.

The solution you found with the 'world.Seconds' property does work but as far as 'animating properties' goes, I think that the best solution is to use a counter and the modulo operator as shown below:

Code:

itemX.OnStart = "this.counter = 0;"
itemX.OnUpdate = "if(++this.counter%10==0) this.Opacity -= 0.05;"



Interestingly, Opacity change is fine, but changing the position of an object MUST
be done using ANOTHER objects 'OnUpdate' event, NOT it's own, as changing
any positions, immediately invokes another update for that objects 'OnUpdate'
event, so it screws itself up, mid-procedure! (Just don't do it!)
Hadn't thought about that implication so far. I guess you're right. So here the QTimer comes again for the rescue

...create a small sub-language!
By that you probably mean to create a .script file which will contain a set of functions that will act as wrappers for more complicated stuff. Pretty much like the function shown below.

Code:

function setBumper(bodyItem)
{
var s = "";
s += "if(this.bumperForce == null) this.bumperForce = 10.0;\n"
s += "var m = other.Mass;\n\n";
s += "var p1 = other.position();\n";
s += "var p2 = new QPointF(p1.x(), p1.y() + m*this.bumperForce);\n";
s += "var line = new QLineF(p1, p2);\n";
s += "other.applyLinearImpulse(line);\n";
bodyItem.OnContact += s;
}



If this is the case then creating a small sub-language would be great! (But I agree that your magnets coding should finish first)

Lynxx Does that display problem still holds?
Dimitris
Admin
Posts: 210
graph
User Offline Click here to see the profile of this user
The administrator has disabled public write access.
 
#634
Re: 'Fun' with timing ! 3 Years, 1 Month ago  
Thanks for all that, from Lynxx & Admin.
Just what I needed, and is how i wanted to go, with timers.
I'd perused the 'Qt' sites, but got confused about the implementation.
(Good bye 'OnUpdate' !)

The coding blocks supplied look fine to me.

Admin, Re: 'Sub-Language'...
Yes, it will be a collection of sub-functions, created by a master function,
that will translate the 'STEPS' into specific timed/staged events.
(Main reason for wanting the timer functions).
Thanks a lot for all that.

Re: Gravity/Magnets...
The last month has not been kind to me, but i'm enjoying all this 'distraction'.
My code function has been working for a while, but wanted to add more features.
Trying to be as realistic as possible. (Takes into account mass/density too!).
And north/south magnets attract/repel ! and much more. Being laid up, i went
overboard writing some 'White-Pages' (ha) .PDF documents, relating to gravity,
and the reasoning behind the Function. (Had nothing else to do!).
But i WILL put a version up in the next day or 2, at the most. With demo scenes.
P.S.
You gave me some scripting, for collision-detecting just outside an object, which
i thank you for, and am playing around with. But in a later version of this Gravity-
Magnet handling, i'm having trouble finding the border around an odd-shaped
polygon. It's important, as calculations modify a bit, when very close, to stop
things like a Force value jumping exceedingly high, before touching.
(Will be more apparent later).
Thanks for everything. And, YES, the other stuff is on hold!! Ha ha.
Glenn555
Moderator
Posts: 110
graphgraph
User Offline Click here to see the profile of this user
Gender: Male Location: Bunbury, Western Australia Birthday: 05/01
1x1 square diag = root-2. Cube diag = root-3. Beautiful !
The administrator has disabled public write access.
 
#636
Re: 'Fun' with timing ! 3 Years, 1 Month ago  
Dimitris wrote:
Lynxx Does that display problem still holds?
Yes, and never had this problem before ..
The end of the codeblock in post 624 should be:
Code:


function MyQTimer(event) {
 ...
}



lol ..
it should be:

function MyQTimer(event) {
...
}

like here.

Already tried inserting special-chars and stuff like:

fun_ction_My_QTi_mer(ev_ent) {
...
}

but nothing helps, code-block ist somehow buggy.
Lynxx
Java/C#-Developer
Moderator
Posts: 198
graphgraph
User Offline Click here to see the profile of this user
Gender: Male Lynxx Homepage Location: Germany Birthday: 03/20
Last Edit: 2011/10/06 12:37 By Lynxx.
Infinity is vast, especially near end.
bye, Lynxx
The administrator has disabled public write access.
 
#640
Re: 'Fun' with timing ! 3 Years, 1 Month ago  
Lynxx.
All the code-blocks, from you and Admin, look perfect to me ?
All correct layout, colour-coded, indented. Can select, copy/paste fine.
Glenn555
Moderator
Posts: 110
graphgraph
User Offline Click here to see the profile of this user
Gender: Male Location: Bunbury, Western Australia Birthday: 05/01
1x1 square diag = root-2. Cube diag = root-3. Beautiful !
The administrator has disabled public write access.
 
Go to topPage: 12
Moderators: Lynxx, Glenn555