Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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:

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

             BodyEditorLoader Fruit_Loader = new BodyEditorLoader(Gdx.files.internal("Shape_Physics/Fruity Physics.json"));

             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.

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

             Fruit_Loader.attachFixture(Fruit_Body[n], Body, Fruit_FD, Fruit_IMG.getWidth());
             Fruit_Origin = Fruit_Loader.getOrigin(Body, Fruit_IMG.getWidth()).cpy();

             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)




         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?

share|improve this question

1 Answer 1

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

In your Apple class body...

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

In your collision detection code...


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.

share|improve this answer
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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.