Edit
Here is a JSFiddle with the code for the “tail” function commenting out.Solar System JSFiddle
I have this object I am working on that has an object orbiting a center mass. That works perfectly.
I am now trying to add a trailing line or “tail” that follows behind the planet. My tail object looks like this:
JavaScript
x
28
28
1
function Tail(maxLength){
2
this.points = [];
3
this.maxLength = maxLength;
4
this.addPoint = function(point){
5
for(var i = Math.min(maxLength, this.points.length); i < maxLength; i++){
6
this.points[i] = this.points[i - 1];
7
}
8
9
this.points[0] = point;
10
}
11
this.draw = function(ctx){
12
for(var i = 1; Math.min(maxLength, this.points.length); i++){
13
if(i < maxLength - 20){
14
ctx.globalAlpha = 1;
15
} else {
16
ctx.globalAlpha = (this.maxLength - i) / 20;
17
}
18
19
ctx.beginPath();
20
ctx.moveTo(this.points[i - 1].x, this.points[i - 1].y);
21
ctx.lineTo(this.points[i].x, this.points[i].y);
22
ctx.stroke();
23
}
24
25
ctx.globalAlpha = 1;
26
}
27
}
28
The addPoint function takes an object that looks like ‘{x: currentX, y: currentY} currentX and currentY are the x and y point of the object when ever it gets called.
I am lost on how I need to add the point to the points array and then draw based on those coordinates.
Advertisement
Answer
I modified your version to working condition
JavaScript
1
124
124
1
var canvas, width, height, ctx;
2
var bodies = [];
3
4
function init(){
5
canvas = document.getElementById("space-time");
6
width = window.innerWidth;
7
height = window.innerHeight;
8
canvas.width = width;
9
canvas.height = height;
10
ctx = canvas.getContext('2d');
11
12
createBodies();
13
14
setInterval(function(){
15
updateSystem();
16
updateBodies(0.01);
17
ctx.clearRect(0, 0, width, height);
18
drawBodies();
19
}, 10);
20
}
21
22
function createBodies(){
23
bodies.push(new Body((this.width / 2) - 250, (this.height / 2) - 300, 200, 0, 1, 10, "#14c71d", true));
24
bodies.push(new Body((this.width / 2) + 100, (this.height / 2) + 100, 350, 0, 1, 5, "#de2d16", true));
25
bodies.push(new Body(this.width / 2, this.height / 2, 0, 0, 1000000, 30, "#FF8501", false)); //sun
26
}
27
28
function drawBodies(){
29
for(var i = 0; i < bodies.length; i++){
30
bodies[i].draw(ctx);
31
}
32
}
33
34
function updateBodies(dt){
35
for(var i = 0; i < bodies.length; i++){
36
bodies[i].update(dt);
37
}
38
}
39
40
function updateSystem(){
41
var G = 10;
42
for(var i = 0; i < bodies.length; i++){
43
for(var j = 0; j < bodies.length; j++){
44
if(i === j) continue;
45
var b1 = bodies[i];
46
var b2 = bodies[j];
47
48
var dist = Math.sqrt((b1.x - b2.x) * (b1.x - b2.x) + (b1.y - b2.y) * (b1.y - b2.y));
49
var force = G * (b1.m * b2.m) / dist / dist;
50
var nx = (b2.x - b1.x) / dist;
51
var ny = (b2.y - b1.y) / dist;
52
53
b1.ax += nx * force / b1.m;
54
b1.ay += ny * force / b1.m;
55
56
b2.ax -= nx * force / b2.m;
57
b2.ay -= ny * force / b2.m;
58
}
59
}
60
}
61
62
function Body(x, y, v, angle, mass, radius, color, hasTail){
63
this.x = x;
64
this.y = y;
65
this.vx = v * Math.cos(angle);
66
this.vy = v * Math.sin(angle);
67
this.m = mass;
68
this.radius = radius;
69
this.color = color;
70
this.ax = 0;
71
this.ay = 0;
72
73
if (hasTail) {
74
this.tail = new Tail(50);
75
}
76
77
this.update = function(dt){
78
this.vx += this.ax * dt;
79
this.vy += this.ay * dt;
80
this.x += this.vx * dt;
81
this.y += this.vy * dt;
82
this.ax = 0;
83
this.ay = 0;
84
85
if(this.tail){
86
this.tail.addPoint({x: this.x, y: this.y});
87
}
88
}
89
90
this.draw = function(ctx){
91
ctx.strokeStyle = this.color;
92
ctx.fillStyle = this.color;
93
ctx.shadowColor = this.color;
94
ctx.shadowBlur = 5;
95
if(this.tail){
96
this.tail.draw(ctx);
97
}
98
ctx.beginPath();
99
ctx.arc(this.x, this.y, this.radius, 0, 6.28);
100
ctx.fill();
101
}
102
}
103
104
function Tail(maxLength){
105
this.points = [];
106
this.maxLength = maxLength;
107
this.addPoint = point => {
108
this.points = [point].concat(this.points).slice(0, this.maxLength);
109
}
110
this.draw = function(ctx){
111
for(var i = 1; i < this.points.length; i++){
112
ctx.beginPath();
113
if(i < maxLength - 20){
114
ctx.globalAlpha = 1;
115
} else {
116
ctx.globalAlpha = (this.maxLength - i) / 20;
117
}
118
ctx.moveTo(this.points[i - 1].x, this.points[i - 1].y);
119
ctx.lineTo(this.points[i].x, this.points[i].y);
120
ctx.stroke();
121
}
122
ctx.globalAlpha = 1;
123
}
124
}
JavaScript
1
3
1
#space-time {
2
background-color: #1a1a1c;
3
}
JavaScript
1
11
11
1
<!DOCTYPE html>
2
<html lang="en">
3
<head>
4
<meta charset="UTF-8">
5
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6
<title>Solar System AJ</title>
7
</head>
8
<body onload="init();">
9
<canvas id="space-time"></canvas>
10
</body>
11
</html>