FREELessons: 13Length: 1.2 hours

Next lesson playing in 5 seconds

  • Overview
  • Transcript

2.3 Augmented Physics Primer

Physics play a major role when it comes to creating an immersive AR experience. In this lesson, I'll talk about physics concepts and how you can add physics to your application.

2.3 Augmented Physics Primer

Hi, and welcome back to Get Started With Augmented Reality for iOS. In this lesson, I will give you an introduction to physics and how to use it in an augmented reality app. Interaction is an important concept of augmented reality. It draws the illusion of an interconnection between digital and real world. That's where physics come in. If digital objects react with a real surface, it feels like they are actually there. Let's look at a very simple example that will be in parts also our example project for the next lessons. Say you want to roll some dice. It would be so much more realistic if the dice would bounce on the surface of the table and off of each other like real dice do. And we can do that almost already with the knowledge we have from previous lesson. Just use plane detection for the table and don't draw the detector plane. Or just an indicator like in the example from Apple we use it to unloading models. Something important that I haven't mentioned yet, it doesn't really use physics but it acts with the digital objects in the same is Hit Testing. Imagining you want to play something on a surface. Like we learned last lesson, you have to know the position in the world where you want to do that. Hit testing casts a ray from the user's perspective to the point that was targeted on the camera and returns the location of a point that was a hit. Meaning the ray passed through another object. With this technique you can determine where to place objects in 3D space although you only have 2D surface for interaction. Okay, so I'm going to reach into the topic of the next lesson a little bit, but physics is essentially about collision and similar behavior. If you have looked a little bit into game development you might have seen simplified overlays over complex models. That is the player or other characters in a game. This is done for collision protection, especially in the early days when computer power wasn't as plenty as now, physics bared a hard task to achieve. So develop a simplified models with boxes, spheres, and powers to make it easier for systems to calculate collisions. Normally, they don't need to be super precise anyways. So using a combination of those shapes, that's good enough. This is also what we are going to do. Or rather, let's think and do for us in the example. So, let's have a look at that. I'm going to reuse the project from the lesson about plain detection, as we're going to need that as well. For this lesson, first I want to add a box to a position above the surface we're touching. I'm going to use the touches and that callback of a few controller to do that. To determine if we are going to add a box, we are going to perform a hit test. First I have to transform the touch to a position under a few. And then use the hit test function and do the same here together hit result. There are a few variants of this function. One uses types where you could use estimated planes or detected ones that are infinite in size. I'm using the one with an anti set of options. Since it could potentially have many hits, I'm only using the first one. There are a few properties on the hit. Interesting for us is the word coordinates property. It contains the coordinates of the hit and word coordinates. There is also a local coordinates one as well. That is in, the relative coordinate system of the nodes that has been hit. Another very useful one would be world normal, being a normal vector off of the surface that was hit. So if you would hit a sphere, your normal vector would be pointing away from the surface, 90 degrees. Okay, to the precision I'm going to add 25 centimeters, so we have some distance from the sphere. It's time to add the box. A cube will be a SCNBox, and just so it's easier for us to see I'm going to make it 20 centimeters long on each side. The cube geometry needs to be encapsulated in a node, so let's create that. The naught can get a position and be added to the sins root note. Now let's try that out. When we have our plane detected we can add some boxed to the world that are above the plane. Nice, but no physics yet and you also can't see them. Before I run it again let me just add a material to the plain very quickly so I can make it semi-transparent. The diffuse part of the material will get the code that is 20% wide. And you can edit the material to the plane geometry, done. Adding physics and synch it is as easy attaching physics body to your node. In our case, I'm adding a SCM physics node of the type dynamic to the node. I'll leave the shape as nil, so seeing it can figure out the ideal shape itself. Let's run it. Now when I add a cube, it instantly is affected by gravity, and falls indefinitely down. The magic part of this was that the physics body is of type dynamic, meaning it can be affected by forces like gravity. I'm going to leave it here for this lesson and we are going to continue this example in the next one. To recap, physics is essentially to make scene believable and create the illusion of interacting with real objects. Physics bodies are simplified representations of complex models for the physics system. Hit testing used to find objects and a 3D rod by costing a ray from the uses perspective to avoid under 2D screen. In our next lesson, we are going to tackle Collision Detection, and Apply Forces to Our Objects. See you there.

Back to the top