NTNUJAVA Virtual Physics Laboratory
Enjoy the fun of physics with simulations!
Backup site http://enjoy.phy.ntnu.edu.tw/ntnujava/

Easy Java Simulations (2001- ) => Questions related to EJS => Topic started by: lookang on December 19, 2008, 02:58:48 pm



Title: is it possible to make applet make a sound when i condition is met?
Post by: lookang on December 19, 2008, 02:58:48 pm
i can't find a button to embed sound clips *.mp3

is it the same as a video file? :D



i found it as a checkbox enabled sound, exploring it now,

hmmmm, doesn't seem to do what i need.

I want the add a line to the event
action

...................*.mp3;

so that everytime the event collosion occurs it will play this *.mp3 file.

how to do it?

I check here and EJS both cannot find the info easily.

thanks!


Title: Re: is it possible to make applet make a sound when i condition is met?
Post by: Fu-Kwun Hwang on December 21, 2008, 12:43:57 am
The current version of EJS only support wav file.
You can add other java library which support mp3 if you want to use mp3 in EJS.
For examples: Jlayer-MP3 library at http://www.javazoom.net/javalayer/javalayer.html


Title: Re: is it possible to make applet make a sound when i condition is met?
Post by: lookang on December 21, 2008, 11:33:17 am
i made sound.wav from the mp3 file.

i named sound.wav as a variable

called sound

i try this syntax to make the sound = true ,

but the collision detection syntax i do understand enough.

i tried this on the constraint page but it didnt work
if ( min < TOLERANCE ) { //  1. am i correct to test for the condition min <  TOLERANCE ?, i think min is a variable inside the collision syntax which i can refer to rite?


sound = true; // testing sound on
               
                        }
     else {
     sound = false; //testing sound off
          }




Title: Re: is it possible to make applet make a sound when i condition is met?
Post by: Fu-Kwun Hwang on December 21, 2008, 11:52:37 am
Paco created a SeeSaw.xml to demo how to use wav file in EJS (available for version 3.47).
Please check out the attached image file (illustrate how to set up properties for sound in EJS).

The sound is on/off when value for playSound variable is true/false.


Title: Re: is it possible to make applet make a sound when i condition is met?
Post by: lookang on December 21, 2008, 09:56:51 pm
i will analyze this asap.

after looking at the source,
i didn't think putting in the initialization page was "sound = false" but i will try later.

was trying to force the codes to do "sound = false"

will test out again asap
thanks!


Title: Re: is it possible to make applet make a sound when i condition is met?
Post by: lookang on December 21, 2008, 10:49:55 pm
i got sound using the action. but i cannot off it.

how to make it play once only?

the codes is this :
ZERO CONDITION:
Code:
for (int i=0; i  x[i] = xTop[i] + l*Math.sin(alpha[i]);
  y[i] = yTop - l*Math.cos(alpha[i]);
 }
 
 // codes for calculation of zero condition based on length
double min = TOLERANCE;
for (int i=0; i  for (int j=i+1; j    double deltax = x[j]-x[i], deltay = y[j]-y[i];
    double allowed = 2*radius;
    double distance = deltax*deltax+deltay*deltay-allowed*allowed;
    if (distance      if (deltax*(omega[j]-omega[i])<0) {
 // take action to take if loop checks for delatx*difference of angular velocities less than zero
                 ball1 = i; ball2 = j; min = distance;
//                sound = true; // testing sound on
               
//      }
//      else { sound = false; //testing sound off
    }
     
     
    }
  }
}
return min;

and i added the acion
ACTION
Code:
double aux = omega[ball1]; // assign aux to omega[ball1]
omega[ball1] = omega[ball2]; // assign omega[ball1] = omega[ball2], because mass same so this will work
omega[ball2] = aux;
sound = true; // sound produced added by weelk but it is non stop why?


Title: Re: is it possible to make applet make a sound when i condition is met?
Post by: lookang on December 21, 2008, 11:14:32 pm
i think the approach is wrong here.

as the codes are constantly testing ball1 size against ball2 size so even when there is no collision, the condition is constantly achieved.

i think i added the sound there so it will not give me the result of a sound when a collision occur but also during balls rubbing each other at the bottom.


should i use another approach like test of momentum transfer?



or maybe  ball1 size against ball2 size && v1 or v2 > 0 ?

it is harder than i thought.


Title: Re: is it possible to make applet make a sound when i condition is met?
Post by: Fu-Kwun Hwang on December 21, 2008, 11:49:02 pm
If you set the sound to true, it will play the sound continuously.
You need to set the sound to off if you do not want to play it.
There are many way to do it.
For example: if you want the sound to play for 0.5 second.
Define a varrable t_on to remember when the time is on.
add t_on=t; just after you set sound=true;
And add the following code to constraint page:
If (Math.abs(t-t_on)>0.5)sound=false; // turn the sound off when it has been play for 0.5s.

You need to tell the program/computer what you want to do.
The program/computer can not decide it for you!


Title: Re: is it possible to make applet make a sound when i condition is met?
Post by: lookang on December 22, 2008, 12:10:01 pm
excellent advise!

variable added:
sound // boolean  true for play false for no play
t_on // time of sound to be play

Constraints:
if (Math.abs(t-t_on)>0.04) sound=false; // turn the sound off when it has been play for 0.04s

Evolution
Event
Action page added
sound = true; // sound produced
t_on = t;

work like a charm :)
i will post my derived newton cradle from prof paco in the community board as a download-able jar file
http://www.phy.ntnu.edu.tw/ntnujava/index.php?action=dlattach;topic=824.0;attach=499

you are the best, prof hwang