Skip to content

Commit 0429e3d

Browse files
committed
Bugged version
The canvas representation starts at the top left corner, and the usual trigonometry starts at the bottom left corner. This causes some problems when using trigonometry to compute directions. To solve this problem, I should develop the physic engine and the graphic engines independantly.
1 parent 9a67558 commit 0429e3d

File tree

1 file changed

+65
-22
lines changed

1 file changed

+65
-22
lines changed

script.js

+65-22
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,38 @@ window.onload = function() {
1919
new Position(60, 60), new Position(60, 260)
2020
)
2121
);
22-
myWorld.path.next = new Line(
23-
new Position(60, 260), new Position(160, 360)
22+
var path = myWorld.path;
23+
path.next = new Circle(new Position(160, 260), 100, Math.PI, Math.PI / 2);
24+
path.next.previous = path;
25+
path = path.next;
26+
path.next = new Line(
27+
new Position(160, 360), new Position(360, 360)
2428
);
25-
myWorld.path.next.previous = myWorld.path;
29+
path.next.previous = path;
30+
path = path.next;
31+
path.next = new Circle(new Position(560, 160), 200, Math.PI / 2, Math.PI / 2);
32+
path.next.previous = path;
33+
/*
34+
path.next = new Circle(new Position(360, 260), 300, Math.PI, Math.PI);
35+
path.next.previous = path;
36+
*/
37+
2638
myWorld.tokens = [new Token(50, myWorld.path, 0, 0.1)];
2739

2840
printPaths(myWorld, rcontext); // We just have to do it once
2941
printTokens(myWorld, tcontext);
3042

31-
setInterval(function() {
32-
updateVelocity(myWorld.tokens[0], myWorld, 1 / 60);
33-
updatePosition(myWorld.tokens[0], 1 / 60);
34-
printTokens(myWorld, tcontext);
35-
}, 1000 / 60);
36-
43+
var play = false;
44+
var interval;
3745
document.getElementById('tokens').onclick = function() {
38-
updateVelocity(myWorld.tokens[0], myWorld, 1);
39-
updatePosition(myWorld.tokens[0], 1);
40-
printTokens(myWorld, tcontext);
41-
console.log(myWorld.tokens[0].velocity);
46+
if (play = !play) {
47+
clearInterval(interval);
48+
} else {
49+
interval = setInterval(function() {
50+
updateTokens(myWorld, 1/60);
51+
printTokens(myWorld, tcontext);
52+
}, 1000/60);
53+
}
4254
}
4355
};
4456

@@ -69,11 +81,12 @@ function Line(start, end) {
6981
this.length = Math.sqrt(distx * distx + disty * disty);
7082
}
7183

72-
function Circle(center, radius, start, end) {
84+
function Circle(center, radius, start, angle) {
7385
this.center = center;
7486
this.radius = radius;
7587
this.start = start;
76-
this.end = end;
88+
this.angle = angle;
89+
this.length = Math.abs(radius * angle);
7790
}
7891

7992
function Token(size, path, progression, friction) {
@@ -85,10 +98,16 @@ function Token(size, path, progression, friction) {
8598
}
8699

87100
function updateVelocity(token, world, time) {
88-
var pathDir = Math.atan(
89-
(token.path.end.y - token.path.start.y) /
90-
(token.path.end.x - token.path.start.x)
91-
);
101+
var pathDir;
102+
if (token.path instanceof Line) {
103+
pathDir = Math.atan(
104+
(token.path.end.y - token.path.start.y) /
105+
(token.path.end.x - token.path.start.x)
106+
);
107+
} else if (token.path instanceof Circle) {
108+
pathDir = - (token.progression / token.path.radius - token.path.start + Math.PI / 2) % Math.PI;
109+
}
110+
console.log(pathDir);
92111
token.velocity += time * world.force.value * Math.cos(world.force.direction - pathDir);
93112
token.velocity *= (1 - time * token.friction);
94113
}
@@ -118,15 +137,25 @@ function updatePosition(token, time) {
118137
}
119138
}
120139

140+
function updateTokens(world, time) {
141+
for (var i = 0; i < world.tokens.length; i++) {
142+
updateVelocity(world.tokens[i], world, time);
143+
updatePosition(world.tokens[i], time);
144+
}
145+
}
121146
function position(token) {
122147
if (token.path instanceof Line) {
123148
return new Position(
124149
token.path.start.x +
125150
(token.path.end.x - token.path.start.x) * token.progression / token.path.length,
126151
token.path.start.y +
127-
(token.path.end.y - token.path.start.y) * token.progression / token.path.length);
128-
} else {
129-
return 0;
152+
(token.path.end.y - token.path.start.y) * token.progression / token.path.length
153+
);
154+
} else if (token.path instanceof Circle) {
155+
return new Position(
156+
token.path.center.x + token.path.radius * Math.cos(token.path.start - (token.progression / token.path.length) * token.path.angle),
157+
token.path.center.y + token.path.radius * Math.sin(token.path.start - (token.progression / token.path.length) * token.path.angle)
158+
);
130159
}
131160
}
132161

@@ -140,10 +169,14 @@ function printPaths(world, context) {
140169
for (p = world.path; hasNext(p); p = p.next) {
141170
if (p instanceof Line) {
142171
printLine(p, context);
172+
} else if (p instanceof Circle) {
173+
printCircle(p, context);
143174
}
144175
}
145176
if (p instanceof Line) {
146177
printLine(p, context);
178+
} else if (p instanceof Circle) {
179+
printCircle(p, context);
147180
}
148181
}
149182

@@ -154,6 +187,16 @@ function printLine(line, context) {
154187
console.log('Line : ' + line.length);
155188
}
156189

190+
function printCircle(circle, context) {
191+
context.beginPath();
192+
if (circle.angle >= 0) {
193+
context.arc(circle.center.x, circle.center.y, circle.radius, circle.start - circle.angle, circle.start);
194+
} else {
195+
context.arc(circle.center.x, circle.center.y, circle.radius, circle.start, circle.start - circle.angle);
196+
}
197+
context.stroke();
198+
}
199+
157200
function printTokens(world, context) {
158201
context.clearRect(0, 0, 1000, 1000);
159202
for (var i = 0; i < world.tokens.length; i++) {

0 commit comments

Comments
 (0)