unity 物理台球_使用基于Android物理的动画制作类似于桌球的游戏

本文介绍如何使用Unity游戏引擎,结合Android物理引擎,创建一款类似斯诺克的台球游戏。通过物理模拟实现真实感的球体碰撞效果。
摘要由CSDN通过智能技术生成

unity 物理台球

学习Android开发 (Learning Android Development)

While playing around with Android Physics-based Animation API, I just created a simple App (just one Activity class— not good programming practice, but to state how simple it is) with about 200 lines of codes, and have the snooker like game.

W¯¯往往微不足道玩弄基于物理学的Android动画API ,我就创建了一个简单的应用程序(只需一个活动讲座不是良好的编程习惯,但是国家是多么简单),与约200行代码,并且有一个类似的游戏斯诺克。

Image for post

基于Android物理的动画 (Android Physics-Based Animation)

Android Physics-Based Animation consists of these two animations, i.e. FlingAnimation and SpringAnimation, which is a subclass of DynamicAnimation.

基于Android物理的动画由这两个动画组成,即FlingAnimationSpringAnimation ,它们是DynamicAnimation的子类。

It is not provided by default on Android. You’ll need to get the dependencies through build.gradle by adding

Android上默认不提供此功能。 您需要通过添加以下内容来通过build.gradle获取依赖build.gradle

implementation "androidx.dynamicanimation:dynamicanimation:1.0.0"

Unlike normal animation, Fling and Spring animation behave according to the velocity provided to them. Hence the behavior is dynamic in correspond to its start velocity.

与普通动画不同,Fling和Spring动画根据提供给它们的速度进行动作。 因此,行为与其初始速度相对应是动态的。

I use both of them and tied them together to make this simple snooker-like game. Check out below for a little more details of how it is implemented.

我将它们都使用,并将它们绑在一起,以制作这款简单的斯诺克式游戏。 请在下面查看有关其实施方式的更多详细信息。

手势检测 (Gesture Detection)

The only touch control I add on is the gestureDetector

我添加的唯一触摸控件是gestureDetector

img_ball.setOnTouchListener { view, motionEvent ->
gestureDetector.onTouchEvent(motionEvent)
true}

Upon detecting the Fling gesture, we could then get both the X and Y velocity. Using those velocities, we’ll start the FlingAnimations

一旦检测到Fling手势,我们就可以同时获得X和Y速度。 使用这些速度,我们将启动FlingAnimations

private val gestureListener = object : GestureDetector.SimpleOnGestureListener() {
       
override fun onDown(e: MotionEvent?): Boolean {
stopAnimation = false
return true
}
override fun onFling(
e1: MotionEvent?,
e2: MotionEvent?,
velocityX: Float,
velocityY: Float
): Boolean {
if (isAnimationRunning()) return false
flingAnimationX.setStartVelocity(velocityX).start()
flingAnimationY.setStartVelocity(velocityY).start()
return true
}
}

动态动画 (Fling Dynamic Animation)

This is the most important behavior of the game. One could just click and swipe the ball towards the direction it should head and the velocity of the fling based on the gesture of the user.

这是游戏中最重要的行为。 只需单击并朝着用户应前进的方向滑动球,然后根据用户的手势挥动速度即可。

Unlike any other animation, the developer doesn’t define the target final destination, but instead, it is determined by the velocity provide

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值