[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [x3d-public] Random animation of a geometric primitive (UNCLASSIFIED)




Does anyone have/"know of" sample code for random motion of a primitive, say a Box?


Hello Andrew,

Here is a sample script in VRML . It generates a random
position inside the unit box at every frame, so it is "white noise".

DEF Trans Transform {
children Shape {
appearance Appearance {
material Material {}
}
geometry Box {}
}
}
DEF Timer TimeSensor {
loop TRUE
}
DEF Scr Script {
field SFNode Trans USE Trans
eventIn SFTime time
url "javascript:
function time(){
Trans.translation=new SFVec3f(Math.random(),Math.random(),Math.random());
}
"
}
ROUTE Timer.time TO Scr.time


second example snipped

Sergey Bederov


Sergey always has such great examples to bulid from, and this is
another. I want to show it as VRML Classic and add two items to the
"white noise" position example.

First, for something like this, I would prefer to include ROUTE for
all the data, instead of the direct USE syntax in Sergey's example..
To me, this is still much easier to think about and document than
the idea of USEing a node, and I believe it may allow better browser
optimization that may be meaningfull in large scenes.

Instead of
DEF Scr Script {
field SFNode Trans USE Trans
eventIn SFTime time
url "javascript:
function time(){
Trans.translation=new SFVec3f(Math.random(),Math.random(),Math.random());
}
"
}
ROUTE Timer.time TO Scr.time


Let's employ a ROUTE to replace the USE:
DEF BoxMover Script {
 inputOnly SFTime cycleTime
 outputOnly SFVec3f newBoxPosition
 url ["random2.js"]
}
ROUTE BoxTimer.cycleTime TO BoxMover.cycleTime
ROUTE BoxMover.newBoxPosition TO Box.translation

random2.js
function cycleTime(){
 newBoxPosition=new SFVec3f(
3*Math.random(),3*Math.random(),10*Math.random()
);
}

The names are changed but the ideas are to first move
the script out into its own file, and next to rig it so
that ROUTE is used for both the gozinta and the gozouta.

Next, I want to show two aspects of the TimeSensor eventOut(s).
One, the cycleTime, which emits an event once each complete
cycle of the sensor, and also the time event which is emmitted
each time the sensor is polled.

It turns out that this also serves as a transparency test because you
should be able to see both objects at all times.

#X3D V3.0 utf8
PROFILE Immersive
META "filename" "random2.x3dv"

NavigationInfo {
 type [ "EXAMINE" "ANY" ]
 headlight FALSE
}

Background { skyColor 0.5 0.76 0.76 }

DEF Entry Viewpoint { description "Entry"
 position 0 0 18
}

DEF Box Transform { rotation 1 1 0 0.2
 children [
   Shape {
     appearance Appearance {
       material DEF red_glass Material {
         ambientIntensity 0.3
         shininess 0.95
         transparency 0.6
         diffuseColor 0.7 0.0 0.0
         emissiveColor 0.0 0.0 0.0
         specularColor 1.0 0.792 0.792
       }
     }
     geometry Box {
     }
   }
   DEF Ball Transform {
     children [
       Shape {
         appearance Appearance { material USE red_glass }
         geometry Sphere { radius 0.3 }
       }
     ]
   }
 ]
}

DEF BoxTimer TimeSensor { loop TRUE }

DEF BallScale Script {
 inputOnly SFTime time
 outputOnly SFVec3f newBallScale
 url ["random2.js"]
}
ROUTE BoxTimer.time TO BallScale.time
ROUTE BallScale.newBallScale TO Ball.scale

DEF BoxMover Script {
 inputOnly SFTime cycleTime
 outputOnly SFVec3f newBoxPosition
 url ["random2.js"]
}
ROUTE BoxTimer.cycleTime TO BoxMover.cycleTime
ROUTE BoxMover.newBoxPosition TO Box.translation

random.js file contents:
function time(){
 newBallScale=new SFVec3f(
6*Math.random(),6*Math.random(),12*Math.random()
);
}

function cycleTime(){
 newBoxPosition=new SFVec3f(
3*Math.random(),3*Math.random(),10*Math.random()
);
}

The first script animates the scale of the sphere and the
second script animates the translation of the box.

http://www.hypermultimedia.com/x3d/random/random2.x3dv

Thanks and Best Regards,
Joe

-------------------------------------------------------------------------
for list subscription/unsubscription,
go to http://www.web3d.org/cgi-bin/public_list_signup/lwgate/listsavail.html