forked from red42/HTML5_Genetic_Cars
-
Notifications
You must be signed in to change notification settings - Fork 0
/
path.js
89 lines (75 loc) · 2.92 KB
/
path.js
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
/* ========================================================================= */
/* ==== Floor ============================================================== */
function cw_createFloor() {
var last_tile = null;
var tile_position = new b2Vec2(-5, 0);
cw_floorTiles = new Array();
Math.seedrandom(floorseed);
for (var k = 0; k < maxFloorTiles; k++) {
if (!mutable_floor) {
// keep old impossible tracks if not using mutable floors
last_tile = cw_createFloorTile(tile_position, (Math.random() * 3 - 1.5) * 1.5 * k / maxFloorTiles);
} else {
// if path is mutable over races, create smoother tracks
last_tile = cw_createFloorTile(tile_position, (Math.random() * 3 - 1.5) * 1.2 * k / maxFloorTiles);
}
cw_floorTiles.push(last_tile);
last_fixture = last_tile.GetFixtureList();
last_world_coords = last_tile.GetWorldPoint(last_fixture.GetShape().m_vertices[3]);
tile_position = last_world_coords;
}
world.finishLine = tile_position.x;
}
function cw_createFloorTile(position, angle) {
body_def = new b2BodyDef();
body_def.position.Set(position.x, position.y);
var body = world.CreateBody(body_def);
fix_def = new b2FixtureDef();
fix_def.shape = new b2PolygonShape();
fix_def.friction = 0.5;
var coords = new Array();
coords.push(new b2Vec2(0, 0));
coords.push(new b2Vec2(0, -groundPieceHeight));
coords.push(new b2Vec2(groundPieceWidth, -groundPieceHeight));
coords.push(new b2Vec2(groundPieceWidth, 0));
var center = new b2Vec2(0, 0);
var newcoords = cw_rotateFloorTile(coords, center, angle);
fix_def.shape.SetAsArray(newcoords);
body.CreateFixture(fix_def);
return body;
}
function cw_rotateFloorTile(coords, center, angle) {
var newcoords = new Array();
for (var k = 0; k < coords.length; k++) {
nc = new Object();
nc.x = Math.cos(angle) * (coords[k].x - center.x) - Math.sin(angle) * (coords[k].y - center.y) + center.x;
nc.y = Math.sin(angle) * (coords[k].x - center.x) + Math.cos(angle) * (coords[k].y - center.y) + center.y;
newcoords.push(nc);
}
return newcoords;
}
/* ==== END Floor ========================================================== */
/* ========================================================================= */
function cw_drawFloor() {
ctx.strokeStyle = "#000";
ctx.fillStyle = "#666";
ctx.lineWidth = 1 / zoom;
ctx.beginPath();
outer_loop:
for (var k = Math.max(0, last_drawn_tile - 20); k < cw_floorTiles.length; k++) {
var b = cw_floorTiles[k];
for (f = b.GetFixtureList(); f; f = f.m_next) {
var s = f.GetShape();
var shapePosition = b.GetWorldPoint(s.m_vertices[0]).x;
if ((shapePosition > (camera_x - 5)) && (shapePosition < (camera_x + 10))) {
cw_drawVirtualPoly(b, s.m_vertices, s.m_vertexCount);
}
if (shapePosition > camera_x + 10) {
last_drawn_tile = k;
break outer_loop;
}
}
}
ctx.fill();
ctx.stroke();
}