Site icon TechHype.io

Physics Layout – A custom Compose layout with a physics engine

This library offsers a custom Jetpack Compose layout that is backed by some physics engine. Currently it is powered by and tightly coupled with dyn4j.

How to use Physics Layout

To get started (and to be honest, there is not much you can currently do after you got started) create a PhysicsLayout and fill it with Composables. Every root level Composable in PhysicsLayout must use the body modifier to tell the simulation how the Composable behaves in the physics world. The body modifier can be configured with following parameters:

PhysicsLayout takes a Simulation as a parameter. Use rememberSimulation to create a Simulation. rememberSimulation is used as the default argument for simulation.

Physics Layout example usage

PhysicsLayout {
    Card(
        modifier = Modifier.body(
            shape = CircleShape,
        ),
        shape = CircleShape,
    ) {
        Icon(
            modifier = Modifier
                .size(32.dp)
                .padding(4.dp),
            imageVector = Icons.Default.Star,
            contentDescription = "Star",
            tint = Color.White
        )
    }
}

This would add a ball with a star in the center of the layout, which then starts falling to the ground.

Note: The shape must be set on both the body modifier and the Card.

Change gravity

If you need to change the gravity of the simulated world, use Simulation.setGravity

Caveats, notes, missing features

Physics Layout on GitHub: https://github.com/KlassenKonstantin/ComposePhysicsLayout
Platform: Android
⭐️: 171
Exit mobile version