Limiting maximum speed of an object

Hi all,

I’ve decided to revisit my asteroidyish game that I was noodling around with a few months ago and see if I can make it worth releasing.

Anyway I have the following code that is called when the player ‘thrusts’…

if player.thrust then local vecX, vecY = angle2VecDeg( player.rotation-90 ) player:applyForce ( vecX, vecY, player.x, player.y ) end

The only problem is that if the player holds the thrust button down the ship will just keep accelerating way past the point where it’s playable.  I’m going to implement a feature where the player can select from a number of ships to control which have differing weight, speed, fire power etc.

So what I’m wanting to know is if there is a way of putting a cap on the maximum speed the ship can move.

Hope this makes sense.

Cheers

Chris

If you have SSK2 lite or pro, it comes with a feature to ‘limit’ the velocity of an object:

https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/actions1/#moveplimitv

function player.enterFrame( self ) -- Accelerate this quickly using 500 units of force, but -- limit the maximum velocity to 750 pixels-per-second ssk.actions.movep.thrustForward( self, { rate = 500 } ) ssk.actions.movep.limitV( self, { rate = 750 } ) end

If you don’t have SSK2, the pseudo-code to limit/cap a velocity is:

Every Frame (AFTER applying forces)

  1. Get object velocity as vector <vx, vy>
  2. Calculate length of vector
  3. Compare length to ‘maxRate’
  4. If less than maxRate, done, else continue
  5. Normalize vector 
  6. Scale vector by maxRate
  7. Set velocity using new vector

Oooh that’s fantastic.  I never even thought of checking SSK2 as I originally started writing this before I bought it.

Looks like I may have to refactor the code to bring it online :slight_smile:

Many thanks Ed.

[edit]

I love this place.  That response was so speedy Ed must have been typing the answer to this while I was still typing the question.

If you have SSK2 lite or pro, it comes with a feature to ‘limit’ the velocity of an object:

https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/actions1/#moveplimitv

function player.enterFrame( self ) -- Accelerate this quickly using 500 units of force, but -- limit the maximum velocity to 750 pixels-per-second ssk.actions.movep.thrustForward( self, { rate = 500 } ) ssk.actions.movep.limitV( self, { rate = 750 } ) end

If you don’t have SSK2, the pseudo-code to limit/cap a velocity is:

Every Frame (AFTER applying forces)

  1. Get object velocity as vector <vx, vy>
  2. Calculate length of vector
  3. Compare length to ‘maxRate’
  4. If less than maxRate, done, else continue
  5. Normalize vector 
  6. Scale vector by maxRate
  7. Set velocity using new vector

Oooh that’s fantastic.  I never even thought of checking SSK2 as I originally started writing this before I bought it.

Looks like I may have to refactor the code to bring it online :slight_smile:

Many thanks Ed.

[edit]

I love this place.  That response was so speedy Ed must have been typing the answer to this while I was still typing the question.