قالب وردپرس درنا توس
Home / Tips and Tricks / ARKit 101: How to Launch Your Own Augmented Reality Missile into the Real World «Mobile AR News :: Next Reality

ARKit 101: How to Launch Your Own Augmented Reality Missile into the Real World «Mobile AR News :: Next Reality



Have you noticed SpaceX and its launches lately? Did you ever imagine what it would be like to shoot your own rocket into the sky? Well, do not imagine anymore!

In this tutorial, you'll learn how to use ARKit to create a simple augmented reality app for iPads and iPhones. More specifically, we will talk about how we can shoot a rocket into the sky and make sure it continues to fly based on World Tracking.

What will you learn?

We will learn to animate 3D objects in the AR world based on real physical conditions. We will also receive help using the [hkTestandAircraftDetectionDetails we discussed earlier.

Minimum Requirements

Step 1: Download the required assets

To help you understand this tutorial I've created a folder with the required 2D and 3D assets needed for the project. With these files, you can easily follow this guide, so download the zipped folder with the assets and unzip.

Step 2: Set up the AR project in Xcode

If you're not sure how to do so Step 2 in our article on controlling a 3D layer using hitTest to get your AR project in Xcode set up. Give your project a different name. B. NextReality_Tutorial3 . Be sure to do a quick test before continuing with the following tutorial.

Step 3: Import assets into your project

In your Xcode project, go to the Project Navigator in the left sidebar. Right-click on the art.scnassets folder, where you save your 3D SceneKit files and then select Add Files to Art.scnassets. Add the following files from the unzipped "Assets" folder that you download in Step 1 above: "rocket.scn", "fire.scnp", "smoke.scnp", "smoke.png", " spark.png ". (Note: Do not delete the texture.png file that comes with the project.)

Back in the Project Navigator, right Click on the yellow folder for "NextReality_Tutorial3" (or how you named your project) Select "Files to & # 39; NextReality_Tutorial3 & # 39;

Navigate to the unzipped "Assets" folder and select "Rocket.swift" file. Be sure to check "Copy items as needed" and leave everything else unchanged. Then click on "Add".

"Rocket.swift" should be included in your project, and your project navigator should look something like this:

This file will focus on rendering the rocket and the smoke / fire particle system.

Step 4: Turn on Horizontally Plane Detection

To quickly discuss the capabilities of ARKit to detect levels, take a look at our tutorial on detecting horizontal planes

Open Class " ViewController.swift "by double-clicking on it. The rest of this tutorial will handle this document. If you want to follow the final code from step 4, just open this link to see it on GitHub.

In the ViewController.swift file, change the scene build line to viewDidLoad () ] method. Change it from:

  let scene = SCNScene (called: "art.scnassets / ship.scn")! 

To the following (which makes sure we do not create a scene with the old ship model): [19659029] let scene = SCNScene ()

Now we activate feature points. Under this line in viewDidLoad () :

  sceneView.showsStatistics = true 

Add the following:

  sceneView.debugOptions = ARSCNDebugOptions.showFeaturePoints 

Next, we enable detection of the horizontal plane. Under this line in viewWillAppear () :

  let configuration = ARWorldTrackingConfiguration () 

Then add the following:

  configuration.planeDetection = .horizontal 

This will ensure that ARKit is able to detect horizontal, flat geometric planes in the real world. The feature points allow us to see all the 3D points that ARKit can recognize.

Run your app on your phone and walk around. Focus on a well-lit area on the ground and you should be able to see yellow feature points as follows:

Step 6: Adding Animation and Physics Effects to the Rocket

Here we add effects that allow our rocket to launch and fly up, emitting the fire from its tail. Feel free to follow the last step 6 code along with while entering the content below.

First, add a double-tip gesture recognizer. Add the following code at the end of viewDidLoad () and after the gesture recognizer from step 5:

  get doubleTapGestureRecognizer = UITapGestureRecognizer (target: self, action: #selector (doubleTapped))
doubleTapGestureRecognizer.numberOfTapsRequired = 2
gesticrecognizer.require (toFail: doubleTapGestureRecognizer)
sceneView.addGestureRecognizer (doubleTapGestureRecognizer) 

This adds the double-tap gesture to launch the rocket.

Next, add doubleTapped () to the end of the file, but before the last curly brace:

  @objc func doubleTapped (Gest: UITapGestureRecognizer) {
// Got rocket and smoke knots
guard let rocketNode = sceneView.scene.rootNode.childNode (with name: "Rocket", recursive: true) else {
fatalError ("Rocket not found")
}

guard let smokeNode = rocketNode.childNode (with name: "smokeNode", recursive: true) else {
fatalError ("Smoke knot not found")
}

// 1. Remove the old smoke particle from the smoke node
smokeNode.removeAllParticleSystems ()

// 2. Add fire particles to the smoke knot
lost fireParticle = SCNParticleSystem (named: "art.scnassets / fire.scnp", inDirectory: nil)
smokeNode.addParticleSystem (FireParticles!)

// 3. Give rocket physics animation skills
rocketNode.physicsBody = SCNPhysicsBody (Type: .dynamic, Form: nil)
rocketNode.physicsBody? .isAffectedByGravity = false
raketeNode.physicsBody? .damping = 0.0
raketeNode.physicsBody? .applyForce (SCNVector3 (0,100,0), asImpulse: false)
} 

Let's examine exactly what we do for each of these steps:

  1. After we get the knots tied to the rocket and the smoke particle generator, we remove the old smoke particle generator. Why? Because our rocket will have fire instead of smoke when it starts!
  2. We load the Fire Particle Good and add this as a new Particle Generator to our Smoke Node.
  3. We are modifying some features of the physicsBody

    Checkpoint : Your entire project at the end of this step should look like the last Step 6 code on my GitHub.

What we have achieved

Good job! They successfully launched a rocket into the sky. Let's go over what you've learned in this tutorial: place a rocket on the ground using horizontal plane detection and hittest, add smoke and fire particles to the rocket, add physics animation to launch the rocket, and do so in the Be able to track your missile after takeoff thanks to worldwide tracking.

If you need the full code for this project, you can find it in my GitHub repository . I hope you liked this ARKit tutorial that gives you a way to the (virtual) stars. If you have comments or feedback, you are welcome to leave it in the comment section. Happy coding!

Do not miss : How to lay grass on the ground using plane detection

Cover pictures and screenshots of Ambuj Punn / Next Reality

Source link