# SFX Played Once per Collision or Hit

I have a question about using Box2D (engine for LibGDX used to make realistic physics). I observed on the code that I've made for the physics here below:

@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button)
{
// TODO Touch Up Event

if(is_Next_Fruit_Touched)
{

Fruit_BD.type = BodyType.DynamicBody;
Fruit_BD.position.set(x, y);

FixtureDef Fruit_FD = new FixtureDef(); // --> Allows you to make the object's physics.
Fruit_FD.density = 1.0f;
Fruit_FD.friction = 0.7f;
Fruit_FD.restitution = 0.2f;

MassData mass = new MassData();
mass.mass = 5f;

Fruit_Body[n] = world.createBody(Fruit_BD);
Fruit_Body[n].setActive(true); // --> Let your dragon fall.
Fruit_Body[n].setMassData(mass);
Fruit_Body[n].setGravityScale(1.0f);

System.out.println("Eggs... " + n);

is_Next_Fruit_Touched = false;
up = y;
Gdx.app.log("Initial Y-coordinate", "Y at " + up);

//Once it's touched, the next fruit will set to drag.
if(n < 50)
{
n++;

}else{

System.exit(0);

}
}

return true;


}

Now, I'm thinking which part o line should I implement for the sound effects. My objectives to make SFX played once for every collision (Or should I say "SFX played once per collision"?) on the following:

• SFX played once if they hit on the objects of its kind. (e.g. apple vs. apple)
• SFX played once on a different sound when it hit on the ground. (e.g. apple land on the mud)

Take note that I'm using Box2D for the Java programming version thanks to LibGDX via Box2D engine and I edited the physics body using Physics Body Editor before I implement it to code.

I tried to check every available methods for body, fixture definition, or body definition to code for the SFX when hit but it seems only for the gravity and weight. Is there possibly available on the document for SFX played when collision happens if possible?

I don't really see how all that physics code is relevant to playing SFX. (Although you do need collision detection to decide when to play sounds.)

The following design is all pseudocode

function onCollisionWith(otherThing)
if     type(otherThing) == "apple" then playSound("bonk!") end
elseif type(otherThing) == "mud"   then playSound("splat!") end
end


thisObjectCurrentlyBeingCheckedFor
.onCollisionWith(thatObjectItWasFoundToCollideWith)


This would produce a sound effect whenever the two objects are colliding.

Depending on how your physics handles the collision, you may want to introduce some state to describe what objects it was already colliding with last frame, so you don't re-play the sound.

• This code is good. However, I'm not using C++. I'm currently using Java programming. – David Dimalanta Dec 9 '12 at 8:18
• "Pseudocode" is an imaginary programming language. It's only illustrative. It needs to be translated to your programming language to actually do anything. In Java, for example, you might want to define a method in a class Fruit like public void onCollisionWith(Fruit otherFruit), containing code similar to the above. – Anko Dec 9 '12 at 10:48
• Translating is like a language, huh? It's more interpreter. – David Dimalanta Dec 10 '12 at 1:32
• I don't understand. – Anko Dec 10 '12 at 11:36
• That's right. C++, Lua, Java and JavaScript for example can all be described as "C-like", because all of their syntaxes are quite similar to C. The more of them you know, the easier it is to learn another. – Anko Dec 11 '12 at 10:48