基于SceneForm實(shí)現(xiàn)子彈射擊(繪制子彈運(yùn)行軌跡)
基于 SceneForm 實(shí)現(xiàn)的子彈射擊(繪制子彈運(yùn)行軌跡)
Sceneform 框架很強(qiáng)大,不了解 Sceneform 的時(shí)候,覺(jué)得要想做 3D 場(chǎng)景需要會(huì) OpenGL,而 OpenGL 的學(xué)習(xí)曲線很陡;接觸到這個(gè)框架之后覺(jué)得小白也可以很快上手,甚至可以實(shí)現(xiàn)第一人稱射擊的效果
注:自己學(xué)習(xí) SceneForm 有一段時(shí)間了,不過(guò)沒(méi)有發(fā)現(xiàn)模擬重力場(chǎng)的接口,不知道是不是自己漏掉了
模擬射擊效果的思路其實(shí)很簡(jiǎn)單
1、加載一個(gè)子彈模型
2、規(guī)劃子彈由近及遠(yuǎn)的軌跡
3、繪制子彈的運(yùn)行軌跡
子彈運(yùn)行軌跡的邏輯代碼;代碼中涉及的 CleanArFragment 在之前的《ARCore 的 SceneForm 框架在沒(méi)有 Plane 情況下的繪制 3D 模型》已經(jīng)給出;另外需要自行提供一個(gè)紋理圖片,即代碼中的 R.drawable.texture。
class MainActivity : AppCompatActivity() {
var arFragment : CleanArFragment? = null
var camera : Camera? = null
var size = Point(); //屏幕尺寸,控制子彈發(fā)射的初始位置
var bullet : ModelRenderable? = null
var scene : Scene? = null
val SHOT = 0x1101 //繪制過(guò)程軌跡信號(hào)
val SHOT_OVER = 0x1102 //清除子彈模型信號(hào)
var handler = object : Handler() {
override fun handleMessage(msg : Message)
{
if (msg.what == SHOT) { //繪制移動(dòng)過(guò)程中的軌跡
var currentStatus = msg.obj as CurrentStatus
currentStatus.node.worldPosition = currentStatus.status
} else if (msg.what == SHOT_OVER) { //一次射擊完成,清除屏幕的子彈
var node = msg.obj as Node
scene!!.removeChild(node)
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// 獲取屏幕尺寸
val display = windowManager.defaultDisplay
display.getRealSize(size)
arFragment = this.supportFragmentManager.findFragmentById(R.id.arFragment) as CleanArFragment
arFragment!!.arSceneView.planeRenderer.isEnabled = false //禁止 sceneform 框架的平面繪制
scene = arFragment!!.arSceneView.scene
camera = scene!!.camera
initbullet()
shootButton.setOnClickListener(listener)
}
var listener : View.OnClickListener = object : View.OnClickListener{
override fun onClick(v: View?) {
shoot()
}
}
@TargetApi(Build.VERSION_CODES.N)
//初始化子彈模型
private fun initbullet() {
Texture.builder().setSource(this@MainActivity, R.drawable.texture).build()
.thenAccept(
{ texture ->
MaterialFactory.makeOpaqueWithTexture(this@MainActivity, texture)
.thenAccept { material ->
// 設(shè)置子彈模型為球體
bullet = ShapeFactory.makeSphere(0.1f, Vector3(0f, 0f, 0f), material) }
}
)
}
private fun shoot() {
//從屏幕發(fā)出的射線,對(duì)應(yīng)子彈的運(yùn)行軌跡
var ray = camera!!.screenPointToRay(size.x / 2f, size.y / 2f);
var node = Node() //子彈節(jié)點(diǎn)
node.renderable = bullet //子彈節(jié)點(diǎn)加載子彈模型
scene!!.addChild(node)
Thread(object : Runnable{
override fun run() {
//子彈射擊過(guò)程中的軌跡,子線程處理軌跡事件,主線程改變軌跡位置
for (i in 1 .. 200 ) { //子彈射程 20 m
var stepLen = i;
var currentPoint = ray.getPoint(stepLen * 0.1f)
var msg = handler.obtainMessage()
msg.what = SHOT
msg.obj = CurrentStatus(node, currentPoint)
handler.sendMessage(msg)
}
//子彈超出距離后,從屏幕清除掉
var msg = handler.obtainMessage()
msg.what = SHOT_OVER
msg.obj = node
handler.sendMessage(msg)
}
}).start()
}
// 子線程和主線程穿點(diǎn)的數(shù)據(jù)類
data class CurrentStatus(var node : Node, var status : Vector3)
}
界面布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <fragment android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/arFragment" android:name="com.hosh.shootapplication.CleanArFragment"/> <View android:layout_width="35dp" android:layout_height="2dp" android:background="#ff0000" android:layout_centerInParent="true" /> <View android:layout_width="2dp" android:layout_height="35dp" android:background="#ff0000" android:layout_centerInParent="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/shootButton" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="8dp" android:text="@string/shoot" /> </RelativeLayout>
實(shí)現(xiàn)效果如下,因?yàn)閯?dòng)圖的偏差,子彈不是很清晰,子彈由中心的紅色十字向遠(yuǎn)處射擊

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android實(shí)時(shí)文件夾創(chuàng)建方法
這篇文章主要介紹了Android實(shí)時(shí)文件夾創(chuàng)建方法,涉及基于Activity實(shí)現(xiàn)文件實(shí)時(shí)查詢的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09
Android快速實(shí)現(xiàn)斷點(diǎn)續(xù)傳的方法
這篇文章主要為大家詳細(xì)介紹了Android快速實(shí)現(xiàn)斷點(diǎn)續(xù)傳的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
Android使用android-wheel實(shí)現(xiàn)省市縣三級(jí)聯(lián)動(dòng)
這篇文章主要為大家詳細(xì)介紹了Android使用android-wheel實(shí)現(xiàn)省市縣三級(jí)聯(lián)動(dòng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08
Android編程之控件狀態(tài)配置文件實(shí)例
這篇文章主要介紹了Android編程之控件狀態(tài)配置文件,以實(shí)例形式分析了Android控件狀態(tài)配置文件對(duì)于選中、獲得焦點(diǎn)、按下時(shí)的狀態(tài)等相關(guān)設(shè)置技巧,需要的朋友可以參考下2016-01-01
Ubuntu中為Android系統(tǒng)實(shí)現(xiàn)內(nèi)置Java應(yīng)用程序測(cè)試Application Frameworks層的硬件服務(wù)
本文主要介紹Ubuntu中為Android系統(tǒng)內(nèi)置應(yīng)用訪問(wèn)Application Frameworks層的硬件服務(wù),這里提供了詳細(xì)的流程和代碼實(shí)例,有興趣的朋友可以參考下2016-08-08
Android中實(shí)現(xiàn)Webview頂部帶進(jìn)度條的方法
這篇文章主要介紹了Android中實(shí)現(xiàn)Webview頂部帶進(jìn)度條的方法,當(dāng)前很流行的一個(gè)效果,就是打開(kāi)網(wǎng)頁(yè)時(shí)會(huì)在頂部顯示一個(gè)打開(kāi)進(jìn)度條,需要的朋友可以參考下2015-01-01
Android canvas畫圖操作之切割畫布實(shí)現(xiàn)方法(clipRect)
這篇文章主要介紹了Android canvas畫圖操作之切割畫布實(shí)現(xiàn)方法,通過(guò)clipRect方法實(shí)現(xiàn)canvas畫布的切割操作,需要的朋友可以參考下2016-10-10
Android?WebView的使用與后退鍵處理詳細(xì)討論
在android開(kāi)發(fā)中我們有時(shí)候根據(jù)項(xiàng)目的需求多少會(huì)加載一些webview,加載webview,我們有時(shí)候會(huì)根據(jù)UI來(lái)自定義返回鍵,下面這篇文章主要給大家介紹了關(guān)于Android?WebView的使用與后退鍵處理的相關(guān)資料,需要的朋友可以參考下2024-04-04

