valueerror Posted October 15, 2014 Share Posted October 15, 2014 there are several occations where my player gets faster and faster all the time without limit.. (this is on purpose but it shouldn't end up in lightspeed.. a velocity of 800-1000 would be more than enough) how would i define such a limit and make sure it's applied? thx in advance Link to comment Share on other sites More sharing options...
valueerror Posted October 16, 2014 Author 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 and here is the code: function constrainVelocity(sprite, maxVelocity) { var body = sprite.body var angle, currVelocitySqr, vx, vy; vx = body.data.velocity[0]; vy = body.data.velocity[1]; currVelocitySqr = vx * vx + vy * vy; if (currVelocitySqr > maxVelocity * maxVelocity) { angle = Math.atan2(vy, vx); vx = Math.cos(angle) * maxVelocity; vy = Math.sin(angle) * maxVelocity; body.data.velocity[0] = vx; body.data.velocity[1] = vy; console.log('limited speed to: '+maxVelocity); }}; constrainVelocity(sprite, 200); Link to comment Share on other sites More sharing options...
Wavertron Posted October 16, 2014 Share Posted October 16, 2014 Handy little method that. Good to know if I need in future. Link to comment Share on other sites More sharing options...
Recommended Posts