Important formula:
(1) Simple slow motion
DX = targetx-object.x;
dy = Targety-object.y;
VX = dx * easing;
VY = dy * easing;
Object.x + = VX;
Object.y + = VY;
Can be streamlined:
VX = (targetx-object.x) * easing;
VY = (targety-object.y) * easing;
Object.x + = VX;
Object.y + = VY;
Further streamlining:
Object.x + = (targetx-object.x) * easing;
Object.y + = (targety-object.y) * easing;
(2) Simple elastic Motion
Ax = (targetx-object.x) * SPRING;
ay = (targety-object.y) * SPRING;
VX + = ax;
Vy + = ay;
VX *= friction;
VY *= Friction;
Object.x + = VX;
Object.y + = VY;
Can be streamlined:
VX + = (targetx-object.x) * SPRING;
VY + = (targety-object.y) * SPRING;
VX *= friction;
VY *= Friction;
Object.x + = VX;
Object.y + = VY;
Further streamlining:
VX + = (targetx-object.x) * SPRING;
VY + = (targety-object.y) * SPRING;
Object.x + = (VX *= friction);
Object.y + = (vy *= friction);
(3) An offset of the elastic motion
DX = Object.x-fixedx;
dy = object.y-fixedy;
Targetx = Fixedx + math.cos (angle) * SPRINGLENGTH;
Targety = Fixedy + math.sin (angle) * SPRINGLENGTH;
Important formula for boundary detection:
(1) Rectangular boundary collision detection
tool.intersects = function (bodya,bodyb) {
return! (bodya.x + bodya.width < bodyb.x | |
bodyb.x + bodyb.width < bodya.x | |
BODYA.Y + bodya.height < BODYB.Y | |
Bodyb.y + bodyb.height < BODYA.Y);
};
(2) Distance-based collision detection
DX = objectb.x-objecta.x;
dy = objectb.y-objecta.y;
Dist = math.sqrt (dx * dx + dy * dy);
if (Dist < Objecta.radius + Objectb.radius) {}
(3) Multi-object Collision detection
for (var i = 0; i < objects.length; i++) {
var objecta = objects[i];
For (var j = i + 1; j < Objects.length; J + +) {
var OBJECTB = objects[j];
if (tool.intersects (OBJECTA,OBJECTB) {}
}
};
A formula for bouncing and a formula for detecting rectangular boundaries