Sign in to follow this  
valueerror

creating a spring sometimes results in a wrong configured spring

Recommended Posts

first i create a mousebody...

mouseBody = game.add.sprite(0, 0, '', 0);game.physics.p2.enable(mouseBody,true);mouseBody.body.setCircle(2);mouseBody.body.data.shapes[0].sensor = true;

this one is kept at the mouse position via the update loop with this lines:

mouseBody.body.x = game.camera.x+game.input.x;mouseBody.body.y = game.camera.y+game.input.y;

then i check if i hit a "tux" with a mouseclick and if so i create a connection between the mousebody and my tux body..

game.physics.p2.createSpring(hitobjectbody, mouseBody, 1,20,1);

this works great but sometimes the spring created doesn't follow the configuration  it is definitely not 1,20,1  it is more like 30,40,1

 

i just can't find out why this is happening but it's driving me nuts..

 

i clear all springs with this lines:

var allSprings = game.physics.p2.getSprings();    if (allSprings.length > 0){         game.physics.p2.removeSpring(allSprings);    }

there is something wrong with remoeSpring because it only takes an array.. no single spring (i already posted about that)

 

thx!

 

(in the attachment you can see that the mousebody keeps it's distance to the tux.. it is exactly below the mouse pointer but the tux can never be there even if the "restLength" of the spring should be 1px as configured..  )

 

 

 

 

 

post-6912-0-16275100-1408655479.jpg

Share this post


Link to post
Share on other sites

well..   why do you think this problem has something to do with the other? (the springs are removed on mouseUp  - otherwise the tux would still be tied to my mouse...

 

i tried what you suggested and it results in an errorles "no spring is removed ever" state....

 

with  :

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

removing of springs doesn't work anymore..

Share this post


Link to post
Share on other sites

I didn't look into your issue yet, all I saw is that you are removing springs the wrong way.

 

game.physics.p2.removeSpring(allSprings); <- does nothing but removing the last spring.

 

After you have changed the code to idx !== -1 you can remove all spring by doing:

 

var allSprings = game.physics.p2.getSprings();    if (allSprings.length > 0){        for (var i=0; i<allSprings.length; i++) game.physics.p2.removeSpring(allSprings[i]);    }
Even if that doesn't solve your issue, it will solve the removing of all springs for your game.

Share this post


Link to post
Share on other sites

that's exactly what i did and it didn't remove anything..

 

removeSpring()  removes the one spring i have..  (there should (atm) never be more than one springs in my game - so this would also work for me right now.. and it seems to work)

game.physics.p2.getSprings();

returns always Array[null]       

 

maybe that's why removing it the proper way  (like you posted above) with the idx !==-1 change does not work for me...

 

this definitely does not work here..  so ...  what version of phaser are you using?  i just remebered that i'm using 2.1 because of another bug in 2.0.7..  

maybe i should build the latest dev branch and give it a try.. :)

edit: nope.. latest dev build 2.1 doesn't solve the problem..  idx!==-1 results in inability to remove springs and the described problem of the first post still exists

edit2:  i tried 2.0.7 with the proposed "hack" and i have the same outcome..  unable to remove any springs after that..  i even copied your code over mine to be sure i made no mistakes

 

 

BUT if there is only ONE spring  and i removed the last spring - there shouldn't be any springs interfering ...

 

so like you said..  this issue has probably another reason...

Share this post


Link to post
Share on other sites

I don't know if this will help, as it does seem different problems, but I wrestled with the spring bugs for a bit.  In the end I managed to overcome the problems by manipulating the p2 springs array directly.  All that the various functions are supposed to do is modify this anyway and then in the loop the springs that are part of this array adjust the movement of the connected bodies.  As such I found that I could remove selected springs like this:

 

 this.game.physics.p2.world.springs.splice(springToDestroy,1); 

 

Obviously you can also use this to change other properties directly to the spring in each index and maybe this will help you overcome some of the weird behaviour

 

Once I realised I could do this most of the bugs could be overcome manually although more than a little hacky!  

 

In another thread Rich said that P2 hasn't changed for quite a bit so I'd imagine any 2.0 release will be the same.  The next release will have an updated p2 so fingers crossed...

 

This was all in aid of this game which is finished now although the springs don't appear until about level 15:

http://www.startyourrockets.com/mikros/

Share this post


Link to post
Share on other sites

thx.. this would work to remove the spring but i have only one spring (for now) so removing it is no problem.. i'm definitely sure that the described problem has nothing to do with the number of springs..

 

there is ONE spring and it behaves very very strange..  more stiffness than configured and way more restlength...  clicking around many times (and creating/destroying springs by clicking on my player for example) harmonizes the problem.. the spring "missbehaviour" gets less on every click until it acts normal (as configured) again...  i just don't get it ..

 

what in the world (in my code) could provoke such weird behaviour?? 

Share this post


Link to post
Share on other sites

Looking at your spring creation parameters they're quite different from mine which seemed like what an 'average' spring would be like:

 

this.spr1 = this.game.physics.p2.createSpring(b1.body, b2.body, 150, 0.15, 0.05);

 

If I recall I couldn't get the springs to work correctly on numbers I expected to use and so I think I just made random stabs in the dark until these worked, but they're so different to yours I wonder if during the creation and destruction the numbers are being dampened/altered until they reach a point p2 can deal with.  I'd imagine your numbers make more sense than mine but with there being clear bugs in the springs maybe the numbers aren't quite what the docs suggest they should be.  I also don't create and destroy springs within the same state, just create at the start and destroy during the game.  Further springs are created in new states and so I may not be seeing the change in properties because I don't have that cycling of the springs within a single state. 

 

I feel your pain with the springs but that's the sum of my experience with them and getting that far nearly drove me insane!

Share this post


Link to post
Share on other sites

thx spencerTL  ...  i will play with the configurations a little bit and see if this changes something..  meanwhile i strippe down my whole game to the player and the spring for you to test..

 

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

 

 

one thing to say is:   to reproduce the strange spring behaviour it is necessary to create a spring once or twice and throw the player around big time..  the longer the spring gets as it's removed the more intense will the missbehaviour of the next spring created be..

 

it seems that the state of the old spring (even if removed) influences the state of the newly created spring... it is longer and stiffer most of the time than the original..

you will see that creating the first spring (clicking on the player) will not move the player around much..  but after a few tries and throws creating a spring will push the player away..

Share this post


Link to post
Share on other sites

@spencerTL:  (and spring configurations)

 

your config would not work at all for me..  to long restlength (should be almost zero) and no stiffness at all.. i wouldn't be able to throw the player with this spring..  BUT

 

thanks to your input i started playing with the configs again and hey..  it seems i found a configuration that makes the described effect almost not reproducable:

game.physics.p2.createSpring(bodyA, bodyB, 0,20,0);   //bodyA, bodyB, restLength, stiffness, damping

after a little while of testing i found out that damping is the reason for this..  the higher the damping the more intense is the weirdness of the next spring..  so i reduced damping to 0 and yay! 

i need to give it more testing but for the moment it looks very good...  i also reduced the restlength to  0 but this didn't do anything - it's just the optimum for my game :)

 

EDIT: the only problem is that without damping my player gets insanely fast..  almost unplayable :(  so i need somesort of damping..

Share this post


Link to post
Share on other sites

I see the problem on your original example.  I think the reason I didn't see something similar is that 1)  mine didn't need to create and destroy springs 2) the two attached bodies were not player controlled just nudged by a player body.

 

I see from your example why you need a short rest length, I wonder if this is part of the problem.  I mean it shouldn't be a problem, but I can imagine that any tests for a spring would have had a longer rest length as that is the more typical spring, and something strange is happening by the bodies being so close to each other. The Phaser example has a rest length of 150, which is probably why I used it.  In yours the bodies never get to stop which is why I think the damping is so important.  Would you be able to apply damping/velocity changes to the bodies themselves in response to the spring to help compensate?  No doubt this is an inelegant hack but I think Springs are too flawed at the moment for elegance!

 

My worry was that I'd spend forever trying to hack around the bugs and then the next update of phaser has working p2 springs/box2d support and it would all be for nothing.  Luckily it was only the removal with the array that I needed to hack yours seems much more involved. 

 

My only real experience with complex physics engines/p2 was my last game so I'm afraid my help is not going to be the most useful, I don't fully understand quite a lot of it.

Share this post


Link to post
Share on other sites

One other thought, you use the removeSpring function which is definitely flawed in some respects, although it seems okay for a single spring, like in your case.  Maybe it is leaving something behind rather than properly removing the spring?

 

Maybe it would be worth a try just replacing this with the brute force:  

this.game.physics.p2.world.springs.splice(0,this.game.physics.p2.world.springs.length);  

and avoids using a function with known, albeit other, issues.

Share this post


Link to post
Share on other sites

so i tried your way to remove springs.. works as espected.. thx..   but the problem with the damping parameter remains..

 

setting damping to more than 0 produces a strange offset (feels like a longer restlength) between the two bodies the next time a spring is created..

the higher the damping the more intense the unwanted effect..

Share this post


Link to post
Share on other sites

I had high hopes for that method.  While looking through the p2 code I never saw anything that that looked like it would cause a destroyed spring to still have influence, their only reference is in that array, but to be fair, there's a lot within the code that was beyond me.

 

Another stab in the dark here, could the spring be passing on its properties to the two bodies and not clearing this up on destroy?  Each time you create a spring maybe try resetting the bodies' damping to their default (and probably all of their other properties too).

Share this post


Link to post
Share on other sites

this actually doesn't sound too dark ^^   i don't know how the damping to the body attached to the spring is applied.. maybe it is passed to the body ... the interesting thing here is that it normalizes itself over time (or a few clicks create/destroy springs) until it's is almost as "normal" like the very first spring created.. 

 

i'm now on vacation for a week but i will have a look into the bodies settings if i find something..  one other way i thought of was limiting the velocity of the bodies.. so no body can get faster than 400px for example...

 

i hope rich or schteppe have some insight on this.. i opened a github issue here:https://github.com/photonstorm/phaser/issues/1134

Share this post


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...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.