Мещерский 48.15 — различия между версиями
Материал из Department of Theoretical and Applied Mechanics
(→Код программы) |
(→Код программы) |
||
Строка 12: | Строка 12: | ||
<syntaxhighlight lang="javascript" line start="1" enclose="div"> | <syntaxhighlight lang="javascript" line start="1" enclose="div"> | ||
var renderer, scene, camera, stats, axes; | var renderer, scene, camera, stats, axes; | ||
− | var control, controls, controls1, spotLight; | + | var control, controls, controls1, spotLight; var dt = 1/60; |
− | |||
− | |||
var g = 9.8; | var g = 9.8; | ||
var length = 30; | var length = 30; | ||
var cubeY = 25; | var cubeY = 25; | ||
− | |||
function init() | function init() | ||
Строка 30: | Строка 27: | ||
renderer.shadowMap.enabled=true; | renderer.shadowMap.enabled=true; | ||
− | axes = new THREE.AxisHelper(20); | + | axes = new THREE.AxisHelper(20); |
scene.add(axes); | scene.add(axes); | ||
Строка 37: | Строка 34: | ||
control = new THREE.OrbitControls(camera,renderer.domElement); | control = new THREE.OrbitControls(camera,renderer.domElement); | ||
− | controls = new function() | + | controls = new function() |
{ | { | ||
this.alpha = Math.PI/6; | this.alpha = Math.PI/6; | ||
Строка 51: | Строка 48: | ||
} | } | ||
− | controls1=new function() | + | controls1=new function() |
{ | { | ||
this.t = 0.0; | this.t = 0.0; | ||
Строка 57: | Строка 54: | ||
} | } | ||
− | var gui = new dat.GUI(); | + | var gui = new dat.GUI(); |
gui.add(controls,'alpha', 0.1,Math.PI/3).onChange(controls.reset); | gui.add(controls,'alpha', 0.1,Math.PI/3).onChange(controls.reset); | ||
gui.add(controls,'m1',1,20).onChange(controls.reset); | gui.add(controls,'m1',1,20).onChange(controls.reset); | ||
Строка 93: | Строка 90: | ||
scene.add(plane1); | scene.add(plane1); | ||
− | + | ||
var planeGeometry2 = new THREE.PlaneGeometry(length,20,6,4); | var planeGeometry2 = new THREE.PlaneGeometry(length,20,6,4); | ||
var planeMaterial2 = new THREE.MeshLambertMaterial({color: 0x6F482A, wireframe: true}); | var planeMaterial2 = new THREE.MeshLambertMaterial({color: 0x6F482A, wireframe: true}); |
Версия 11:37, 22 декабря 2017
Задача: С помощью языка программирования JavaScript смоделировать колебания маятника, точка подвеса которого движется по заданному закону.
Выполнил: Троцкая Валерия, 23632/2
Решение
Код программы
Текст программы на языке JavaScript:
1 var renderer, scene, camera, stats, axes;
2 var control, controls, controls1, spotLight; var dt = 1/60;
3 var g = 9.8;
4 var length = 30;
5 var cubeY = 25;
6
7 function init()
8 {
9 scene = new THREE.Scene(); // создаем сцену
10 camera = new THREE.PerspectiveCamera(45,window.innerWidth/window.innerHeight,0.1,1000); // создаем камеру
11
12 renderer = new THREE.WebGLRenderer();
13 renderer.setClearColor(0XEEEEEE,1);
14 renderer.setSize(window.innerWidth,window.innerHeight);
15 renderer.shadowMap.enabled=true;
16
17 axes = new THREE.AxisHelper(20);
18 scene.add(axes);
19
20
21
22 control = new THREE.OrbitControls(camera,renderer.domElement);
23
24 controls = new function()
25 {
26 this.alpha = Math.PI/6;
27 this.blockRadius = 3;
28 this.m1 = 3;
29 this.m2 = 1;
30 this.animate = false;
31 this.showAcs = false;
32 this.reset = function() {
33 controls1.t = 0;
34 ReDraw();
35 }
36 }
37
38 controls1=new function()
39 {
40 this.t = 0.0;
41 this.a = (controls.m1*g*Math.sin(controls.alpha)-controls.m2*g)/(2*controls.m1+controls.m2);
42 }
43
44 var gui = new dat.GUI();
45 gui.add(controls,'alpha', 0.1,Math.PI/3).onChange(controls.reset);
46 gui.add(controls,'m1',1,20).onChange(controls.reset);
47 gui.add(controls,'m2',1,20).onChange(controls.reset);
48 gui.add(controls, 'animate');
49 gui.add(controls, 'showAcs').onChange(ReDraw);
50 gui.add(controls, 'reset');
51 //gui.add(controls,'m',1,10).onChange(ReDraw);
52 gui.add(controls1, 't').listen();
53 gui.add(controls1, 'a').listen();
54
55 ambientLight=new THREE.AmbientLight(0x000000);
56 scene.add(ambientLight);
57 document.getElementById("WebGL").appendChild(renderer.domElement);
58 // Camera
59 camera.position.x = -30;
60 camera.position.y = 40;
61 camera.position.z = 80;
62 camera.lookAt(scene.position);
63 // Ligth
64 spotLight = new THREE.SpotLight( 0xffffff );
65 spotLight.position.set( -40, 80, 50 );
66 spotLight.castShadow = true;
67 scene.add(spotLight );
68
69 // Main Plane
70 var planeGeometry2 = new THREE.PlaneGeometry(length,20);
71 var planeMaterial2 = new THREE.MeshLambertMaterial({color: 0x6F482A, side: THREE.DoubleSide});
72 plane1 = new THREE.Mesh(planeGeometry2,planeMaterial2);
73 plane1.rotation.x = -0.5*Math.PI;
74 plane1.position.x = -length/2;
75 plane1.position.y = 0;
76 plane1.position.z = 0;
77 plane1.receiveShadow = true;
78 scene.add(plane1);
79
80
81 var planeGeometry2 = new THREE.PlaneGeometry(length,20,6,4);
82 var planeMaterial2 = new THREE.MeshLambertMaterial({color: 0x6F482A, wireframe: true});
83 plane2 = new THREE.Mesh(planeGeometry2,planeMaterial2);
84 plane2.rotation.x = -0.5*Math.PI;
85 plane2.position.set(length/2,0,0);
86 scene.add(plane2);
87
88 stats = initStats();
89 Draw();
90 renderScene();
91
92 window.addEventListener('resize',onResize,false);
93 };
94
95 function ReDraw() // функция, перерисовывающая всю картинку
96 {
97 controls1.a = (controls.m1*g*Math.sin(controls.alpha)-controls.m2*g)/(2*controls.m1+controls.m2);
98 scene.remove(block1);
99 scene.remove(block2);
100 scene.remove(cube);
101 scene.remove(plane3);
102 scene.remove(torus);
103 scene.remove(cr1);
104 scene.remove(cr2);
105 scene.remove(cr3);
106 scene.remove(arr1);
107 scene.remove(arr2);
108 scene.remove(arr3);
109 scene.remove(arr4);
110 Draw();
111 }
112
113 function Draw()
114 {
115
116 // Second Plane
117 var planeGeometry3 = new THREE.PlaneGeometry(length/Math.cos(controls.alpha),20);
118 var planeMaterial3 = new THREE.MeshLambertMaterial({color: 0x6F482A, wireframe: false, side: THREE.DoubleSide});
119 plane3 = new THREE.Mesh(planeGeometry3,planeMaterial3);
120 plane3.rotation.set(-0.5*Math.PI,-controls.alpha,0);
121 plane3.position.set(length/2,0.5*length*Math.tan(controls.alpha),0);
122 plane3.receiveShadow = true;
123 scene.add(plane3);
124 // Block1
125 block1 = new THREE.Mesh(new THREE.CylinderGeometry(controls.blockRadius,controls.blockRadius,3,32), new THREE.MeshLambertMaterial({color: 0xA8A8A8, wireframe: false, side: THREE.DoubleSide}));
126 block1.rotation.set(-0.5*Math.PI,0,0);
127 block1.position.set(length,length*Math.tan(controls.alpha),0);
128 block1.castShadow = true;
129 scene.add(block1);
130 // Block2
131 block2 = new THREE.Mesh(new THREE.CylinderGeometry(controls.blockRadius,controls.blockRadius,3,32), new THREE.MeshLambertMaterial({color: 0xA8A8A8, wireframe: false, side: THREE.DoubleSide}));
132 block2.rotation.set(-0.5*Math.PI,0,0);
133 block2.position.x = length/2-controls1.a*controls1.t*controls1.t*Math.cos(controls.alpha)*0.5;
134 block2.position.y = block2.position.x*Math.tan(controls.alpha)+controls.blockRadius/Math.cos(controls.alpha);
135 block2.castShadow = true;
136 scene.add(block2);
137 // Thorus
138 torus = new THREE.Mesh( new THREE.TorusGeometry( 6, 0.1, 16, 100, controls.alpha ), new THREE.MeshBasicMaterial( { color: 0xff0000 } ) );
139 torus.position.set(0,0,9.9)
140 scene.add(torus);
141 // Cube
142 cube = new THREE.Mesh(new THREE.BoxGeometry(4, 4, 4), new THREE.MeshLambertMaterial({color: 0x7CA05A, wireframe: false}));
143 cube.position.set(length+controls.blockRadius,-cubeY+controls1.a*controls1.t*controls1.t,0);
144 scene.add(cube);
145 //More rope
146 cr1 = new THREE.Mesh(new THREE.CylinderGeometry(0.2,0.2,(length*Math.tan(controls.alpha)+cubeY),32), new THREE.MeshLambertMaterial({color: 0x000000, wireframe: false}));
147 cr1.rotation.set(0,0,0);
148 cr1.position.set(length+controls.blockRadius, (length*Math.tan(controls.alpha)+cube.position.y)/2, 0);
149 cr1.scale.y = (length*Math.tan(controls.alpha)-cube.position.y)/(length*Math.tan(controls.alpha)+cubeY);
150 cr1.castShadow = true;
151 scene.add(cr1);
152
153 cr2 = new THREE.Mesh(new THREE.CylinderGeometry(0.2,0.2,1), new THREE.MeshLambertMaterial({color: 0x000000, wireframe: false}));
154 cr2.rotation.set(0,0,-Math.PI/2+controls.alpha);
155 cr2.position.set((length+block2.position.x-controls.blockRadius*Math.sin(controls.alpha))/2,(length*Math.tan(controls.alpha)+block2.position.y+controls.blockRadius*Math.cos(controls.alpha))/2, 0);
156 cr2.scale.y = (length-block2.position.x-controls.blockRadius*Math.sin(controls.alpha))/Math.cos(controls.alpha);
157 cr2.castShadow = true;
158 scene.add(cr2);
159
160 function Curve2() {
161 THREE.Curve.call(this);
162 };
163 Curve2.prototype = Object.create(THREE.Curve.prototype);
164 Curve2.prototype.constructor = Curve2;
165 Curve2.prototype.getPoint = function(t) {
166 var tx = length+controls.blockRadius*Math.cos(controls.alpha+Math.PI*0.5-t*(controls.alpha+Math.PI*0.5));
167 var ty = length*Math.tan(controls.alpha)+controls.blockRadius*Math.sin(controls.alpha+Math.PI*0.5-t*(controls.alpha+Math.PI*0.5));
168 return new THREE.Vector3(tx,ty,0);
169 };
170 var path2 = new Curve2();
171 cr3 = new THREE.Mesh(new THREE.TubeGeometry(path2,20,0.2,8), new THREE.MeshBasicMaterial({color: 0x000000, wireframe: false}));
172 scene.add(cr3);
173
174 //arrows
175 arr1 = new THREE.Mesh(new THREE.CylinderGeometry(0.3,0.3,4,32), new THREE.MeshBasicMaterial({color: 0xff0000, wireframe: false}));
176 arr1.rotation.set(0,0,0);
177 arr1.position.set(length+controls.blockRadius,cube.position.y+controls.m2*2+controls1.a*2,0);
178 arr1.scale.y = controls1.a;
179 if (!controls.showAcs) {arr1.visible = false}
180 else {arr1.visible = true}
181 scene.add(arr1);
182 arr2 = new THREE.Mesh(new THREE.CylinderGeometry(0,0.7,Math.sign(controls1.a)*2,32), new THREE.MeshBasicMaterial({color: 0xff0000, wireframe: false}));
183 arr2.rotation.set(0,0,0);
184 arr2.position.set(length+controls.blockRadius,cube.position.y+controls.m2*2+controls1.a*4,0);
185 if (!controls.showAcs) {arr2.visible = false}
186 else {arr2.visible = true}
187 scene.add(arr2);
188 arr3 = new THREE.Mesh(new THREE.CylinderGeometry(0.3,0.3,4,32), new THREE.MeshBasicMaterial({color: 0xff0000, wireframe: true}));
189 arr3.rotation.set(0,0,-1.5*Math.PI+controls.alpha);
190 arr3.position.set(block2.position.x-Math.sign(controls1.a)*(controls.blockRadius)*Math.cos(controls.alpha)-controls1.a*2*Math.cos(controls.alpha),block2.position.y-Math.sign(controls1.a)*(controls.blockRadius)*Math.sin(controls.alpha)-controls1.a*2*Math.sin(controls.alpha),0);
191 arr3.scale.y = controls1.a;
192 if (!controls.showAcs) {arr3.visible = false}
193 else {arr3.visible = true}
194 scene.add(arr3);
195 arr4 = new THREE.Mesh(new THREE.CylinderGeometry(0,0.7,-Math.sign(controls1.a)*2,32), new THREE.MeshBasicMaterial({color: 0xff0000, wireframe: false}));
196 arr4.rotation.set(0,0,-Math.PI/2+controls.alpha);
197 arr4.position.set(block2.position.x-Math.sign(controls1.a)*(controls.blockRadius)*Math.cos(controls.alpha)-controls1.a*4*Math.cos(controls.alpha),block2.position.y-Math.sign(controls1.a)*(controls.blockRadius)*Math.sin(controls.alpha)-controls1.a*4*Math.sin(controls.alpha),0);
198 if (!controls.showAcs) {arr4.visible = false}
199 else {arr4.visible = true}
200 scene.add(arr4);
201
202 renderer.render(scene,camera);
203 }
204
205 function renderScene()
206 {
207 if((block2.position.y <= controls.blockRadius) || (cube.position.y >= length*Math.tan(controls.alpha)-controls.blockRadius) || (((block2.position.y-block1.position.y)*(block2.position.y-block1.position.y)+(block2.position.x-block1.position.x)*(block2.position.x-block1.position.x)) <= 4*controls.blockRadius*controls.blockRadius)) {
208 controls1.t = 0;
209 }
210 if (controls.animate)
211 {
212 controls1.t+=dt;
213 ReDraw();
214 }
215
216 stats.update();
217 requestAnimationFrame(renderScene);
218 renderer.render(scene,camera);
219 };
220
221 function initStats()
222 {
223 var stats=new Stats();
224 stats.setMode(0);
225 stats.domElement.style.position='0px';
226 stats.domElement.style.left='0px';
227 stats.domElement.style.top='0px';
228 document.getElementById("Stats-output").appendChild(stats.domElement);
229 return stats;
230 };
231
232 function onResize()
233 {
234 camera.aspect=window.innerWidth/window.innerHeight;
235 camera.updateProjectionMatrix();
236 renderer.setSize(window.innerWidth,window.innerHeight);
237 }
238
239 window.onload = init;
Возможности программы
- изменение угла наклона прямой
Решение частного случая
Условия задачи:
Точка подвеса маятника, состоящего из материальной точки массы
на нерастяжимой нити длины , движется по заданному закону по наклонной прямой, образующей угол с горизонтом. Составить уравнение движения маятника.Решение:
Кинетическая энергия маятника
, где . Здесь . Тогда квадрат скорости равен и кинетическая энергия равна соответственно Потенциальная энергия будет равнаУравнение Лагранжа для системы с одной степенью свободы имеет вид:
- Вычисляем производные, входящие в это уравнение
Подставим полученные производные в уравнение Лагранжа:
, поделим обе части уравнения на и получим