Most forces encountered in human experience fall into one
of the categories already covered. These represent a categorization of
forces: surface gravity which is constant,
air friction which depends on velocity, and spring which
depends on position. Most likely a behavior you want
to achieve will fall into one of these categories, or a combination of
them.
The first thing to determine is: what does the force on
the object depend on, and how can it be expressed in terms of
the variables in your program.
I'll go through an example of how to use
the physics model to create my own imaginary environment.
In this environment, I want objects to be repelled from the top, right
and left edges of the stage. The closer the object gets to an edge, the
stronger is the force repelling it away. If it hits the bottom edge it
will be shot upward at a random direction and speed.
So there will be three forces: top, left, and right.
What would these forces depend on?
Click to add objects - To restart, right-click and
select "restart" Invented forces - source
movie - vector version
--acceleration, velocity, position
xAccel = xLeftForce + xRightForce
yAccel = yTopForce
xVelo = xVelo + xAccel
yVelo = yVelo + yAccel
x = x + xVelo
y = y + yVelo
--check condition if y > (thestage).rect.height
then
y = (thestage).rect.height
xVelo = random(19)
- 10-- -9
to 9
yVelo = -random(15) end if
sp.loch = x
sp.locv = y end
In this demo, the forces are acting parallel to the x and
y axes, so only an x ory component is coded
for each force. It's still the same form, but the 0 components are left
out (vertical for left and right forces, horizontal for top force are
0).
You've probably noticed that the three forces depend
on position. The requirement for the force is that the closer
the position is to the edge, the greater the force. There are
a variety of different equations that would work. For example, the force
equation for the left edge could be:
All of these give larger positive values the smaller
x gets, which is the basic requirement for the left force I wanted
to create. Try them and see how the behavior of the object changes. The
xRightForce equation gives larger negative values
the smaller (stageWidth - x) gets.
The last equation is in the form of a spring force
with resting position at 500. What would this one do if x
became greater than 500? Which direction would the force act in?
What happens when x is greater than 500
in the first equation? What's the difference between the first and second
equations?
•What would the animation look like if the objects
had different masses? How would you add general gravity?