-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexcercise_1_9_motion101_acceleration2.js
More file actions
118 lines (97 loc) · 2.44 KB
/
excercise_1_9_motion101_acceleration2.js
File metadata and controls
118 lines (97 loc) · 2.44 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
"use strict";
// Require the max-api module to connect to Max via node.script
const maxAPI = require("max-api");
var Victor = require("victor");
const random = require("random");
const mathplus = require("math-plus");
var width1 = 600
var height1 = 400;
var location1 = new Victor(width1/2, height1/2);
var velocity1 = new Victor(0,0);
var acceleration1 = new Victor(1,1);
class Mover {
constructor(location, velocity, width, height, acceleration, topspeed)
{
this.location = location;
this.velocity = velocity;
this.width = width;
this.height = height;
this.acceleration = acceleration;
this.topspeed = topspeed;
}
update()
{
var topLeft = new Victor(0, 0);
var bottomRight = new Victor(this.width, this.height);
var location1 = new Victor(random.float(-1, 1), random.float(0, 200));
//this.acceleration.random(this.acceleration.x this.acceleration.y);
console.log("acceleration " , this.acceleration.x);
console.log("velocity " , this.velocity.toString());
this.acceleration.multiply(random.int( 0, 2), random.int(0, 2));
this.velocity.add(this.acceleration);
this.velocity.limit(this.acceleration);
this.location.add(this.velocity);
}
display()
{
maxAPI.outlet("newLocationOutput", this.location.x, this.location.y);
}
checkEdges() {
if (this.location.x > this.width) {
this.location.x = 0;
}
else if (this.location.x < 0) {
this.location.x = this.width;
}
if (this.location.y > this.height) {
this.location.y = 0;
}
else if (this.location.y < 0) {
this.location.y = this.height;
}
}
}
/* ublic float magSq() {
return (x*x + y*y + z*z);
}
.lengthSq()
class limit(float max) {
if (magSq() > max*max) {
normalize();
mult(max);
}
return this;
} */
class Limit
{
constructor(max, velocity)
{
this.max = max;
this.velocity = velocity;
}
limit()
{
if(this.velocity.lengthSq() > this.max * this.max)
{
this.velocity.normalize();
this.velocity.mult(this.max);
}
maxAPI.outlet("newLocationOutput", this.velocity.x, this.velocity.y);
}
}
maxAPI.addHandler("motion", (...args) =>
{
var mover = new Mover();
var limit = new Limit();
limit.velocity = velocity1;
limit.max = 6;
mover.width = width1;
mover.height = height1;
mover.location = new Victor(width1/2, height1/2);
mover.velocity = new Victor(0,0);
mover.acceleration = new Victor(0,0);
mover.topspeed = 6;
mover.update();
mover.checkEdges();
mover.display();
});