Jump to content

is there a proper way to limit the speed of a p2 body?


valueerror
 Share

Recommended Posts

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/

 

Regards

George

 
 
 
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

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...
 Share

  • Recently Browsing   0 members

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