Jump to content

"Shaking" a sprite


Recommended Posts

Hi, you can create your own tweening function. For shaking you can use "wiggle" function:

function wiggle(aProgress: number, aPeriod1: number, aPeriod2: number): number {
            var current1: number = aProgress * Math.PI * 2 * aPeriod1;
            var current2: number = aProgress * (Math.PI * 2 * aPeriod2 + Math.PI / 2);

            return Math.sin(current1) * Math.cos(current2);
        }

and call it from your game like this:

            tweenX.to({ x: this.position.x + aRadius}, aTime, function (k) {
                return Easing.wiggle(k, aXPeriod1, aXPeriod2);
            }, true, 0, -1);

... change arguments aXPeriod1 and aXPeriod2 to make different shakes. aRadius gives number of pixels from current x position.

To shake in both X and Y directions add another tween for Y with different periods.

If set for x and y with different periods it in fact does nice sin/cos spline movement in range of aRadius (can be different for x and y tween). If period is high and time short, then it makes fast shake. Nice thing is, that this shake looks more natural than just random offsetting. If time is long then you can have for example randomly moving dust particles. See http://sbc.littlecolor.com/woodventure/ - branches are using it, sunrays are using it and dust particles are too using it... even hedgehog's muzzle is using it :-)

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...