mirror of
https://github.com/Noratrieb/redox.git
synced 2026-01-14 16:25:04 +01:00
mouse
This commit is contained in:
parent
add4166030
commit
78853473ff
5 changed files with 122 additions and 16 deletions
|
|
@ -1,11 +1,15 @@
|
|||
import {Ctx} from "./MainDraw";
|
||||
import Particle from "./classes/Particle";
|
||||
import Particle, {colorFromCharge} from "./classes/Particle";
|
||||
import Vector from "./classes/Vector";
|
||||
import {CANVAS_HEIGHT, CANVAS_WIDTH} from "../App";
|
||||
import {circle} from "./Shapes";
|
||||
|
||||
const particles: Particle[] = [];
|
||||
let particles: Particle[] = [];
|
||||
let mouseProperties: MouseProperties = {charge: 0, strength: 1, pos: new Vector()};
|
||||
|
||||
export function initParticles() {
|
||||
particles = [];
|
||||
|
||||
const chargeToMass = [0.1, 1, 10];
|
||||
for (let i = 0; i < 200; i++) {
|
||||
const charge = Math.random() < 0.3 ? 0 : Math.random() < 0.5 ? 1 : -1;
|
||||
|
|
@ -16,8 +20,19 @@ export function initParticles() {
|
|||
}
|
||||
}
|
||||
|
||||
interface MouseProperties {
|
||||
charge: number,
|
||||
strength: number,
|
||||
pos: Vector
|
||||
}
|
||||
|
||||
export function changeMouseProperties(transformer: (old: MouseProperties) => MouseProperties) {
|
||||
mouseProperties = transformer(mouseProperties);
|
||||
}
|
||||
|
||||
export function drawParticles(ctx: Ctx) {
|
||||
particles.forEach(p => p.draw(ctx));
|
||||
circle(ctx, mouseProperties.pos.x, mouseProperties.pos.y, 5, colorFromCharge(mouseProperties.charge));
|
||||
}
|
||||
|
||||
export function updateParticles() {
|
||||
|
|
@ -33,7 +48,7 @@ function calculateChargedForces() {
|
|||
const innerParticle = particles[j];
|
||||
if (innerParticle.charge !== 0) {
|
||||
const dist = particle.position.distance(innerParticle.position);
|
||||
if (dist < 1000 && dist > 0.30) {
|
||||
if (dist < 300 && dist > 0.30) {
|
||||
const f1 = innerParticle.position.sub(particle.position).scale(0.5 / dist ** 2);
|
||||
if (particle.charge === innerParticle.charge) {
|
||||
particle.applyForce(f1.negated());
|
||||
|
|
@ -45,6 +60,20 @@ function calculateChargedForces() {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
// mouse
|
||||
const dist = particle.position.distance(mouseProperties.pos);
|
||||
if (mouseProperties.charge !== 0 && dist < 10000 && dist > 0.30) {
|
||||
const f1 = mouseProperties.pos
|
||||
.sub(particle.position)
|
||||
.scale(0.5 / dist ** 2)
|
||||
.scale(mouseProperties.strength * 5);
|
||||
if (particle.charge === mouseProperties.charge) {
|
||||
particle.applyForce(f1.negated());
|
||||
} else {
|
||||
particle.applyForce(f1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue