Go Back
Lesson 2 || free fall simulation
Getting Started
- make a new repl html, css, js project
- copy the lesson 2
index.html, script.js, and style.css code into the repl
- hit run on the repl, you should see a bouncing ball if you scroll down.
- feel free to delete the directions in the index.html file, since they are already on this page.
- in script.js, read the comment on model view control
Changing Initialization Variables
- go to the "//ball physics initialization" comment
- try changing x to canvas.width/4, and re run the repl. what happens?
- try changing vx to -2 and re run the repl. what happens?
- mess with the other ball physics initialization variables. put them back to normal when you are done.
- i wouldnt mess with the environment initialization variables, or time initialization.
- the only one worth messing with is simSpeed, try changing it to .05
Understanding View and Model
- The view() function clears the canvas element every frame to delete the last frame.
- Delete the line, ctx.clearRect(0, 0, canvas.width, canvas.height). re run the repl. what happens?
- put the line back
- the viewBall() function draws the ball on the screen.
- the model() function calculates the change in time from the last frame, and passes the change in time into
modelBall
modelBall()
- Model ball has an if statement for bouncing off of the floor, one for the left wall, and one for the right
wall.
- The left wall and right wall conditions flip the balls horizontal velocity on impact.
- The floor bounce sets the balls vertical velocity to an exact value, otherwise computational rounding makes
the ball bounce higher and higher.
- If you have gotten this far, see the control() function. in repl, hit the S key and see what happens.
- Try hooking up the other controls in the control function().
Wrapping up.
- on repl, hit the github branch icon , commit, and push
your
changes
to a new repo named lesson2.
- In github, you can go to the settings of your new lesson2 repo, github pages, choose main. Enforce https.
- go to yourusername.github.io/lesson2 and see if its up.
- you can do this for any github repository :)