-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexcercise_1_9_motion101_acceleration.js
More file actions
84 lines (71 loc) · 2.06 KB
/
excercise_1_9_motion101_acceleration.js
File metadata and controls
84 lines (71 loc) · 2.06 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
"use strict";
// Require the max-api module to connect to Max via node.script
const maxAPI = require("max-api");
var Victor = require("victor");
var random = require("random");
//var mathplus = require("math-plus");
//var randomVec = require("gl-matrix/random");
var Vector2 = require("gl-matrix/vec2");
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);
var accelerationVectandomNum1 = Vector2.fromValues(1,200);
var randomNum1 = random.float(0.1,1.9);
class Mover {
constructor(location, velocity, width, height, acceleration, topspeed, randomNum)
{
this.location = location;
this.velocity = velocity;
this.width = width;
this.height = height;
this.acceleration = acceleration;
this.topspeed = topspeed;
this.randomNum = randomNum
}
update()
{
this.acceleration = Vector2.random(this.acceleration);
this.randomNum = random.float(0,2);
this.acceleration = Vector2.scale(this.acceleration,this.acceleration,this.randomNum);
var accelerationVectors = new Victor(this.acceleration[1], this.acceleration[0]);
//console.log("accelerationVector" , accelerationVectors.toString())
this.velocity.limit(6,.5);
this.velocity.add(accelerationVectors);
//console.log("accelerationVector vector" , newVel.toString())
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;
}
}
}
maxAPI.addHandler("motion", (...args) =>
{
var mover = new Mover();
mover.width = width1;
mover.height = height1;
mover.location = location1;
mover.velocity = velocity1;
mover.acceleration = acceleration1;
mover.topspeed = 6;
mover.randomNum = randomNum1;
mover.update();
mover.checkEdges();
mover.display();
});