Skip to content

Instantly share code, notes, and snippets.

@dilrajsingh1997
Last active June 14, 2022 18:10
Show Gist options
  • Save dilrajsingh1997/0aa0e7f610482c5fab6b04435a5928b7 to your computer and use it in GitHub Desktop.
Save dilrajsingh1997/0aa0e7f610482c5fab6b04435a5928b7 to your computer and use it in GitHub Desktop.
const val G = -9.8f
// we are sending one item to the producerChannel at a time
val xVelocity = Random.nextInt(-50, 50)
val yVelocity = 100 // Random.nextInt(0, 200)
return ItemStateBuilder(
composeCanvasDrawItem = getTextCanvasObject("\uD83D\uDD25"),
initialX = width / 2,
initialY = (height - 500f),
)
.animateX {
object : Animation<Float, AnimationVector1D> {
override val durationNanos: Long
get() = Long.MAX_VALUE
override val isInfinite: Boolean
get() = true
override val targetValue: Float
get() = 0f
override val typeConverter: TwoWayConverter<Float, AnimationVector1D>
get() = Float.VectorConverter
override fun getValueFromNanos(playTimeNanos: Long): Float {
return initialX + (xVelocity * playTimeNanos / 100_000_000).toFloat()
}
override fun getVelocityVectorFromNanos(playTimeNanos: Long): AnimationVector1D {
return Float.VectorConverter.convertToVector(targetValue)
}
}
}
.animateY {
object : Animation<Float, AnimationVector1D> {
override val durationNanos: Long
get() = Long.MAX_VALUE
override val isInfinite: Boolean
get() = true
override val targetValue: Float
get() = 0f
override val typeConverter: TwoWayConverter<Float, AnimationVector1D>
get() = Float.VectorConverter
override fun getValueFromNanos(playTimeNanos: Long): Float {
val time = playTimeNanos / 100_000_000f
return initialY - ((yVelocity * time) + (0.5 * G * (time * time))).toFloat()
}
override fun getVelocityVectorFromNanos(playTimeNanos: Long): AnimationVector1D {
return Float.VectorConverter.convertToVector(targetValue)
}
}
}
.terminalCondition { _, interpolatedY, _, _, _, _, _ ->
interpolatedY > height
}
.build()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment