Jump to content

Very strange collision behavior


Recommended Posts

I made a pretty good Frogger clone using PhaserJS, but when refactoring the code, I ran into a very, very strange problem.

My vehicles currently have a velocity of 200. When Frogger collides with one of them, that vehicle's velocity drops in half. Frogger also gains the velocity of that object.

In update

this.game.physics.arcade.collide(frogger, vehicles, this.loseLife);

Here's my vehicles code

objects.forEach(o => this.game.physics.arcade.enable(o));
    [normalCar1, normalCar2, normalCar3, specialVehicle1, specialVehicle2, specialVehicle3, emergencyVehicle1, emergencyVehicle2, emergencyVehicle3].forEach(function(object) {
        object.body.velocity.x = object.direction == 'right' ? 200 : -200;

Here is my loseLife() function

loseLife: function() {
    /*switch (frogger.health) {
        case 5: lifeFive.destroy(); break;
        case 4: lifeFour.destroy(); break;
        case 3: lifeThree.destroy(); break;
        case 2: lifeTwo.destroy(); break;
        case 1: lifeOne.destroy(); break;

    /*if (!frogger.alive) {

The important method here is resetPosition() which moves Frogger back to the starting position

resetPosition: function() {
    frogger.alpha = 0;
    frogger.x = this.game.world.centerX;
    frogger.y = this.game.world.height - 18;
    game.time.events.add(1000, function() {
        this.game.timer = 50000; 
        frogger.alpha = 1;
    }, this);

The important thing here is that I do not specify that I want the velocity to increase

Hopefully, a video will make it easier to understand


Edit: A temporary working solution

I was able to "solve" this by adding this line in the resetPosition() function

frogger.body.velocity.x = 0;

And to remove that weird behavior of the car slowing down

objects.forEach(function(object) {
    object.body.immovable = true;

This is a hack though. I shouldn't have to explicitly add these lines, because collisions shouldn't cause this behavior in the first place.

Link to comment
Share on other sites


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...