Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

removing springs is not working properly. #1134

Closed
valueerrorx opened this issue Aug 23, 2014 · 16 comments

Comments

Projects
None yet
2 participants
@valueerrorx
Copy link

commented Aug 23, 2014

removeSpring(); takes an array instead of one single spring.

removeSpring(); deletes the last created spring. but every new created spring is behaving like it is influenced by the old spring

see here for a test: http://test.xapient.net/phaser/springtest/
click on the cow to create a spring and throw it.. the harder you throw the stronger is the effect on the next created spring..

it also seems that there is an issue with this part of phaser:

World.prototype.removeSpring = function(s){
    var idx = this.springs.indexOf(s);
    if(idx === -1){
        Utils.splice(this.springs,idx,1);
    }
};

read here for more info:
http://www.html5gamedevs.com/topic/8710-creating-a-spring-sometimes-results-in-a-wrong-configured-spring/?p=51886

@valueerrorx

This comment has been minimized.

Copy link
Author

commented Aug 23, 2014

the mentioned effect where the newly created spring creates a deflexion of the body depends on the "damping" parameter.. it is completely gone when damping is set to 0 (but this is no way to go because without damping the bodies become to fast on the spring)

i guess this is a speparate problem - should i open another issue for that?

@photonstorm

This comment has been minimized.

Copy link
Owner

commented Aug 28, 2014

Could you please check this against the dev branch, as I just updated to the latest version of p2 tonight and added in support for linear and rotational springs.

photonstorm added a commit that referenced this issue Aug 29, 2014

Moved Springs to not extend p2 Springs as they break when added to th…
…e p2.World. Springs and spring removal now working properly (#1134)
@photonstorm

This comment has been minimized.

Copy link
Owner

commented Aug 29, 2014

Ok I created a "remove springs" test case and it seems to be working fine now.

@valueerrorx

This comment has been minimized.

Copy link
Author

commented Sep 2, 2014

hi there.. back from my vacation...

i can NOT delete a spring anymore with the newest build (5 minutes ago)
.. could you please provide your testcase?
here is mine.. as you can see the spring is never released once you created one by clicking on the cow..
http://test.xapient.net/phaser/springtest/

thank you very much..

@photonstorm

This comment has been minimized.

Copy link
Owner

commented Sep 2, 2014

Here, this still works fine for me:


var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update });

function preload() {

    game.load.image('atari', 'assets/sprites/atari130xe.png');
    game.load.image('ball', 'assets/sprites/red_ball.png');
    game.load.image('sky', 'assets/skies/cavern2.png');

}

var sprite1;
var sprite2;
var cursors;
var spring;

function create() {

    game.add.image(0, 0, 'sky');

    //  Enable p2 physics
    game.physics.startSystem(Phaser.Physics.P2JS);

    //  Add 2 sprites which we'll join with a spring
    sprite1 = game.add.sprite(400, 300, 'ball');
    sprite2 = game.add.sprite(400, 400, 'atari');

    game.physics.p2.enable([sprite1, sprite2]);

    sprite1.body.collideWorldBounds = true;
    sprite2.body.collideWorldBounds = true;

    //  Create our spring
    //  The parameters are: createSpring(sprite1, sprite2, restLength, stiffness, damping, worldA, worldB, localA, localB)
    //  See the docs for more details
    spring = game.physics.p2.createSpring(sprite1, sprite2, 20, 10, 1);

    text = game.add.text(20, 20, 'move with arrow keys, click to remove spring', { fill: '#ffffff' });

    cursors = game.input.keyboard.createCursorKeys();

    game.input.onDown.add(removeSpring, this);

}

function removeSpring() {

    game.physics.p2.removeSpring(spring);

}

function update() {

    sprite2.body.setZeroVelocity();

    if (cursors.left.isDown)
    {
        sprite2.body.moveLeft(400);
    }
    else if (cursors.right.isDown)
    {
        sprite2.body.moveRight(400);
    }

}
@photonstorm

This comment has been minimized.

Copy link
Owner

commented Sep 2, 2014

Your code is wrong, look:

for (i=0; i<=allSprings.length; i++){ game.physics.p2.removeSpring(allSprings);

should be:

for (i=0; i<=allSprings.length; i++){ game.physics.p2.removeSpring(allSprings[i]);

@valueerrorx

This comment has been minimized.

Copy link
Author

commented Sep 2, 2014

sorry this is just a typo.. it also doesn't work with the correct code ...

removing just one spring works as you said. so this one works:

 myspring= game.physics.p2.createSpring(cow, mouseAnchor, 0,20,2);
 game.physics.p2.removeSpring(myspring) 

but getting ALL springs and removing them in a for loop does not work.. i updated my example (added the missing i ) .. i couldn't find another error.. if i do the same for constraints for example it works as it should.. printing the array that getSprings creates gives me [null]


also .. did you see the weird effect in my examlpe that the damping parameter creates?
i created two other testcases (where the removing of the spring works with a console.log) to show this behaviour:

here i set damping to 0:
http://test.xapient.net/phaser/springtest/index1.html
(every new spring behaves exactly the same)

here i set damping to 2: (1 would be enough but the effect is bigger with 2)
http://test.xapient.net/phaser/springtest/index2.html
(every new spring - depending on how heavy you streched the old one - produces a strange "distance" of the two bodies

@photonstorm

This comment has been minimized.

Copy link
Owner

commented Sep 2, 2014

Ok so the actual error is that getSprings returns null :) See latest commit.

re: damping I've no idea, try creating the springs directly in p2 without using Phaser at all and see if it still does it - in which case it needs opening as a p2 issue.

photonstorm added a commit that referenced this issue Sep 2, 2014

@valueerrorx

This comment has been minimized.

Copy link
Author

commented Sep 2, 2014

tried it.. works now.. bug resolved ! thx.

as for the other one.. i'm gonna give it a try and report back.. (maybe in a separate issue if it doesn't change anything)

@valueerrorx

This comment has been minimized.

Copy link
Author

commented Sep 2, 2014

sorry rich... i'm unable to add a spring only by using p2.. there is too much i don't know if i work only with p2 to recreate my example and adding the spring in my code with

myspring = new p2.LinearSpring(cow.body,mouseAnchor.body, {
                    stiffness: 0,
                    restLength: 20,
                    damping : 2
                });
    Phaser.Physics.P2.addSpring(myspring);

results in "undefined is not a function"

so i can not determine if this problem is p2 or phaser related but it is definitely there..
if you hold down the mouse button in my example to keep the spring alive you will see that the offset is slowly getting smaller.. if you wait until it's almost 0 you can then create a new spring with normal behaviour (until you strech this spring every new created spring will be normal)

it looks like the damping of the old spring is still affecting the new spring as long as it takes to get the two bodies together again despite the damping..

i created a separate issue for that :
#1161

@photonstorm

This comment has been minimized.

Copy link
Owner

commented Sep 2, 2014

The way you're doing it is nearly right, just do cow.body.data instead (data = the actual p2 body), and game.physics.p2.world.addSpring(spring) as this will bypass Phaser adding it, and add it directly to the world on its own.

@valueerrorx

This comment has been minimized.

Copy link
Author

commented Sep 2, 2014

oh.. i already had game.physics.p2.world.addSpring .. the .data was the problem thx!

so i did this:
http://test.xapient.net/phaser/springtest/
and the problem is still there..
does this mean i have to talk to schteppe about this ?

do you think this could have something to do with the way i pin the second body (the mouseanchor) to the mouse position?

@photonstorm

This comment has been minimized.

Copy link
Owner

commented Sep 2, 2014

Yes you need to ask him. It might be anchor related, but it's hard to say. He'll know quickly :)

@valueerrorx

This comment has been minimized.

Copy link
Author

commented Sep 7, 2014

http://test.xapient.net/phaser/springtest/index4.html

here is the working example.
as you have foreseen he knew the solution quickly..

the solution was the following:

You need to set the anchor body as static. Otherwise, the spring will apply force to it, and it will get velocity. You don't actually see this velocity in action since you always set the position of the anchor body each frame. This "hidden velocity" is added to the calculations of the damping.

To fix, do this:

mouseAnchor.body.static = true;

and btw.
feel free to include the code in the phaser examples :)

thank you very much!

@photonstorm

This comment has been minimized.

Copy link
Owner

commented Sep 7, 2014

Awesome. And thanks, I'll include the code as it's a very useful example to have :)

@valueerrorx

This comment has been minimized.

Copy link
Author

commented Apr 7, 2015

this bug seems to be back again but now it affects getConstraints

game.physics.p2.world.constraints.splice(0,game.physics.p2.world.constraints.length);
delivers all constraints but
game.physics.p2.getConstraints(); delivers an array with "undefined" in it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.