HTML and CSS Reference
In-Depth Information
Important formulas in this chapter
Obviously, the big formula here is gravity.
Basic gravity
force = G × m 1 × m 2 / distance 2
JavaScript-friendly gravity implementation
function gravitate (partA, partB) {
var dx = partB.x - partA.x,
dy = partB.y - partA.y,
distSQ = dx * dx + dy * dy,
dist = Math.sqrt(distSQ),
force = partA.mass * partB.mass / distSQ,
ax = force * dx / dist,
ay = force * dy / dist;
partA.vx += ax / partA.mass;
partA.vy += ay / partA.mass;
partB.vx -= ax / partB.mass;
partB.vy -= ay / partB.mass;
}
This function is using an instance of the Ball class, but you can use any object, as long as it can store
values for velocity, mass, and position.
Summary
This chapter covered interaction between particles at a distance, and how you can use gravity and
springing for interesting effects. As a result, you have two new ways to make some very dynamic motion
graphics involving many objects.
In the next couple of chapters, we discuss some new subjects: forward kinematics and inverse kinematics .
These techniques enable you to make neat things such as robot arms and walking figures.
 
Search WWH ::




Custom Search