MCPcopy Create free account
hub / github.com/melonjs/melonJS / updateShape

Method updateShape

packages/matter-adapter/src/index.ts:586–607  ·  view source on GitHub ↗
(renderable: Renderable, shapes: BodyShape[])

Source from the content-addressed store, hash-verified

584 }
585
586 updateShape(renderable: Renderable, shapes: BodyShape[]): void {
587 // matter-js doesn't support live shape mutation cleanly — the
588 // pragmatic approach is to rebuild the body. Preserve the prior
589 // def, swap shapes, re-register — AND carry forward the velocity
590 // state (linear + angular) so a moving body whose shape changes
591 // mid-flight doesn't stop dead.
592 const oldDef = this.defMap.get(renderable);
593 if (!oldDef) {
594 return;
595 }
596 const oldBody = this.bodyMap.get(renderable);
597 const savedVel = oldBody
598 ? { x: oldBody.velocity.x, y: oldBody.velocity.y }
599 : undefined;
600 const savedAngVel = oldBody?.angularVelocity ?? 0;
601 this.removeBody(renderable);
602 const newBody = this.addBody(renderable, { ...oldDef, shapes });
603 if (savedVel) {
604 Matter.Body.setVelocity(newBody, savedVel);
605 Matter.Body.setAngularVelocity(newBody, savedAngVel);
606 }
607 }
608
609 getVelocity(renderable: Renderable, out?: Vector2d): Vector2d {
610 const body = this.bodyMap.get(renderable);

Callers

nothing calls this directly

Calls 5

removeBodyMethod · 0.95
addBodyMethod · 0.95
getMethod · 0.65
setVelocityMethod · 0.65
setAngularVelocityMethod · 0.65

Tested by

no test coverage detected