-
Notifications
You must be signed in to change notification settings - Fork 0
/
Linear_Damping.js
63 lines (50 loc) · 1.88 KB
/
Linear_Damping.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Author: Austin Bearden
// Date Created: 5/4/2018
// Description: Linear Damping
// Organization: PhysicsFoosball
// Filename: Linear_Damping.js
//execute calcualtions when button is clicked
function calculateAll() {
// input values:
// 1) damping value
// 2) initial velocity
//get values from input boxes
var damping_val = parseFloat(document.getElementById('dampingVal').value);
console.log(damping_val);
var init_velocity = parseFloat(document.getElementById('initVel').value);
console.log(init_velocity);
// build the world
var world = new CANNON.World();
world.gravity.set(0, 0, -9.82); // m/s^2
// build a ball object
// give the ball a shape
var radius = 1;
var ball = new CANNON.Body({
mass: 5,
position: new CANNON.Vec3(0, 0, 0),
shape: new CANNON.Sphere(radius),
velocity: new CANNON.Vec3(init_velocity, init_velocity, init_velocity) // velocity in m / s
});
ball.linearDamping = damping_val; // linear damping
// add the ball to my world
world.addBody(ball);
// create the plane for the world
var ground = new CANNON.Body({
mass: 0 // mass == 0 makes body static
// but what does it mean to have a Static body
});
var ground_shape = new CANNON.Plane();
// define the plane's shape
ground.addShape(ground_shape);
// add plane to world
world.addBody(ground);
var initial_velocity = ball.velocity.x;
console.log("BV: " + initial_velocity);
world.step(1/5); // 0.5 seconds
var current_velocity = ball.velocity.x;
console.log("AV: " + current_velocity);
document.getElementById("newVel").innerHTML = "Current Velocity: " + current_velocity + " meters per second";
}
//Accesses button from html
var calculate = document.getElementById("calculate");
calculate.addEventListener("click", function() { calculateAll(); } );