misc/platformerbody
플랫포머 물리 컴포넌트.
Platformer physics component.
PlatformerBodyextends Component
플랫포머 물리 컴포넌트.
- 중력 적분 + 바닥 접지 + 좌우 이동 경계처럼 횡스크롤 게임이 재발명하던 최소 물리(운동학)를 담는다. 노드의 로컬 위치를 직접 움직인다.
- 사용:
const body = node.addComponent(PlatformerBody);
body.setGroundY(560);
body.setHorizontalBounds(40, 920);
// 이동: body.setVelocityX(±speed), 도약: body.jump(-720)Platformer physics component.
- Holds the minimal physics (kinematics) that side-scrollers kept reinventing, such as gravity integration + ground contact + horizontal movement bounds. Moves the node's local position directly.
- Usage:
const body = node.addComponent(PlatformerBody);
body.setGroundY(560);
body.setHorizontalBounds(40, 920);
// move: body.setVelocityX(±speed), jump: body.jump(-720)import { Engine, EngineConfiguration, Scene, WorldNode, Paint, PlatformerBody, Vector2, Pivot } from "./build/vanilla.js";
const playerNode = new WorldNode();
const paint = playerNode.addComponent(Paint);
paint.setColor("#ffcc00");
const platformerBody = playerNode.addComponent(PlatformerBody);
platformerBody.setGroundY(300);
platformerBody.setHorizontalBounds(40, 600);
platformerBody.setVelocityX(120);
platformerBody.setLandedEvent(() => {
console.log("landed");
});
class MainScene extends Scene {
initialize(engine) {
super.initialize(engine);
playerNode.setPivot(Pivot.bottomCenter);
playerNode.setLocalPosition(Vector2.create(80, 100));
playerNode.setContentSize(Vector2.create(32, 48));
const root = this.getRoot();
root.addChild(playerNode);
}
touchPress(viewInputPosition) {
super.touchPress(viewInputPosition);
platformerBody.jump(-720);
}
}
const engineConfiguration = new EngineConfiguration();
const engine = new Engine(engineConfiguration);
engine.run(new MainScene());갱신. (속도 적분 → 중력 → 접지 → 경계)
Update. (velocity integration → gravity → ground contact → bounds)
- timeDeltanumber
- 지난 프레임부터의 시간. (초)Time since the last frame. (Seconds)
도약. (접지 중일 때만, 위쪽이 음수 속도)
Jumps. (only while grounded, upward is negative velocity)
- jumpSpeednumber
- 음수면 위로.Negative is up.
반환Returnsboolean 도약 성공 여부.Whether the jump succeeded.
짝Counterpart: vanilla-engine PlatformerBody.jump
import { WorldNode, PlatformerBody } from "./build/vanilla.js";
const node = new WorldNode();
const platformerBody = node.addComponent(PlatformerBody);
platformerBody.setGroundY(0);
platformerBody.tick(1 / 60);
console.log(platformerBody.isGrounded(), platformerBody.jump(-600));
let highestY = 0;
for (let frame = 0; frame < 60; ++frame) {
platformerBody.tick(1 / 60);
const position = node.getLocalPosition();
highestY = Math.min(highestY, position.y);
}
console.log(highestY.toFixed(1), platformerBody.isGrounded());가로 속도 설정. (픽셀 / 초)
Set the horizontal velocity. (pixels / second)
- velocityXnumber
- 가로 속도. (픽셀 / 초)Horizontal velocity. (pixels / second)
짝Counterpart: vanilla-engine PlatformerBody.set_velocity_x
세로 속도 설정. (픽셀 / 초, 아래쪽이 양수)
Sets the vertical velocity. (pixels / second, down is positive)
- velocityYnumber
- 세로 속도. (픽셀 / 초, 위쪽이 음수)Vertical velocity. (pixels / second, up is negative)
짝Counterpart: vanilla-engine PlatformerBody.set_velocity_y
속도 반환. (픽셀 / 초)
Returns the velocity. (pixels / second)
반환ReturnsVector2 속도. (픽셀 / 초, 아래쪽이 양수)Velocity. (pixels / second, down is positive)
짝Counterpart: vanilla-engine PlatformerBody.get_velocity
중력 가속도 설정. (픽셀 / 초², 아래쪽이 양수, 기본 2000)
Sets the gravity acceleration. (pixels / second², down is positive, default 2000)
- gravitynumber
- 중력 가속도. (픽셀 / 초², 아래쪽이 양수)Gravity acceleration. (pixels / second², down is positive)
짝Counterpart: vanilla-engine PlatformerBody.set_gravity
중력 가속도 반환. (픽셀 / 초²)
Returns the gravity acceleration. (pixels / second²)
반환Returnsnumber 중력 가속도. (픽셀 / 초²)Gravity acceleration. (pixels / second²)
짝Counterpart: vanilla-engine PlatformerBody.get_gravity
바닥 높이 설정. (노드가 이 Y 아래로 내려가지 않는다, 기본 0)
Sets the ground height. (the node does not go below this Y, default 0)
- groundYnumber
- 바닥의 Y. (부모 기준 지역 좌표)Ground Y. (local coordinates relative to the parent)
짝Counterpart: vanilla-engine PlatformerBody.set_ground_y
바닥 높이 반환.
Returns the ground height.
반환Returnsnumber 바닥의 Y. (부모 기준 지역 좌표)Ground Y. (local coordinates relative to the parent)
짝Counterpart: vanilla-engine PlatformerBody.get_ground_y
좌우 이동 경계 설정. (null 이면 해당 방향 제한 없음)
Sets the horizontal movement bounds. (null means no limit in that direction)
- leftBoundnumber | null
- 왼쪽 경계 X. (null 이면 제한 없음)Left bound X. (no limit if null)
- rightBoundnumber | null
- 오른쪽 경계 X. (null 이면 제한 없음)Right bound X. (no limit if null)
짝Counterpart: vanilla-engine PlatformerBody.set_horizontal_bounds
바닥에 닿아 있는지 여부.
Whether touching the ground.
반환Returnsboolean 바닥에 닿아 있으면 참.True if touching the ground.
짝Counterpart: vanilla-engine PlatformerBody.is_grounded
착지 알림 설정. (공중에서 바닥에 닿는 순간)
Sets the landing callback. (the moment it touches the ground from the air)
- landedEventFunction
- 공중에서 바닥에 닿는 순간 부를 함수. () => voidFunction called the moment it lands from the air. () => void
짝Counterpart: vanilla-engine PlatformerBody.set_landed_event