PhysicsI

by Simon Raper

JavaScript

//constants
var g = 0.981;
var initXVel = 5;
var initYVel = 30;
var t = 0,
    x = 0,
    y = 0;

//Set up svg
var svg = d3.select("body").append("svg").attr("width", 500)
    .attr("height", 500);


//Create adaptable scales
var xScale = d3.scale.linear()
    .domain([0, 300])
    .range([10, 480]);
var yScale = d3.scale.linear()
    .domain([0, 500])
    .range([480, 10]);


//Set up projectile
svg.append("circle").attr("class", "ball")
.attr("cx", function () {
    return xScale(x);
})
.attr("cy", function () {
    return yScale(y);
})
.attr("r", 5);


//Loop for time

//setTimeout(
//    function(){
    console.log(x, y, t);
    x = t * initXVel;    
    y = t * initYVel - 0.5 * g * t * t;
    d3.select(".ball").attr("cx", function () {
        //console.log(xScale(x));
        return xScale(x);
    })
    .attr("cy", function () {
        //console.log(yScale(y));
        return yScale(y);
    });
    t=t+1;
    }
//}, 1000);