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?


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.

| improve this answer | |
  • 1
    \$\begingroup\$ This code is good. However, I'm not using C++. I'm currently using Java programming. \$\endgroup\$ – David Dimalanta Dec 9 '12 at 8:18
  • 1
    \$\begingroup\$ "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. \$\endgroup\$ – Anko Dec 9 '12 at 10:48
  • 1
    \$\begingroup\$ Translating is like a language, huh? It's more interpreter. \$\endgroup\$ – David Dimalanta Dec 10 '12 at 1:32
  • 1
    \$\begingroup\$ I don't understand. \$\endgroup\$ – Anko Dec 10 '12 at 11:36
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – Anko Dec 11 '12 at 10:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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