Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use SceneForm to realize bullet firing and draw bullet trajectory

Shulou Source: shulou.com Published: 2022-06-03 06:11:33 10月01日 Update

This article shows you how to use SceneForm to achieve bullet shooting and draw the bullet trajectory, the content is concise and easy to understand, absolutely can make your eyes bright, through the detailed introduction of this article, I hope you can get something.

Bullet firing based on SceneForm (drawing bullet trajectory)

The Sceneform framework is very powerful. When you don't know Sceneform, you need OpenGL to make a 3D scene, and the learning curve of OpenGL is very steep. When you come into contact with this framework, you feel that rookies can get started quickly, and even achieve the effect of first-person shooting.

Note: I have been studying SceneForm for some time, but I haven't found the interface to simulate the gravity field. I don't know if I missed it.

The idea of simulating the shooting effect is actually very simple.

1. Load a bullet model 2. Plan the bullet's trajectory from near to far. 3. Draw the bullet's trajectory.

Bullet trajectory of the logic code; the code involved in the CleanArFragment in the previous "ARCore SceneForm framework in the absence of Plane drawing 3D model" has been given; in addition, you need to provide a texture image, that is, the R.drawable.texture in the code.

Class MainActivity: AppCompatActivity () {var arFragment: CleanArFragment? = null var camera: Camera? = null var size = Point () / / screen size Control the initial position of the bullet firing var bullet: ModelRenderable? = null var scene: Scene? = null val SHOT = 0x1101 / / drawing process track signal val SHOT_OVER = 0x1102 / / clear bullet model signal var handler = object: Handler () {override fun handleMessage (msg: Message) {if (msg.what = = SHOT) {/ / draw the trajectory in the moving process var currentStatus = msg.obj as CurrentStatus currentStatus.node.worldPosition = currentStatus.status} else if (msg) .what = = SHOT_OVER) {/ / one shot completed Clear the bullets on the screen var node = msg.obj as Node scenetically loaded. RemoveChild (node)} override fun onCreate (savedInstanceState: Bundle?) {super.onCreate (savedInstanceState) setContentView (R.layout.activity_main) / / get the screen size val display = windowManager.defaultDisplay display.getRealSize (size) arFragment = this.supportFragmentManager.findFragmentById (R.id.arFragment) as CleanArFragment arFragmentroom.arSceneView.planeRenderer.isEnabled = false / / prohibit the planar drawing of sceneform frames scene = arFragment Var listener: View.OnClickListener = object: View.OnClickListener {override fun onClick (v: View?) {shoot ()}} @ TargetApi (Build.VERSION_CODES.N) / / initialize the bullet model private fun initbullet () {Texture.builder () .setSource (this@MainActivity) R.drawable.texture) .build () .thenAccept ({texture-> MaterialFactory.makeOpaqueWithTexture (this@MainActivity, texture) .thenAccept {material-> / / set the bullet model to sphere bullet = ShapeFactory.makeSphere (0.1f, Vector3 (0f, 0f, 0f), material)})} private fun shoot () {/ / rays emitted from the screen The trajectory of the corresponding bullet var ray = camerabullet. ScreenPointToRay (size.x / 2f, size.y / 2f) Var node = Node () / / bullet node node.renderable = bullet / / bullet node loads bullet model scenegrain override fun run. AddChild (node) Thread (object: Runnable {override fun run () {/ / the trajectory during bullet firing, the child thread handles the trajectory event, and the main thread changes the trajectory position for (I in 1. 200) {/ / bullet range 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)} / / after the bullet exceeds the distance, clear var msg = handler.obtainMessage () msg.what = SHOT_OVER msg.obj = node handler.sendMessage (msg)}} from the screen. Start ()} / / data class data class CurrentStatus (var node: Node, var status: Vector3) of the penetration point of the child thread and the main thread

Interface layout

Because of the deviation of the moving picture, the bullet is not very clear, and the bullet is fired from the red cross in the center to the distance.

The above content is how to use SceneForm to achieve bullet shooting and draw the trajectory of bullets, have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Tags: Bullets tracks shooting running models screens frames threads code effects processes positions signals content dimensions skills knowledge nodes learning power Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Linux Shulou Information Redmi Huawei