Skip to content

Instantly share code, notes, and snippets.

@elsherbini
Created May 16, 2013 16:40
Show Gist options
  • Save elsherbini/5593124 to your computer and use it in GitHub Desktop.
Save elsherbini/5593124 to your computer and use it in GitHub Desktop.
Sherwood's Bouncing Ball
{"description":"Sherwood's Bouncing Ball","endpoint":"","display":"div","public":true,"require":[{"name":"glowscript","url":"https://bitbucket.org/davidscherer/glowscript/raw/8af3ac25a24e733367472159f7f2e1a0ca276dbc/package/glow.1.0.min.js"},{"name":"compiler","url":"https://bitbucket.org/davidscherer/glowscript/raw/8af3ac25a24e733367472159f7f2e1a0ca276dbc/package/compiler.1.0.min.js"},{"name":"symbols","url":"https://bitbucket.org/davidscherer/glowscript/raw/8af3ac25a24e733367472159f7f2e1a0ca276dbc/package/symbols.1.0.min.js"}],"fileconfigs":{"inlet.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"config.json":{"default":true,"vim":false,"emacs":false,"fontSize":12},"_.md":{"default":true,"vim":false,"emacs":false,"fontSize":12}},"fullscreen":false,"play":false,"loop":false,"restart":false,"autoinit":true,"pause":true,"loop_type":"period","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01,"thumbnail":"http://i.imgur.com/ZdAXSIJ.png"}
var body = d3.select("#display");
body.append("div")
.attr("id", "glowscript")
.attr("class", "glowscript")
.style("margin-left","50px");
// A simple animation shows immediate effects of changing a GlowScript program.
// Button at bottom of this page was used to specify use of 3D GlowScript library.
// See glowscript.org for information on GlowScript.
// Change any parameter and see the effect immediately.
// If you select a number, a scroll bar lets you change the number.
window.__context = { glowscript_container: $("#glowscript").removeAttr("id") }
var scene = canvas()
scene.title.text("A ball bounces in a box")
var s = "In GlowScript programs:\n"
s += " Rotate the camera by dragging with right button\n"
s += " Zoom with left+right mouse buttons, or mouse wheel"
scene.caption.text(s)
var side = 4
var thk = 0.3
var s2 = 2*side - thk
var s3 = 2*side + thk
var wallR = box ( {pos:vec( side, 0, 0), size:vec(thk,s2,s3), color : color.red} )
var wallL = box ( {pos:vec(-side, 0, 0), size:vec(thk,s2,s3), color : color.red} )
var wallB = box ( {pos:vec(0, -side, 0), size:vec(s3,thk,s3), color : color.blue} )
var wallT = box ( {pos:vec(0, side, 0), size:vec(s3,thk,s3), color : color.blue} )
var wallBK = box( {pos:vec(0, 0, -side), size:vec(s2,s2,thk), color : color.gray(0.7)} )
var ball = sphere ( {color : color.green, size : vec(0.8,0.8,0.8)} )
ball.mass = 1.0
ball.p = vec (-0.15, -0.23, +0.27)
attach_trail(ball, {pps:200, retain:100})
side = side - thk*0.5 - ball.size.x/2
var dt = 0.3
function move() {
rate(200,move) // execute the move function about 200 times per second
ball.pos = ball.pos.add(ball.p.multiply(dt/ball.mass))
if (! (-side < ball.pos.x && ball.pos.x < side)) {
ball.p.x = -ball.p.x
}
if (! (-side < ball.pos.y && ball.pos.y < side)) {
ball.p.y = -ball.p.y
}
if (! (-side < ball.pos.z && ball.pos.z < side)) {
ball.p.z = -ball.p.z
}
}
move() // Execute move to start the repetition
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment