joshmarinacci / webxr-workshops

Modular workshop series for teaching WebXR (VR & AR)
MIT License
55 stars 2 forks source link

physics - suggested changes to Pong Game section #34

Closed NickPax closed 6 years ago

NickPax commented 6 years ago

Suggested text after "Add an event handler to run when the scene fires the loaded event.":

We need the help of JavaScript again. Near the bottom of the file you should see this code within a couple of <script> </script> tags:

on($('a-scene'),'loaded',()=>{

})

Add these lines between the curly braces:

const pos = ball.getComputedAttribute('position')

    //start the ball moving
    ball.body.applyImpulse(
      new CANNON.Vec3(0, 0, -50),
      new CANNON.Vec3().copy(pos)
      )

That script code should now look something like this:

<script>
      const $ = (sel) => document.querySelector(sel)
      const on = (el,type,cb) => el.addEventListener(type,cb)
on($('a-scene'),'loaded',()=>{
  const pos = ball.getComputedAttribute('position')

    //start the ball moving
    ball.body.applyImpulse(
      new CANNON.Vec3(0, 0, -50),
      new CANNON.Vec3().copy(pos)
      )
  })
</script>

Note: Josh, I'm loving this! Learning about physics and A-Frame here. I appreciate your style. Glitch is great for this! Thank you

joshmarinacci commented 6 years ago

good point. updated.