Example usage for javax.media.j3d PointSound setPosition

List of usage examples for javax.media.j3d PointSound setPosition

Introduction

In this page you can find the example usage for javax.media.j3d PointSound setPosition.

Prototype

public void setPosition(Point3f position) 

Source Link

Document

Sets this sound's location from the vector provided.

Usage

From source file:SimpleSounds.java

/**
 * Add a sound to the transform group. This takes a point sound object and
 * loads into it a sounds from a given file. The edge of the sound's extent
 * is also defined in a parameter.//from   w  w w . ja v a2 s .c o  m
 * 
 * @param tg
 *            TransformGroup that the sound is to be added to
 * @param sound
 *            PointSound to be used
 * @param soundFile
 *            String that is the name of the sound file to be loaded
 * @param edge
 *            float that represents the sound's maximum extent
 */
protected void addObjectSound(TransformGroup tg, PointSound sound, String soundFile, float edge) {
    //First we get the current transform so that we can
    //position the sound in the same place
    Transform3D objXfm = new Transform3D();
    Vector3f objPosition = new Vector3f();
    tg.getTransform(objXfm);
    objXfm.get(objPosition);
    //Create the media container to load the sound
    MediaContainer soundContainer = new MediaContainer(soundFile);
    //Use the loaded data in the sound
    sound.setSoundData(soundContainer);
    sound.setInitialGain(1.0f);
    //Set the position to that of the given transform
    sound.setPosition(new Point3f(objPosition));
    //Allow use to switch the sound on and off
    sound.setCapability(PointSound.ALLOW_ENABLE_READ);
    sound.setCapability(PointSound.ALLOW_ENABLE_WRITE);
    sound.setSchedulingBounds(bounds);
    //Set it off to start with
    sound.setEnable(false);
    //Set it to loop forever
    sound.setLoop(BackgroundSound.INFINITE_LOOPS);
    //Use the edge value to set to extent of the sound
    Point2f[] attenuation = { new Point2f(0.0f, 1.0f), new Point2f(edge, 0.1f) };
    sound.setDistanceGain(attenuation);
    //Add the sound to the transform group
    tg.addChild(sound);
}