Chandu
Chandu

Reputation: 131

How to do slow motion effect in flash game with box2d

As i am working on top view racing game in which am trying to add slow motion effect when a car hits objects. I have tried with decreasing Stage.frameRate but the game appears lagging. and i have also tried with online tutorial called touch my pixel ( ref : http://blog.touchmypixel.com/2009/12/box2d-contactpoint-filtering/ ). But i didn't understand.

Is there any solution for showing such kind of slow motion effect. can anybody help me in this regard

Thanks and regards,

Chandrasekhar

Upvotes: 1

Views: 633

Answers (1)

Marty
Marty

Reputation: 39476

Easiest way would be to have a global modifier property somewhere which can be used to multiply the movement of everything in the game.

For example, you could have the property speedModifier default to 1.

public var speedModifier:Number = 1;

And whenever you apply velocities, just multiply by the modifier:

body.SetLinearVelocity( new b2Vec2(x * speedModifier, y * speedModifier) );

This way all you need to do to half the speed of the game is to half the modifier:

speedModifier = 0.5;

To keep your code tidier and make managing this component of your game easier, there is probably a straightforward way to iterate over all of the bodies within the Box2D world and modify their velocities at the top of each update step. Something along the lines of:

for each(var i:b2Body in world.GetBodyList())
{
    var currentVel:b2Vec2 = i.GetLinearVelocity();

    var newVel:b2Vec2 = new b2Vec2(
        currentVel.x * speedModifier,
        currentVel.y * speedModifier
    );

    i.SetLinearVelocity( newVel );
}

Upvotes: 1

Related Questions