View sourcecode

The following files exists in this folder. Click to view.

fysikscript.js

186 lines UTF-8 Unix (LF)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const colltext = document.getElementById("colltext");

let left,right,up,down;

canvas.width = window.innerWidth;
canvas.height = window.innerHeight;

class Ball{
    constructor(x,y,r){
        this.x = x;
        this.y = y;
        this.r = r;
        this.xvel = 0;
        this.yvel = 0;
    }

    moveBall() {
        this.x += this.xvel;
        this.y += this.yvel;
    }

    drawBall() {
        ctx.beginPath();
        ctx.arc(this.x, this.y, this.r, 0, 2 * Math.PI);
        ctx.strokeStyle = 'red';
        ctx.stroke();
        ctx.fillStyle = 'red';
        ctx.fill();
    }
}

const power = 0.5;
function gas(){
    if(left){
        Ball1.xvel-=power;
    }
    if(up){
        Ball1.yvel-=power;
    }
    if(right){
        Ball1.xvel+=power;
    }
    if(down){
        Ball1.yvel+=power;
    }
}

document.addEventListener('keydown', function(e){
    if(e.keyCode == 37){
        left = true;
    }
    if(e.keyCode == 38){
        up = true;
    }
    if(e.keyCode == 39){
        right = true;
    }
    if(e.keyCode == 40){
        down = true;
    }
})

document.addEventListener('keyup', function(e){
    if(e.keyCode == 37){
        left = false;
    }
    if(e.keyCode == 38){
        up = false;
    }
    if(e.keyCode == 39){
        right = false;
    }
    if(e.keyCode == 40){
        down = false;
    }
})

let balls = [];


let Ball1 = new Ball(200,200,30);
balls.push(Ball1);

for (var i=0;i<=1000; i+=100)
{
   for (var j=300;j<=700; j+=100)
    {
        let newBall = new Ball(i+300,j,20);
        balls.push(newBall);
    }
}

function magnitude(x, y)
{
    return Math.sqrt(x**2+y**2);
}

function dot(x1, y1, x2, y2)
{
    return x1*x2+y1*y2;
}

function unit(x, y)
{
    let factor = 1/magnitude(x, y);
    return [x*factor, y*factor];
}

let elasticity = 0.9;
let collisions = 0;
function processCollisionPair(B1, B2)
{
    let dist = magnitude(B1.x-B2.x, B1.y-B2.y);
    let sum = B1.r + B2.r;
    if (dist <= sum)
    {
        collisions += 1;
        let dX = B2.x - B1.x;
        let dY = B2.y - B1.y;
        let [normalX, normalY] = unit(dX, dY);
        let dvX = B2.xvel - B1.xvel;
        let dvY = B2.yvel - B1.yvel;
        let sepVel = dot(dvX, dvY, normalX, normalY) * elasticity;
        let newSepVel = -sepVel;
        let sepVelX = normalX * newSepVel;
        let sepVelY = normalY * newSepVel;

        B1.xvel += -sepVelX;
        B1.yvel += -sepVelY;
        B2.xvel += sepVelX;
        B2.yvel += sepVelY;

        let depth = (sum - dist) / 2;
        B1.x -= normalX * depth;
        B1.y -= normalY * depth;
        B2.x += normalX * depth;
        B2.y += normalY * depth;
    }
}

function processWallCollision(Ball)
{
    if((Ball.x + Ball.r) >= canvas.width){ //Höger
        Ball.xvel *= -0.8;
        Ball.x = canvas.width - Ball.r;
    }
    if((Ball.x - Ball.r) <= 0){ //Vänster
        Ball.xvel *= -0.8;
        Ball.x = 0 + Ball.r;
    }
    if((Ball.y + Ball.r) >= canvas.height){ //Ned
        Ball.yvel *= -0.8;
        Ball.y = canvas.height - Ball.r;
    }
    if((Ball.y - Ball.r) <= 0){ //Upp
        Ball.yvel *= -0.8;
        Ball.y = 0 + Ball.r;
    }
}

function processCollision()
{
    for (var i=0;i<balls.length;i++)
    {
        processWallCollision(balls[i]);
        var start = i+1;
        for (var j=start; j<balls.length; j++)
        {
            processCollisionPair(balls[i], balls[j]);
        }
    }
}

function mainLoop(){
    colltext.innerText = `Antal kollisioner: ${collisions}`;
    ctx.clearRect(0,0,canvas.width,canvas.height)
    gas();
    processCollision();
    balls.forEach(ball=>ball.moveBall());
    balls.forEach(ball=>ball.drawBall());
    requestAnimationFrame(mainLoop);
}

requestAnimationFrame(mainLoop);