Physics module - Lucas-Code27/Canvas-plus-plus GitHub Wiki

How to Setup

[!IMPORTANT] The physics system is currently just a library of collision detection functions and is not a full featured physics engine like Box2D yet

First you have to import the module

import * as physics from "/canvas++/modules/physics.js"

Then you can put the collision function into an if statement and fill out the arguments

if (physics.rectvrectcollision(Px,520,60,40,Cx1,Cy1,20,20)){
  Cy1 = 0
  Cx1 = random.randi(380)
  Score++
  coinsound.play()
}

The arguments go as follows:

  • r1x - First rectangle's x position
  • r1y - First rectangle's y position
  • r1w - First rectangle's width
  • r1h - First rectangle's height
  • r2x - Second rectangle's x position
  • r2y - Second rectangle's y position
  • r2w - Second rectangle's width
  • r2h - Second rectangle's height

[!IMPORTANT] You can find what the functions do here as the functions are directly ported from this article: https://www.jeffreythompson.org/collision-detection/table_of_contents.php