A formula for bouncing and a formula for detecting rectangular boundaries

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.