Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by Joncom

Sorry, the title should have read: Why don't these block bounce around forever at a constant rate?

Was playing around with Entity.bounciness tonight.
And I was curious, how come the squares lose speed over time?

There is no friction.
And minBounceVelocity is 0.

Shouldn't they keep bouncing around forever at a constant rate?

// square.js
EntitySquare = ig.Entity.extend({

    size: { x: 16, y: 16 },
    speed: 200,
    bounciness: 1,
    minBounceVelocity: 0,
    collides: ig.Entity.COLLIDES.ACTIVE,
    animSheet: new ig.AnimationSheet('media/tilesheet.png', 16, 16),

    init: function(x, y, settings) {
        this.parent(x, y, settings);

        // Give the entity its appearance.
        this.addAnim('default', 1, [0]);

        // Set speed as the max velocity.
        this.maxVel.x = this.maxVel.y = this.speed;

        // Generate a random angle between -180 and 180 degrees, in radians.
        this.angle = (Math.ceil(Math.random()*360) - 180) * (Math.PI/180);

        // Set initial velocity.
        this.setVelocityByAngle(this.angle, this.speed);
    },

    setVelocityByAngle: function(angle, velocity) {
        var slope = Math.tan(angle);
        var x_factor = (Math.abs(angle) < 1.57 ? 1 : -1); // 1.57 rads ~~ 90 degrees
        var y_factor = (angle > 0 ? 1 : -1);
        var rise = (Math.abs(slope) / (1 + Math.abs(slope)));
        var run = (1 / (1 + Math.abs(slope)));
        this.vel.y = y_factor * velocity * rise;
        this.vel.x = x_factor * velocity * run;
    }

});

1 decade ago by quidmonkey

When two ACTIVE entities collide in Impact, both are moved apart and .bounciness is not part of the equation:
    var v2 = (left.vel.x - right.vel.x)/2;
    left.vel.x = -v2;
    right.vel.x = v2;

As you can see, the total vel of both is summed and then halved. Since your init is applying an angular vel that's randomly < 200 speed for all entities, when they collide, their vel along both axes is being manipulated by that equation above.

1 decade ago by Joncom

That explanation makes perfect sense quidmonkey.
Thank you.
Page 1 of 1
« first « previous next › last »