Taleforge Posted March 13, 2014 Share Posted March 13, 2014 Hi, I want to limit the Speed of a sprite (P2JS enabled), wich is accelerated bysprite.body.thrust(400);and slowed bysprite.body.reverse(400)Its simply like the new P2 - Thrust-Example. where you control a spaceship. How can I limit the speed of that sprite?If the player keeps accelerating the speed will go insane! Link to comment Share on other sites More sharing options...
Local_Minimum Posted May 1, 2014 Share Posted May 1, 2014 I hit the same wall trying to migrate from ARCADE to P2 physics. I made a working solution for speed capping for want of better solution. I put into a separate function so that all my P2JS-bodies can check their speeds with it:var limitSpeedP2JS = function(p2Body, maxSpeed) { var x = p2Body.velocity.x; var y = p2Body.velocity.y; if (Math.pow(x, 2) + Math.pow(y, 2) > Math.pow(maxSpeed, 2)) { var a = Math.atan2(y, x); x = -20 * Math.cos(a) * maxSpeed; y = -20 * Math.sin(a) * maxSpeed; p2Body.velocity.x = x; p2Body.velocity.y = y; } return p2Body;}I have absoluteley no clue what the -20 magic number is about, but it was needed for my game to limit the speeds correctly. It might be related to masses, gravity, drag, damping, or something else entirely. Hope that works for you as well. Link to comment Share on other sites More sharing options...
rich Posted May 1, 2014 Share Posted May 1, 2014 In P2 distances are measured in meters, not pixels. 20 is the conversion value Phaser uses (you can actually over-ride it via your game config, but that's what the default is). Link to comment Share on other sites More sharing options...
Local_Minimum Posted May 1, 2014 Share Posted May 1, 2014 Thanks for clarifying both the number and the reason, it explains why all my constants felt wrong after migrating to P2. Link to comment Share on other sites More sharing options...
valueerror Posted October 15, 2014 Share Posted October 15, 2014 how do i use this function? i placed it in the update loop and gave it my p2 body and a speed limit of 200 and the body is jumping around like crazy.. it's not working at all Link to comment Share on other sites More sharing options...
george Posted October 16, 2014 Share Posted October 16, 2014 I use a limiting function inside the update loop. The correct way would be to place the limiting function somewhere inside p2's substep loop which is called multiple times (10 by default) during a single render update. The constraining happens far too late in the update loop and that's why you might run into tunneling or some kind of jittering especially during lower frame rates. It should be fine in most cases so it's worth a try. Here a small example based on the mentioned thrust example:http://jsfiddle.net/sh036s95/ RegardsGeorge Link to comment Share on other sites More sharing options...
valueerror Posted October 16, 2014 Share Posted October 16, 2014 thank you george... your jsfiddle example works perfectly in my game.. i don't know what exactly makes the difference but it works Link to comment Share on other sites More sharing options...
Recommended Posts