Fixing things and adding springPendulum
This commit is contained in:
152
springPendulum.html
Normal file
152
springPendulum.html
Normal file
@@ -0,0 +1,152 @@
|
||||
<!--
|
||||
This work is licensed under CC BY-NC-ND 4.0
|
||||
Link to license: http://creativecommons.org/licenses/by-nc-nd/4.0/
|
||||
Attribute to Russell Georgi
|
||||
-->
|
||||
<html>
|
||||
<head>
|
||||
<title>
|
||||
Spring Pendulum Example
|
||||
</title>
|
||||
<style>
|
||||
html, body {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
margin: 0px;
|
||||
border: 0;
|
||||
overflow: hidden;
|
||||
display: block;
|
||||
}
|
||||
|
||||
canvas {
|
||||
position: absolute;
|
||||
}
|
||||
|
||||
t {
|
||||
position: absolute;
|
||||
font-size: 75px;
|
||||
left: 5%;
|
||||
top: 5%;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<canvas id="myCanvas" width="1" height="1" style="border:1px solid #ffffff;">
|
||||
Your browser does not support the HTML5 canvas tag.</canvas>
|
||||
<!--t id="time">0</t-->
|
||||
|
||||
<script>
|
||||
var c = document.getElementById("myCanvas");
|
||||
var ctx = c.getContext("2d");
|
||||
c.width = window.innerWidth;
|
||||
c.height = window.innerHeight;
|
||||
//Time step
|
||||
var dt = 0.02;
|
||||
var t = 0;
|
||||
var g = 98;
|
||||
var l = 200;
|
||||
var x = 0;
|
||||
var xp = 0;
|
||||
var theta = Math.PI / 8;
|
||||
var thetap = 0;
|
||||
var m = 1;
|
||||
var k = 2;
|
||||
//Relative position of canvas
|
||||
var xPos = 0;
|
||||
var yPos = 200;
|
||||
//Moves and scales canvas
|
||||
ctx.translate(ctx.canvas.width / 2, ctx.canvas.height / 2)
|
||||
ctx.scale(1, -1);
|
||||
|
||||
function Clear(ctx)
|
||||
{
|
||||
ctx.clearRect(-c.width, -c.height, c.width * 2, c.height * 2);
|
||||
}
|
||||
|
||||
function Update()
|
||||
{
|
||||
Clear(ctx);
|
||||
Draw();
|
||||
t += dt;
|
||||
xp += (m * (l + x) * thetap * thetap + m * g * Math.cos(theta) - k * x) * dt / m;
|
||||
thetap += (-m * g * Math.sin(theta) - 2 * m * xp * thetap) * dt / (m * (l + x));
|
||||
x += xp * dt;
|
||||
theta += thetap * dt;
|
||||
setTimeout(Update, 1);
|
||||
}
|
||||
|
||||
function Reset()
|
||||
{
|
||||
theta = 0;
|
||||
x = 0;
|
||||
thetap = 0;
|
||||
xp = 0;
|
||||
t = 0;
|
||||
}
|
||||
|
||||
function Draw()
|
||||
{
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(-c.width, yPos);
|
||||
ctx.lineTo(c.width, yPos);
|
||||
ctx.stroke();
|
||||
drawSpring(xPos, 0, xPos + (l + x) * Math.sin(theta), - (l + x) * Math.cos(theta), 1, 3, 10, 10);
|
||||
ctx.beginPath();
|
||||
ctx.arc(xPos + (l + x) * Math.sin(theta), yPos - (l + x) * Math.cos(theta), 10, 0, Math.PI * 2);
|
||||
ctx.fill();
|
||||
}
|
||||
|
||||
function drawSpring(x1, y1, x2, y2, a, b, perpSize, numLoops, tStep = 0.1)
|
||||
{
|
||||
ctx.translate(0, yPos);
|
||||
if (Math.sign(theta) == 1)
|
||||
{
|
||||
ctx.rotate(Math.atan((y2 - y1) / (x2 - x1)) * (Math.sign(theta)));
|
||||
} else
|
||||
{
|
||||
ctx.rotate(Math.PI - Math.atan((y2 - y1) / (x2 - x1)) * (Math.sign(theta)));
|
||||
}
|
||||
var xSize = Math.sqrt(((x1 - x2) ** 2 + (y1 - y2) ** 2));
|
||||
var tStart = Math.PI / 2;
|
||||
var tEnd = (3 * Math.PI / 2) + (2 * Math.PI * numLoops);
|
||||
var xEnd = a * tEnd - b * Math.sin(tEnd);
|
||||
var yEnd = a + b;
|
||||
var xFactor = xSize / xEnd;
|
||||
var yFactor = perpSize / yEnd;
|
||||
var xShift = x1 - a * (Math.PI / 2) + b * Math.sin(Math.PI / 2);
|
||||
var yShift = y1 - a + b * Math.cos(Math.PI / 2);
|
||||
for (p = tStart; p <= tEnd; p += tStep)
|
||||
{
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(xShift + xFactor * (a * p - b * Math.sin(p)), yShift + yFactor * (a - b * Math.cos(p)));
|
||||
ctx.lineTo(xShift + xFactor * (a * (p + tStep) - b * Math.sin(p + tStep)), yShift + yFactor * (a - b * Math.cos(p + tStep)));
|
||||
ctx.stroke();
|
||||
}
|
||||
if (Math.sign(theta) == 1)
|
||||
{
|
||||
console.log("sign = 1");
|
||||
ctx.rotate(-Math.atan((y2 - y1) / (x2 - x1)) * (Math.sign(theta)));
|
||||
} else
|
||||
{
|
||||
console.log("sign = -1");
|
||||
ctx.rotate(-Math.PI + Math.atan((y2 - y1) / (x2 - x1)) * (Math.sign(theta)));
|
||||
}
|
||||
ctx.translate(0, -yPos);
|
||||
}
|
||||
|
||||
|
||||
window.addEventListener('resize', function(event) {
|
||||
c.width = window.innerWidth;
|
||||
c.height = window.innerHeight;
|
||||
ctx.translate(c.width / 2, c.height / 2);
|
||||
ctx.scale(1, -1);
|
||||
}, true);
|
||||
|
||||
Update();
|
||||
|
||||
</script>
|
||||
</body>
|
||||
<p xmlns:cc="http://creativecommons.org/ns#" style="font-size: 1vw; bottom: 0px; position: absolute;">
|
||||
This work is licensed under
|
||||
<a href="http://creativecommons.org/licenses/by-nc-nd/4.0/?ref=chooser-v1" target="_blank" rel="license noopener noreferrer" style="display:inline-block;">CC BY-NC-ND 4.0<img style="height:22px!important;margin-left:3px;vertical-align:text-bottom;" src="https://mirrors.creativecommons.org/presskit/icons/cc.svg?ref=chooser-v1"><img style="height:22px!important;margin-left:3px;vertical-align:text-bottom;" src="https://mirrors.creativecommons.org/presskit/icons/by.svg?ref=chooser-v1"><img style="height:22px!important;margin-left:3px;vertical-align:text-bottom;" src="https://mirrors.creativecommons.org/presskit/icons/nc.svg?ref=chooser-v1"><img style="height:22px!important;margin-left:3px;vertical-align:text-bottom;" src="https://mirrors.creativecommons.org/presskit/icons/nd.svg?ref=chooser-v1"></a></p>
|
||||
</html>
|
||||
Reference in New Issue
Block a user