2dphysics unfiniished simple 1 object versioon in shimeji

This commit is contained in:
2026-03-24 18:04:34 +01:00
parent 2fa12e22d5
commit 2183dda2df
11 changed files with 707 additions and 438 deletions
+87 -150
View File
@@ -1,42 +1,37 @@
import Vector2 from "./vector";
export class Shimeji {
private element: HTMLElement;
private canvas: HTMLCanvasElement;
private rect: DOMRect;
private x: number;
private y: number;
private velocityX: number = 0;
private velocityY: number = 0;
// Mouse tracking
private mouseX: number = 0;
private mouseY: number = 0;
private mouseAccelX: number = 0;
private mouseAccelY: number = 0;
private pos: Vector2 = Vector2.zero;
private vel: Vector2 = Vector2.zero;
private mousePos: Vector2 = Vector2.zero;
private pivot: Vector2 = Vector2.zero;
private isBeingDragged: boolean = false;
private rafId: number | null = null;
private prevDeltaTime: number = 0;
// Physics parameters
private readonly RESTITUTION: number = 0.6; // 1 perfect bounciness 0 no bounce
private readonly STIFFNESS: number = 100; // Spring constant
private readonly DAMPING: number = 1; // Air resistance 1 - no resistance 0.1 high air density
private readonly MASS: number = 10.0; // Mass
private readonly TIMESCALE: number = 1;
private readonly RESTITUTION: number = 0.4;
private readonly CURSOR_STRENGTH: number = 250;
private readonly DAMPING: number = 1;
private readonly MASS: number = 0.1;
private readonly FRICTION: number = 0.9;
private readonly GRAVITY: number = 50;
// Gravity (pixels per frame squared)
private gravity: number = 10;
// Boundaries
private bounds: { minX: number; minY: number; maxX: number; maxY: number };
// Optional forces
private wind: number = 0;
private friction: number = 0.8;
constructor(element: HTMLElement) {
constructor(element: HTMLElement, canvas: HTMLCanvasElement) {
this.element = element;
this.canvas= canvas;
this.rect = element.getBoundingClientRect();
// Initialize bounds (will update on resize)
this.bounds = {
minX: 0,
minY: 0,
@@ -44,9 +39,7 @@ export class Shimeji {
maxY: window.innerHeight
};
// Start with element at center
this.x = window.innerWidth / 2;
this.y = window.innerHeight / 2;
this.pos.set(window.innerWidth / 2, window.innerHeight/ 2);
this.updateBounds();
window.addEventListener('resize', () => this.updateBounds());
@@ -63,6 +56,9 @@ export class Shimeji {
maxX: window.innerWidth,
maxY: window.innerHeight
};
this.canvas.width = window.innerWidth;
this.canvas.height = window.innerHeight;
}
start(): void {
@@ -74,7 +70,8 @@ export class Shimeji {
}
const animate = (delta: number) => {
const dt: number = Math.min(delta - this.prevDeltaTime, 0.05);
let dt = (delta - this.prevDeltaTime) / 1000; // seconds
dt = Math.min(dt, 0.05 * this.TIMESCALE); // cap to 50 ms
this.prevDeltaTime = delta;
this.updatePhysics(dt);
@@ -98,25 +95,18 @@ export class Shimeji {
e.preventDefault();
this.isBeingDragged = true;
this.element.style.cursor = 'grabbing';
const parent = this.element.parentElement;
if(parent) parent.style.cursor = 'grabbing';
const rect = this.element.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
const pivotX = (x / rect.width) * 100;
const pivotY = (y / rect.height) * 100;
this.element.style.transformOrigin = `${pivotX}% ${pivotY}%`;
if(parent) parent.style.cursor = 'grabbing';
this.pivot.x = e.offsetX - this.rect.width / 2;
this.pivot.y = e.offsetY - this.rect.height / 2;
});
document.addEventListener('mousemove', (e) => {
this.mouseX = e.pageX;
this.mouseY = e.pageY;
this.mouseAccelX = e.movementX * 0.1;
this.mouseAccelY = e.movementY * 0.1;
this.mousePos.x = e.pageX;
this.mousePos.y = e.pageY;
});
document.addEventListener('mouseup', (e) => {
@@ -126,7 +116,11 @@ export class Shimeji {
const parent = this.element.parentElement;
if(parent) parent.style.cursor = 'auto';
this.pivot = Vector2.zero;
}
this.canvas.getContext('2d')!.clearRect(0, 0, this.canvas.width, this.canvas.height);
});
this.element.addEventListener('dragstart', (e) => {
@@ -136,135 +130,78 @@ export class Shimeji {
private updatePhysics(dt: number): void {
if (this.isBeingDragged) {
console.log(this.element.style.transformOrigin);
// Spring force toward mouse
const dx = this.mouseX - this.x;
const dy = this.mouseY - this.y;
const direction: Vector2 = this.mousePos.sub(this.pivot).sub(this.pos);
const forceX = dx * this.STIFFNESS;
const forceY = dy * this.STIFFNESS;
const force: Vector2 = direction.mul(this.CURSOR_STRENGTH);
const acceleration: Vector2 = force.div(this.MASS).deadZone(0.001);
// Apply force to acceleration (F = ma)
const accelerationX = Math.abs(forceX / this.MASS) < 0.0001 ? 0 : (forceX / this.MASS);
const accelerationY = Math.abs(forceY / this.MASS) < 0.0001 ? 0 : (forceY / this.MASS);
acceleration.mulEq(dt);
console.log(accelerationX, accelerationY);
// Update velocity with acceleration
this.velocityX += accelerationX * dt;
this.velocityY += accelerationY * dt;
this.velocityX *= 0.8;
this.velocityY *= 0.8;
this.vel.addEq(acceleration);
this.vel.mulEq(0.8);
}
// Apply gravity (always present)
this.velocityY += this.gravity;
this.velocityX *= this.DAMPING;
this.velocityY *= this.DAMPING;
this.vel.y += this.GRAVITY;
this.vel.mulEq(this.DAMPING);
// Apply additional friction when on ground (touching bottom)
if (this.y + this.getElementHeight() >= this.bounds.maxY) {
this.velocityX *= this.friction;
if (this.pos.y + this.rect.width >= this.bounds.maxY) {
this.vel.x *= this.FRICTION;
}
// Update position with velocity
this.x = this.x + this.velocityX * dt;
this.y = this.y + this.velocityY * dt;
}
private getElementHeight(): number {
return this.element.offsetHeight;
}
private getElementWidth(): number {
return this.element.offsetWidth;
const outVel = this.vel.mul(dt);
this.pos.addEq(outVel);
}
private applyBoundaries(): void {
const elementWidth = this.getElementWidth();
const elementHeight = this.getElementHeight();
const leftBound = this.rect.width / 2;
const rightBound = this.bounds.maxX - this.rect.width / 2;
// X-axis boundaries (account for element width with center positioning)
const leftBound = elementWidth / 2;
const rightBound = this.bounds.maxX - elementWidth / 2;
if (this.x < leftBound) {
this.x = leftBound;
this.velocityX = -(this.RESTITUTION) * this.velocityX;
} else if (this.x > rightBound) {
this.x = rightBound;
this.velocityX = -(this.RESTITUTION) * this.velocityX;
if (this.pos.x < leftBound) {
this.pos.x = leftBound;
this.vel.x = -(this.RESTITUTION) * this.vel.x;
} else if (this.pos.x > rightBound) {
this.pos.x = rightBound;
this.vel.x = -(this.RESTITUTION) * this.vel.x;
}
// Y-axis boundaries
const topBound = elementHeight / 2;
const bottomBound = this.bounds.maxY - elementHeight / 2;
const topBound = this.rect.height / 2;
const bottomBound = this.bounds.maxY - this.rect.height / 2;
if (this.y < topBound) {
this.y = topBound;
this.velocityY = -(this.RESTITUTION) * this.velocityY;
} else if (this.y > bottomBound) {
this.y = bottomBound;
this.velocityY = -(this.RESTITUTION) * this.velocityY;
if (Math.abs(this.velocityY) < 0.3) {
this.velocityY = 0;
}
if (this.pos.y < topBound) {
this.pos.y = topBound;
this.vel.y = -(this.RESTITUTION) * this.vel.y;
} else if (this.pos.y > bottomBound) {
this.pos.y = bottomBound;
this.vel.y = -(this.RESTITUTION) * this.vel.y;
}
}
private drawRay(pos1: {x: number, y: number}, pos2: {x: number, y: number}) {
const ctx = this.canvas.getContext("2d")!;
ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
ctx.strokeStyle = "white";
ctx.lineWidth = 0.1
ctx.beginPath();
ctx.moveTo(pos1.x, pos1.y);
ctx.lineTo(pos2.x, pos2.y);
ctx.stroke();
}
private render(): void {
this.element.style.transform = `translate(${this.x}px, ${this.y}px) translateY(-50%)`;
this.element.style.transform = `translate(${this.pos.x}px, ${this.pos.y}px)`;
/* if(this.isBeingDragged) {
this.drawRay(
{x: this.mouseX, y: this.mouseY},
{x: this.x + this.pivotX, y: this.y + this.pivotY}
)
} */
}
// Public methods to control physics
setGravity(value: number): void {
this.gravity = value;
}
setWind(value: number): void {
this.wind = value;
}
setPhysics(stiffness: number, damping: number, mass: number = 1.0): void {
(this as any).STIFFNESS = stiffness;
(this as any).DAMPING = damping;
(this as any).MASS = mass;
}
setFriction(value: number): void {
this.friction = value;
}
// Add impulse (like a jump)
addImpulse(x: number, y: number): void {
this.velocityX += x;
this.velocityY += y;
}
// Reset position
resetPosition(x?: number, y?: number): void {
this.x = x ?? this.bounds.maxX / 2;
this.y = y ?? this.bounds.maxY / 2;
this.velocityX = 0;
this.velocityY = 0;
}
getState() {
return {
x: this.x,
y: this.y,
velocityX: this.velocityX,
velocityY: this.velocityY,
mouseX: this.mouseX,
mouseY: this.mouseY
};
this.vel.x += x;
this.vel.y += y;
}
moveTo(pos: number) {