Game Development Community

Triggered sounds

by arteria3d · in Torque Game Engine · 12/29/2004 (3:56 am) · 26 replies

Digging futher into TGE to learn more, i wish to add a simple rustling sound, when my character walks through a bush object.

Therefore i can use the on:collision command. Do i simply make a new class, say 'Bush' and attach this classname to the DTS objects in question. Once this is done, i can then determine whether the player has collided with that class of object, and thus play the resulting sound???

About the author

Owner of uk based Ltd company ArteriaMediaLtd. with a trading name of Arteria3d Website;arteria3d.com

Page «Previous 1 2
#1
12/29/2004 (5:29 am)
Look in the demo folder of the Torque install

flag.cs




datablock StaticShapeData(FlagPole)
{
category = "Demo";
shapeFile = "~/data/shapes/objects/flagpole.dts";
};

datablock ShapeBaseImageData(FlagPoleImage)
{
shapeFile = "~/data/shapes/objects/flagpole.dts";
mountPoint = 1;
offset = "0 0 -1";
emap = false;
};

thats how you create the object... at least a static shape, which should suffice for a bush

then for collision, should look something like this

function FlagPole::onCollision( %this, %obj, %col )
{

//emit sound code here

}
#2
12/29/2004 (5:32 am)
Does the item need a collision box to make the collision???? because foilage i dont want to have collision boxes - so the player can walk through? Does it simply test that the player has colided with the object?
#3
12/29/2004 (9:47 am)
I have had my player walk through an object and had its onCollision function called. Collision in torque does not necessarily mean you bounce off.
#4
12/29/2004 (12:10 pm)
If your using the foliage replicator you might want to go about this differently... I'd use trigger(s)
#5
12/29/2004 (12:14 pm)
I havent got to the foilage reps yet. At the moment i just want to trigger bushes and foilage i am placing manually

This is my code:



datablock staticshapedata(bush)
{
category = "Misc";
shapefile = "~/data/shapes/plants/yukaA.dts";
preload=true;

};



function StaticShapeData::create(%block)
{
%obj = new StaticShape()
{
dataBlock = %block;
rotate = true;
};
return(%obj);
}




function bush::onCollision(%this,%obj,%col)
{
echo("hit");
}




When i go over the object i expect - the console to echo 'hit' but it doesnt. Do we definatally not have to use collision boxes?

Also when i finally get it working - if the player stays on the bush that will trigger the sound - will the soun trigger every time the player moves, or will it just keep triggering the sound repeatedly until the player moves off it?

Thanks for your help

Steve
#6
12/29/2004 (12:31 pm)
Okay i got the code working. Also realised i dont need a collision box too, to trigger the event.

Onthe subject of the sound repeating - hmmm.... even when not looped this still occures. Looking at the console - whilst the player is on the item, the collision is being continually tested - not just when he moves about on the item - so the sound keeps triggering. I need the trigger to work on the bush, when the player actually moves, rather than being stationary. Is there a way of linking the On:collision, and determining if the player is moving? an therefore only triggers the sound when moving, through the foilage rather than just staying still?
#7
12/29/2004 (1:24 pm)
Hmm... guess the easiest way to do this in script would be to modify the onCollision to check for player movement... player velocity... without looking into it not sure which value, but should be just a simple if statement checking a value

edit: if it keeps re-initiating itself might need to throw in a toggly set of case/ if statements also
#8
12/29/2004 (2:11 pm)
I'm a TorqueScript newbie myself, but you would want to get the magnitude of the player's velocity and use that with some magic numbers to adjust playback and volume of your sound. %obj is the onCollision should be the player or whatever collided. Some code could be:

function bush::onCollision(%this,%obj,%col)
{
    // speed will be very small or zero if the 
    // colliding object is not moving
    %speed = VectorLen( %obj.getVelocity() );

    // play your sound here... 

}

Not sure how sound looping works, but you should take care that your not re-playing the sound too qucikly after multiple onCollision calls.
#9
12/29/2004 (2:17 pm)
Also using %val to determine whether a movement key is being pressed and if its not - then within the on collision function - stop the sound playing if no movement key is pressed.

Now... i am having trouble finding a good thread on triggered sounds...
I looked in player.cs for some examples, and came up with the following script but it keeps saying looking for function...transform.....Is this not the way a sound would be played?


datablock ItemData( bush )
{

category = "Misc";
shapefile = "~/data/shapes/plants/yukaA.dts";
};


function ItemData::create( %data )
{

%obj = new Item()
{
dataBlock = %data;
//rotate = true;
static = true;
};

return %obj;
}


datablock AudioProfile(bush)
{
fileName = "~/data/sound/dust.wav";
description = AudioClose3d;
preload = true;
};








function bush::onCollision( %this, %obj, %col )
{
echo( "Collision" );





serverplay3d(bush,%this.getTransform());








}
#10
12/29/2004 (4:36 pm)
I think theres a better command to play sounds... cant remember it off the top of my head... axlplay... or something along those lines... search for playing sounds again...

I thought about the keycheck, though that could be bad in a server to client situation, better to check the velocity magnitude and keep the scripts working with eachother that way.
#11
12/29/2004 (4:48 pm)
Using the velocity also has a benefit as it should work for *any* ShapeBase derived object. This means a barrel rolling down a hill can trigger brush sounds just as well as your player running around.

I haven't looked at the sound system (or lack of it in the TSE case), but i assume sounds playing outside of range of the listener are lightweight and culled.
#12
12/29/2004 (5:26 pm)
Yeah, only overhead should be on the server, if you have hundreds of thousands of brush sounding bushes and hundreds of thousands of barrels then it might be a concern, other than that it should be fine.
#13
01/03/2005 (2:06 pm)
I have just written the folloowing, which does echo that a collision has been made - but the actual sound isnt played??? Am i not using the sound command correctly


datablock ItemData( SuperBomb )
{

category = "Misc";
shapefile = "~/data/shapes/plants/yukaA.dts";
};


function ItemData::create( %data )
{

%obj = new Item()
{
dataBlock = %data;
//rotate = true;
static = true;
};

return %obj;
}


datablock AudioProfile(bush)
{
fileName = "~/data/sound/dust.wav";
description = AudioClose3d;
preload = true;
};
#14
01/03/2005 (9:48 pm)
Try Something like this

datablock AudioProfile(sound1)
{
filename = "~/data/sound/crossbow_reload.ogg";
description = AudioClose3d;
preload = true;
};


datablock staticShapeData(bush)
{
   category = "misc";
   shapeFile = "~/data/shapes/campfires/campfire.dts";
};


function bush::onCollision(%this,%obj,%col)
{
    if (%col.getType() & $TypeMasks::PlayerObjectType)
      {
         // %obj.playAudio(0,sound1);
         // or
         serverplay3D(sound1,%obj.getTransform());
         echo("playsound sound1 ");
      }

}

A Campfire with reload sound :)
#15
01/05/2005 (9:45 am)
Datablock ItemData( SuperBomb )
{

category = "Misc";
shapefile = "~/data/shapes/plants/yukaA.dts";
};


function ItemData::create( %data )
{

%obj = new Item()
{
dataBlock = %data;
//rotate = true;
static = true;
};

return %obj;
}


datablock AudioProfile(bush)
{
fileName = "~/data/sound/environment sounds/amb.wav";
description = AudioClose3d;
preload = true;
};








function SuperBomb::onCollision( %this, %obj, %col )
{
if (%col.getType() & $TypeMasks::PlayerObjectType)
{
echo( "Collision" );
//serverplay3D(bush,%obj.getTransform());
%obj.playAudio(0,bush);
echo("playsound sound1 ");
}



}

I did the above which is just like your code, but just altererd it slightly - and the 'collision' echos to the console but no sound
#16
01/05/2005 (9:56 am)
What about using material mapping?
I added a few test sounds for some of my .dif objects (sidewalk textures mainly), and without any other code, they worked fine. So wouldn't a bush texture work as well?
#17
01/05/2005 (11:11 am)
Hi Eric,

Can you post an example please

Thanks

Steve
#18
01/05/2005 (10:40 pm)
@Erik
How do you trigger the materialmapping and get the material from a dif object or a dts shape ?
I know about the terrrain materialmapping .
Base materialstep sound on interiors is a predefiened value in player.cc.
#19
01/06/2005 (1:36 am)
Okay, I guess I did use some extra code to do that :)
player.cc, add an additional check after this one:
// Only put foot puffs and prints on the terrain
if( rInfo.object->getTypeMask() & TerrainObjectType)
{
make it for interior object types. Copy the same material map code, just cut out all the footpuff stuff.
Finally, add a couple new sound types to playFootstepSound(), as yes they are hardcoded.

Edit: I just remembered you were originally asking about a dts object, not an interior :) It _should_ still work, but I can't say for sure. I do have another conditional sound for vehicle objects playing the metal sound, but I don't call material mapping for that, as pretty much all vehicles are metal.
#20
01/06/2005 (11:21 am)
Erik - does this work the same as for terrain textures - where it compares a texture on the interior object and plays a suitable sound?? If so can u post how code wise i key the sounds to the actual intereior texture?

Steve
Page «Previous 1 2