Shimeji with physics added !!!!!
This commit is contained in:
@@ -0,0 +1,273 @@
|
||||
export class Shimeji {
|
||||
private element: HTMLElement;
|
||||
|
||||
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 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
|
||||
|
||||
// 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) {
|
||||
this.element = element;
|
||||
|
||||
// Initialize bounds (will update on resize)
|
||||
this.bounds = {
|
||||
minX: 0,
|
||||
minY: 0,
|
||||
maxX: window.innerWidth,
|
||||
maxY: window.innerHeight
|
||||
};
|
||||
|
||||
// Start with element at center
|
||||
this.x = window.innerWidth / 2;
|
||||
this.y = window.innerHeight / 2;
|
||||
|
||||
this.updateBounds();
|
||||
window.addEventListener('resize', () => this.updateBounds());
|
||||
|
||||
console.log(this.element.getBoundingClientRect());
|
||||
|
||||
this.setupDragEvents();
|
||||
}
|
||||
|
||||
private updateBounds(): void {
|
||||
this.bounds = {
|
||||
minX: 0,
|
||||
minY: 0,
|
||||
maxX: window.innerWidth,
|
||||
maxY: window.innerHeight
|
||||
};
|
||||
}
|
||||
|
||||
start(): void {
|
||||
if (this.rafId !== null) return;
|
||||
|
||||
const firstFrame = (delta: number) => {
|
||||
this.prevDeltaTime = delta;
|
||||
this.rafId = requestAnimationFrame(animate);
|
||||
}
|
||||
|
||||
const animate = (delta: number) => {
|
||||
const dt: number = Math.min(delta - this.prevDeltaTime, 0.05);
|
||||
this.prevDeltaTime = delta;
|
||||
|
||||
this.updatePhysics(dt);
|
||||
this.applyBoundaries();
|
||||
this.render();
|
||||
this.rafId = requestAnimationFrame(animate);
|
||||
};
|
||||
|
||||
this.rafId = requestAnimationFrame(firstFrame);
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
if (this.rafId !== null) {
|
||||
cancelAnimationFrame(this.rafId);
|
||||
this.rafId = null;
|
||||
}
|
||||
}
|
||||
|
||||
private setupDragEvents(): void {
|
||||
this.element.addEventListener('mousedown', (e) => {
|
||||
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}%`;
|
||||
|
||||
});
|
||||
|
||||
document.addEventListener('mousemove', (e) => {
|
||||
this.mouseX = e.pageX;
|
||||
this.mouseY = e.pageY;
|
||||
this.mouseAccelX = e.movementX * 0.1;
|
||||
this.mouseAccelY = e.movementY * 0.1;
|
||||
});
|
||||
|
||||
document.addEventListener('mouseup', (e) => {
|
||||
if(this.isBeingDragged) {
|
||||
this.isBeingDragged = false;
|
||||
this.element.style.cursor = 'grab';
|
||||
|
||||
const parent = this.element.parentElement;
|
||||
if(parent) parent.style.cursor = 'auto';
|
||||
}
|
||||
});
|
||||
|
||||
this.element.addEventListener('dragstart', (e) => {
|
||||
e.preventDefault();
|
||||
});
|
||||
}
|
||||
|
||||
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 forceX = dx * this.STIFFNESS;
|
||||
const forceY = dy * this.STIFFNESS;
|
||||
|
||||
// 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);
|
||||
|
||||
|
||||
console.log(accelerationX, accelerationY);
|
||||
|
||||
// Update velocity with acceleration
|
||||
this.velocityX += accelerationX * dt;
|
||||
this.velocityY += accelerationY * dt;
|
||||
|
||||
this.velocityX *= 0.8;
|
||||
this.velocityY *= 0.8;
|
||||
}
|
||||
|
||||
// Apply gravity (always present)
|
||||
this.velocityY += this.gravity;
|
||||
|
||||
this.velocityX *= this.DAMPING;
|
||||
this.velocityY *= this.DAMPING;
|
||||
|
||||
// Apply additional friction when on ground (touching bottom)
|
||||
if (this.y + this.getElementHeight() >= this.bounds.maxY) {
|
||||
this.velocityX *= 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;
|
||||
}
|
||||
|
||||
private applyBoundaries(): void {
|
||||
const elementWidth = this.getElementWidth();
|
||||
const elementHeight = this.getElementHeight();
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
// Y-axis boundaries
|
||||
const topBound = elementHeight / 2;
|
||||
const bottomBound = this.bounds.maxY - elementHeight / 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private render(): void {
|
||||
this.element.style.transform = `translate(${this.x}px, ${this.y}px) translateY(-50%)`;
|
||||
}
|
||||
|
||||
// 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
|
||||
};
|
||||
}
|
||||
|
||||
moveTo(pos: number) {
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user