Skip to content

Instantly share code, notes, and snippets.

@jfraboni
Last active February 12, 2019 01:49
Show Gist options
  • Save jfraboni/1a308c86b8f328db5e0c447a8c7aaa29 to your computer and use it in GitHub Desktop.
Save jfraboni/1a308c86b8f328db5e0c447a8c7aaa29 to your computer and use it in GitHub Desktop.
Snippet: Method to update the diagonal velocity of a body
/**
* Updates the diagonal velocity properties of a body,
* taking into account the body's current velocity
* and applying any forces acting against the body
* as acceleration on both the x and y axis.
*
* NOTE: This method DOES NOT update the position of
* the body, it only updates its velocity.
*
* @param {Object} body: The body must be an Object
* with velocityX, velocityY and rotation properties.
* @param {Number} forceOnX: The force acting against
* the body on the x axis.
* @param {Number} forceOnY: The force acting against
* the body on the y axis.
*/
updateVelocity(body, forceOnX, forceOnY) {
const
angle = body.rotation * Math.PI / 180,
accelerationOnX = Math.cos(angle) * forceOnX,
accelerationOnY = Math.sin(angle) * forceOnY;
body.velocityX += accelerationOnX;
body.velocityY += accelerationOnY;
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment