|
|
|
|
Chapter:
Interacting with images
Friction simulation
Friction is the force that resists the relative motion of something
else (like an object, a gas, a liquid, etc), and is the effect that we
will be attempting to simulate (in a simplified form) in this sketch.
By moving the mouse up and down within the Display Window the image
should try to follow the mouse and slow down as it approaches the
mouse, this will create the impression of friction.
We'll start by creating three new variables. Add the following
statements at the top of the sketch, below the PImage statement:
float yPos; //declaration related
to image Y position
float difY; //declaration
related
to difference between mouse and image's Y position
int frict = 30; //initialization, friction. high
values make image move slower
The float keyword is used for declaring floating point variables.
Floating point values are numbers that have a fractional part, that is
to say they are numbers with a decimal value such as 1.0, -2.9865,
10000.423, 0.0000001 and so on. Floating point numbers will tend to
require more memory for storage so if you can get away with
representing the number data as type int and not compromising the
integrity of your program you should consider doing so.
We are using the float datatype in this program because we will be
using division in one of our expressions. This is important because
true division will always return a number with a decimal value, if you
were to use an int instead of a float or double depending on the
programming language you are using, you risk having datatype conversion
errors or truncated integers returned. As a result when you use
division in your expressions always consider whether representing that
data with an int is a good idea or not.
The three variables yPos, difY and frict are going to be used to
represent three different values of which only one will remain constant
throughout the duration that the program is running. The only variable
that will remain constant is the frict variable. As a result the frict
variable has been initialized through a declaration and assignment
statement outside of both setup() and draw() structures. This value
will control how fast or slow we would like the image to move towards
the mouse.
Creating a variable such as this within the global variable scope, will
give us access to the variable within both setup() and draw() thereby
allowing us to tweak the value associated with the variable and have it
update throughout the program. This will save us some unnecessary work
in the long run. When the sketch is complete setting the frict value
high will cause the image to appear to have more resistance when moving
towards the position of the mouse ultimately making the image move
slower, and setting this value low (but not to zero, because division
by zero is not permitted in mathematics, as is the case in programming)
will cause the image to appear to have no resistance and move almost
immediately to the Y position of the mouse.
The variable yPos will be used to store the Y position of the image and
also update the Y position of the image, and the variable difY will be
used to store the difference between the mouse's Y position and the
center of the image. Both of these variables will need to be updated
regularly for each of the cycles of the draw() function, this is
because each time the user moves the mouse the position of the image
must change as a result of difY now being less or more depending on
whether the mouse is closer or further from the images center.
Variable naming
conventions
Before proceeding, it's worth noting a convention used for naming the
variables in this program. There are several rules (depending on what
language you are programming in) that determine what a valid variable
name is. Generally, you'll find that many popular higher level
languages share these similarities regarding valid variable declaration:
1. Variable names can only contain alphanumeric characters and
underscores (that is “_”, “a” to “z”, “A” to “Z” and/or “0” to “9”).
Spaces cannot be used in variable names.
2. Variable names cannot start with numbers.
3. Variable names should be descriptive but not verbose, for example
“yPos” as opposed to “Y_Position_Of_The_Image”.
4. Use consistent casing. Variable names generally do not start with an
upper-case character. This convention is reserved for Classes. In the
previous example I've used a mixture of both upper-case and lower-case
characters to name my variables such as yPos which is intended to be an
abbreviation for “y Position” or difY which is an abbreviation for “the
difference in Y positions”.
Some programmers might use different conventions including underscores
because to them this might look more readable y_pos or dif_y.
Either of these conventions is perfectly valid. But you should
determine which convention you are using, and stick with it throughout
your program. Changing from one naming convention to another can often
make a program confusing and less readable.
5. Your variable names should be self-documenting, that is to say they
should be descriptive about their purpose without having to include
additional comments explaining their purpose. This point is with the
exception of intentionally, explicitly documented code, which is
generally a convention used for teaching programming.
6. Variables that are typed in all upper-case characters are often
referred to as constants. Their value is not supposed to change. This
is simply a naming convention and there is nothing stopping you from
changing the value of a constant in many higher level languages,
although this is not recommended. So beware when using this naming
convention as it could lead to confusion if not used properly.
|
|
|
|
|
|