139 lines
5.5 KiB
HTML
139 lines
5.5 KiB
HTML
<!--
|
|
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>
|
|
Problem 3-8
|
|
</title>
|
|
<style>
|
|
html, body {
|
|
width: 100%;
|
|
height: 100%;
|
|
margin: 0px;
|
|
border: 0;
|
|
overflow: hidden;
|
|
display: block;
|
|
}
|
|
|
|
canvas {
|
|
position: absolute;
|
|
}
|
|
</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>
|
|
|
|
<script>
|
|
var c = document.getElementById("myCanvas");
|
|
var ctx = c.getContext("2d");
|
|
c.width = window.innerWidth;
|
|
c.height = window.innerHeight;
|
|
//Time step
|
|
var dt = 0.002;
|
|
var t = 0;
|
|
var g = 200;
|
|
var mPlane = 0.5;
|
|
var mBlock = 1;
|
|
var th = Math.PI / 6;
|
|
var planeLength = 100;
|
|
var blockSideLength = 10;
|
|
var planePosition = 0;
|
|
var blockPositionX = 0;
|
|
var blockPositionY = 0;
|
|
var planeVelocity = 0;
|
|
var blockVelocityX = 0;
|
|
var blockVelocityY = 0;
|
|
var planeAcceleration = 0;
|
|
var blockAccelerationX = 0;
|
|
var blockAccelerationY = 0;
|
|
calcAccelerations();
|
|
//Relative position of canvas
|
|
var xPos = 0;
|
|
var yPos = 0;
|
|
//Moves and scales canvas
|
|
ctx.translate(ctx.canvas.width / 2, ctx.canvas.height / 2)
|
|
ctx.scale(1, -1);
|
|
|
|
function calcAccelerations()
|
|
{
|
|
planeAcceleration = -mBlock * g * Math.sin(th) * Math.cos(th) / (mPlane + mBlock * Math.sin(th) * Math.sin(th));
|
|
blockAccelerationX = -planeAcceleration * mPlane / mBlock;
|
|
blockAccelerationY = -g - (mPlane * planeAcceleration * Math.cos(th)) / (mBlock * Math.sin(th));
|
|
}
|
|
|
|
function Reset()
|
|
{
|
|
planePosition = 0;
|
|
blockPositionX = 0;
|
|
blockPositionY = 0;
|
|
planeVelocity = 0;
|
|
blockVelocityX = 0;
|
|
blockVelocityY = 0;
|
|
calcAccelerations();
|
|
}
|
|
Reset();
|
|
|
|
function Clear(ctx)
|
|
{
|
|
ctx.clearRect(-c.width, -c.height, c.width * 2, c.height * 2);
|
|
}
|
|
|
|
function Update()
|
|
{
|
|
Clear(ctx);
|
|
Draw();
|
|
t += dt;
|
|
if (t > 0.7)
|
|
{
|
|
//Reset();
|
|
}
|
|
planeVelocity += planeAcceleration * dt;
|
|
planePosition += planeVelocity * dt;
|
|
blockVelocityX += blockAccelerationX * dt;
|
|
blockVelocityY += blockAccelerationY * dt;
|
|
blockPositionX += blockVelocityX * dt;
|
|
blockPositionY += blockVelocityY * dt;
|
|
setTimeout(Update, 1000/60);
|
|
}
|
|
|
|
function Draw()
|
|
{
|
|
ctx.beginPath();
|
|
ctx.moveTo(-c.width, yPos);
|
|
ctx.lineTo(c.width, yPos);
|
|
ctx.stroke();
|
|
ctx.beginPath();
|
|
ctx.moveTo(planePosition, yPos);
|
|
ctx.lineTo(planePosition, yPos + Math.tan(th) * planeLength);
|
|
ctx.lineTo(planePosition + planeLength, yPos);
|
|
ctx.stroke();
|
|
ctx.beginPath();
|
|
ctx.moveTo(xPos + blockPositionX + planeLength - 90, yPos + blockPositionY + 90 * Math.tan(th));
|
|
ctx.lineTo(xPos + blockPositionX + planeLength - 90 + blockSideLength * Math.cos(Math.PI / 2 - th), yPos + blockPositionY + 90 * Math.tan(th) + blockSideLength * Math.sin(Math.PI / 2 - th));
|
|
ctx.lineTo(xPos + blockPositionX + planeLength - 90 + blockSideLength * (Math.cos(Math.PI / 2 - th) + Math.cos(th)), yPos + blockPositionY + 90 * Math.tan(th) + blockSideLength * (Math.sin(Math.PI / 2 - th) - Math.sin(th)));
|
|
ctx.lineTo(xPos + blockPositionX + planeLength - 90 + blockSideLength * Math.cos(th), yPos + blockPositionY + 90 * Math.tan(th) - blockSideLength * Math.sin(th));
|
|
ctx.lineTo(xPos + blockPositionX + planeLength - 90, yPos + blockPositionY + 90 * Math.tan(th));
|
|
ctx.stroke();
|
|
|
|
}
|
|
|
|
|
|
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> |