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?
Answer
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.
No comments:
Post a Comment