2
\$\begingroup\$

I used the collision physics (i.e. Box2d, Physics Body Editor) and implemented onto the java code. I'm trying to make the fall speed higher according to the examples:

  • It falls slower if light object (i.e. feather).
  • It falls faster depending on the object (i.e. pebble, rock, car).

I decided to double its falling speed for more excitement. I tried adding the mass but the speed of falling is constant instead of gaining more speed. check my code that something I put under input processor's touchUp() return method under same roof of the class that implements InputProcessor and Screen:

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

    if(is_Next_Fruit_Touched)
    {
        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.
        Fruit_Body[n].setMassData(mass);
        Fruit_Body[n].setGravityScale(1.0f);

        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)
        {
            n++;

        }else{

            System.exit(0);

        }
    }

    return true;
}

And take note, at show() method , the view size from the camera is at 720x1280:

    camera_1 = new OrthographicCamera();
    camera_1.viewportHeight = 1280;
    camera_1.viewportWidth = 720;
    camera_1.position.set(camera_1.viewportWidth * 0.5f, camera_1.viewportHeight * 0.5f, 0f);
    camera_1.update();

I know it's a good idea to add weight to make the falling object falls faster once I released the finger from the touchUp() after I picked the object from the upper right of the screen but the speed remains either constant or slow. How can I solve this? Can you help?

\$\endgroup\$

5 Answers 5

12
\$\begingroup\$

It's basic physics: heavier objects don't fall faster!

A feather has a bigger area than a pebble; hence it gets slowed down by air resistance a lot more. Introduce a drag force that slows down objects the faster they fall, in the opposite direction of the velocity. The drag force will cancel out gravity once terminal velocity has been reached.

drag_force = object_specific_constant * velocity^2

The object specific constant is a combination of a lot of factors, including object size, fluid density and viscosity. The most important factor is size though! A feather is big with a lot of surface area; a pebble of the same mass is a lot smaller. The force gravity inflicts is the same (since they are the same mass) but the drag on the feather is a lot higher, so the feather slows down and reaches terminal velocity much sooner.

Pick a value that makes it behave like you want. I'd suggest something based on the object area (width * height * magic_value_that_makes_it_behave_like_I_want) for simplicity.

Box2d has something they call linear damping which works similar to this.

\$\endgroup\$
7
  • 2
    \$\begingroup\$ I believe this is the most correct answer. Use different drag co-efficients for the different objects to achieve different fall rates. Most physics engines support this e.g Box2D. Although I suggest you revise your answer to include what Majd has stated that Mass does not typically affect fall speed. \$\endgroup\$ Nov 26, 2012 at 10:36
  • \$\begingroup\$ Let's see what I can do for your answer. \$\endgroup\$ Nov 26, 2012 at 13:10
  • \$\begingroup\$ Although everything about this answer is correct, adding a drag force would be the incorrect way to handle the problem. Box2D already has built-in functionality for this. Not to mention, the equation you provide is pretty sketch. \$\endgroup\$
    – Amplify91
    Nov 26, 2012 at 17:01
  • \$\begingroup\$ @Amplify91 What's sketch about it? It's the actual formula for air resistance. \$\endgroup\$ Nov 26, 2012 at 18:03
  • 2
    \$\begingroup\$ I can see how it is confusing, I didn't even understand the name at first. It's just a constant that is specific to that object. In a real world scenario it would primarily be (and simplified to) the surface area being acted upon by the wind resistance. For programming it would just be an arbitrary number that would cause the object to behave as intended. Ccxvii should probably put this as an explanation in the answer. \$\endgroup\$ Nov 26, 2012 at 20:46
3
\$\begingroup\$

As ccxvii said, heavier objects do not fall faster. The only reason lighter objects sometimes fall more slowly is because the have higher air resistance. In a vacuum, all objects fall at the same speed. LibGdx uses Box2D for physics and Box2D is pretty realistic so changing mass will not affect how fast an object falls. However, there are two ways around this to achieve what you are looking for:

  1. Simulate air resistance on your objects (more for lighter objects like feathers). Do this with the following code: yourBody.setLinearDamping(someFloatValue);
    Don't be afraid to set that value well above 1.

  2. Change the gravitation scale for each object. This is not a physically accurate option, but it may be just what you are looking for. In my platformer, I set my main character's gravitational scale to 1.4f so that he doesn't float as much while jumping and is easier to maneuver at high speed. Use this method: yourBody.setGravityScale(someFloatValue);
    Keep this value around 1 to avoid too much of the effect.

Set either of these values once when you are creating you objects.

\$\endgroup\$
4
  • \$\begingroup\$ Also, make sure your world's gravity is high enough to make your objects fall as fast as you want them to. Real world gravity is -9.8, but you may want to go higher. \$\endgroup\$
    – Amplify91
    Nov 26, 2012 at 17:07
  • 1
    \$\begingroup\$ About number 1, setLinearDampling(), is it possible to set a negative number, over -9000, for even heavier? \$\endgroup\$ Nov 27, 2012 at 2:00
  • \$\begingroup\$ @DavidDimalanta Can't tell if troll... I've set it around 10 before. \$\endgroup\$
    – Amplify91
    Nov 27, 2012 at 2:12
  • \$\begingroup\$ Have you played Jewel Stacker for the Android? Two the same square gems and I tried this game. I drag the first square gem at the top and the second one on the center. Then, I run on both at tremendous speed but the first one falls faster than the second one; even if I set the first one on top and the second on the middle, the first square catches up the second while falling like a race. This is the concept I was trying to look for for defying gravity. It maybe possible at @Majd's answer, so were you, but on the gravity concept. \$\endgroup\$ Nov 27, 2012 at 2:17
1
\$\begingroup\$

Changing the mass will only have an effect on how the body's inertia (F=m*a), how it reacts to forces. Have you heard of Galileos experiment at the leaning tower of Pisa? If you want them to fall att different speeds you have to either:

  1. Set the velocity of the body after creation

    Body.setLinearVelocity(Vector2 v)

  2. Apply a force to the body after creation Body.

    Body.applyForce(Vector2 force, Vector2 point)

  3. Apply an impulse to the body after creation

    applyLinearImpulse(Vector2 impulse, Vector2 point)

\$\endgroup\$
2
  • 1
    \$\begingroup\$ I agree with what you say about inertia, but this is not how it should be handled in Box2d. Firstly, if you want any kind of realism in your physics, NEVER manually set the velocity as you suggest in point 1. Applying vertical forces and impulses may get you closer to what you are trying to achieve (especially forces) but again, are the incorrect way of handling this and will not give the effect you are looking for. \$\endgroup\$
    – Amplify91
    Nov 26, 2012 at 17:04
  • 1
    \$\begingroup\$ I can use Majd's idea for the next project later on. But for now, my current project, a stack-up game for the Android, needs to "cheat" the speed fall since this game tests the reflexes and puzzle solving skills. Best of all, kid friendly. \$\endgroup\$ Nov 27, 2012 at 1:38
1
\$\begingroup\$

Seems like your Box2d world is using 1 px = 1 meter. Imagine that you are playing with huge objects with tons weight in real world - that is what happening in your game. Make it ~50 (+/-20) px = 1 meter and play with it.

Wish you luck.

\$\endgroup\$
12
  • 1
    \$\begingroup\$ Where can I find it to adjust the pixels per meter thing? \$\endgroup\$ Nov 27, 2012 at 7:37
  • \$\begingroup\$ When you creating new box2d object you just need to devide all the parameters by your devider, for example 30. I mean the following: \$\endgroup\$ Nov 28, 2012 at 6:18
  • 1
    \$\begingroup\$ No. You can play with bodys mass, density, gravity, but what I said is the most common bug. \$\endgroup\$ Nov 28, 2012 at 11:31
  • 1
    \$\begingroup\$ yeap, he is right. quote: "you should convert the values received from Box2D from meters to pixels. This will improve the stability of the physics simulation." link: box2d.org/2011/12/pixels \$\endgroup\$ Nov 28, 2012 at 13:16
  • 1
    \$\begingroup\$ =) good luck!!! \$\endgroup\$ Dec 3, 2012 at 10:48
-2
\$\begingroup\$

Just increase the gravity acceleration, there is a basic formula:

V = 0.5 * g * t^2

g -> gravity acceleration t -> time in the air

\$\endgroup\$
1
  • 3
    \$\begingroup\$ I don't think this adequately answers the question, considering there is another already established and accepted answer. \$\endgroup\$ Oct 16, 2013 at 16:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .