/
Separate.test.html
97 lines (87 loc) · 2.35 KB
/
Separate.test.html
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
<!DOCTYPE html>
<!--
/**
* @author Raoul Harel
* @license The MIT license (LICENSE.txt)
* @copyright 2015 Raoul Harel
* @url https://github.com/rharel/js-steering-behaviors
*/
-->
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Separate Visual Test [js-steering-behaviors]</title>
<link rel="stylesheet" href="css/visual_test.css">
<script src="../../dist/steering_behaviors.js"></script>
<script src="js/Simulation.js"></script>
<script src="js/Renderer.js"></script>
</head>
<body>
<h1>Separate behavior visual test</h1>
<script>
(function() {
const canvas = document.createElement('canvas');
const _2d = canvas.getContext('2d');
canvas.width = 500;
canvas.height = 500;
document.body.appendChild(canvas);
const simulation = new Simulation(canvas.width, canvas.height);
const renderer = new Renderer
(
{
size: new SB.Vector(30, 40),
stroke: 'black',
fill: 'lightgreen',
line_width: 2
}
);
const NaiveNN = new SB.Spatial.NaiveNN();
const get_NN = position => NaiveNN
.get_nearest_in_radius(position, 200)
.filter(item => item.position !== position);
const separation_behavior = new SB.Behavior.Separate(get_NN, 200);
simulation.agents.push(
{
vehicle: new SB.Vehicle
({
position: new SB.Vector(245, 245),
max_thrust: 100,
max_speed: 200
}),
behavior: separation_behavior,
style: { fill: 'lightgreen' }
});
simulation.agents.push(
{
vehicle: new SB.Vehicle
({
position: new SB.Vector(255, 255),
max_thrust: 100,
max_speed: 200
}),
behavior: separation_behavior,
style: { fill: 'red' }
});
simulation.agents.forEach((agent, i) =>
{
NaiveNN.set_site_position(i, agent.vehicle.position);
});
let t_before = new Date();
function main()
{
const t_now = new Date();
const dt = (t_now - t_before) / 1000;
t_before = t_now;
simulation.step(dt === 0 ? 0.001 : dt);
renderer.render_background(_2d);
simulation.agents.forEach(agent =>
{
renderer.render_vehicle(_2d, agent.vehicle, agent.style);
});
setTimeout(() => requestAnimationFrame(main), 15);
}
main();
})();
</script>
</body>
</html>