Book Image

Learning Unity Physics

By : K. Aava Rani
Book Image

Learning Unity Physics

By: K. Aava Rani

Overview of this book

<p>Unity is a powerful game development engine that provides rich functionalities to create 2D and 3D games. Developers get the opportunity to build cross-platform mobile and desktop games from scratch. With the increasing interest in using Physics in interactive development, Unity3D offers a cutting-edge platform for simulation, game development, and application development through a user-friendly interface. You will learn the fundamentals of Physics in game development. The book starts by providing a quick introduction on how we can use Physics for interactive development. Furthermore, it provides a detailed description on the uses of Physics material in Unity3D and how we can use different colliders for interaction. The book also focuses on Rigidbodies and Joints, its types and properties. There is also a dedicated section on how we can optimize applications and games if we use Physics in Unity3D. By the end of this book, you will be capable of successfully developing Physics-based simulations, games, and applications.</p>
Table of Contents (14 chapters)

Basic components of Physics for interactive development


There are a few basic components that can be used for interactive development. Let's take a look at them one by one:

Integration

There are multiple factors that have to be considered when we are talking about the integration of Physics. How we track objects that participate in simulation is of paramount importance in this regard.

To implement Physics in interactive development, we normally track all the objects that are simulating in a data structure.

To implement Physics for each object, we need to know some important information such as the object's physical properties, that is, mass, current velocity, current position, orientation, the external forces acting on the object, and the future time of an object.

Note

What is future time?

Future time = the current time + the time slice for the frame

Collision detection

There is no collision detection if only one object is moving in a vacuum. However, most interactive developments involve more than one object and these objects move in an environment. Therefore, a situation arises where two objects are moving towards each other.

What will happen if no action is taken? These objects will just pass through each other. In most interactive developments, however, we don't want the objects to pass through each other.

In order to handle those scenarios where collision occurs, the interactive development needs to know that two objects are colliding. One of Physics' most important tasks is to identify these scenarios.

The collision detection code has to determine all such pairs of overlapping objects, collect some additional data such as how far they overlap and in which orientation, and provide this data to the interactive development for further processing.

In the later chapters, we will see how we can handle collision detection in Unity3D.

Collision resolution

What will happen after the collision? Let's take a look at one such scenario where two or more objects overlap and see what can we do in this case. In many cases, some specific rules are added to the interactive development.

For example, in a shooting game, when a bullet hits the player's ship, the game might decide to show an explosion animation. Following this, before removing the player's ship, the game might start the level again and reduce the number of lives of the player. These effects of the collision are driven by the game itself and not by Physics because they are very specific to the game.

However, there are certain cases where the game doesn't have to be involved.

We've covered the basics of all Physics components. Every simulation suitable for interactive development will have these components. Other than the covered topics, there are many more features to be included, such as joints, cloth simulations, Physics-based animations, and so on.