This library offsers a dyn4j wrapper for Jetpack Compose.
Before reaching version 1.0, this library is considered experimental, which means that there is no guaranteed backwards compatibility between versions. Signatures, interfaces, names, etc. may and will most likely change.
physics.mov
dependencies {
implementation 'io.github.klassenkonstantin:physics-layout:<version>'
}
To get 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 should behave in the physics world. The body
modifier can be configured with following parameters:
id
: The id the body should have in the simulation. Useful for operations that act directly on bodies (not yet supported).shape
: Describes the outer bounds of the body. Only RoundedCornerShapes are supported.isStatic
: Set true for unmovable bodies like walls and floors.initialTranslation
: Where this body should be placed in the layout. AnOffset
of (0,0) is the center of the layout, not top left.initialImpulse
: The impulse that should be applied to this body once it's placed into the worlddragConfig
: Set toDragConfig.Draggable
to enable drag support
PhysicsLayout
takes a Simulation
as a parameter. Use rememberSimulation
to create a Simulation. rememberSimulation
is used as the default argument for simulation
.
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 theCard
.
If you need to change the gravity of the simulated world, use Simulation.setGravity
- I don't think Compose was made to display hundrets of Composables at the same time. So maybe it's not a good idea to build a particle system out of this.
- In general, what is true for all of Compose is especially true for this Layout: Release builds perform way better than debug builds.
- State is not restored on config changes 😱.
- Currently there is no way to observe bodies / collosions / etc.