Let’s finish off the Mover class by incorporating a function to determine what the object should do when it reaches the edge of the window. That ParticleSystem object will have as its data a list of Particle objects…and each Particle object will have as its data several PVector objects! For example, in Chapter 4 we’ll write a class to describe a system of particles. That data can be numbers (integers, floats, etc.) or other objects! We’ll see this over and over again in this book. An object is simply something that holds data (and functionality). So what are they doing inside of yet another object, the Mover object? In fact, this is just about the most normal thing ever. The PVector class is the template for making the location object and the velocity object. After all, we spent the beginning of this chapter discussing the PVector class. If object-oriented programming is at all new to you, one aspect here may seem a bit confusing. An object on screen has a location (where it is at any given moment) as well as a velocity (instructions for how it should move from one moment to the next). What does it mean to program motion using vectors? We’ve seen the beginning of this in Example 1.2: the bouncing ball. And you don’t have to wait too long, as your reward will come in the next chapter.įor now, however, we want to focus on simplicity. What might seem like more work now will pay off later, and pay off quite nicely. But that plan quickly breaks down when you need a hundred, or a thousand, or ten thousand things. For example, when you first learn about an array, it might seem like much more work to use an array than to just have several variables stand for multiple things. This is actually a common occurrence when first learning a new data structure. It will take some time before the awesomeness of using the PVector class fully comes to light. This one’s not so bad just take the plus sign and replace it with a minus! Vector subtractionĪll this vector math stuff sounds like something we should know about, but why? How will it actually help us write code? The truth of the matter is that we need to have some patience. Having already covered addition, let’s start with subtraction. Lerp() - linear interpolate to another vectorĭist() - the Euclidean distance between two vectors (considered as points)ĪngleBetween() - find the angle between two vectorsĬross() - the cross product of two vectors (only relevant in three dimensions) Rotate() - rotate a 2D vector by an angle Heading() - the 2D heading of a vector expressed as an angle Limit() - limit the magnitude of a vector Normalize() - normalize the vector to a unit length of 1 Mag() - calculate the magnitude of a vector Mult() - scale the vector with multiplication When you see the term “vector” in this book, you can assume it refers to a Euclidean vector, defined as an entity that has both magnitude and direction.Ī vector is typically drawn as a arrow the direction is indicated by where the arrow is pointing, and the magnitude by the length of the arrow itself. What we want is called a Euclidean vector (named for the Greek mathematician Euclid and also known as a geometric vector). While all these definitions are interesting, they’re not what we’re looking for. In the C++ programming language, a vector (std::vector) is an implementation of a dynamically resizable array data structure. In the field of epidemiology, a vector is used to describe an organism that transmits infection from one host to another. It’s the name of a breakfast cereal manufactured by Kellogg’s Canada. Vector is the name of a New Wave rock band formed in Sacramento, CA in the early 1980s. Now, the word vector can mean a lot of different things. Absolutely everything contained within the first five chapters of this book requires the use of the most basic building block for programming motion-the vector. Divided into three parts, the book will start by looking at basic physics-how an apple falls from a tree, a pendulum swings in the air, the earth revolves around the sun, etc. This book is all about looking at the world around us and coming up with clever ways to simulate that world with code.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |