vanilladocs
vanillapunch.com
API / misc/platformerbody

misc/platformerbody

플랫포머 물리 컴포넌트.

  • 원본 misc/platformerbody.js 를 옮긴 것이다. 중력 적분, 바닥 접지, 좌우 이동 경계처럼 횡스크롤 게임이 다시 만들던 최소 물리(운동학)를 담는다. 노드의 로컬 위치를 직접 움직인다. (아래쪽이 +Y)
  • 사용:
let body = node_tree[hero_id].add_component(PlatformerBody::new());
body.set_ground_y(560.0);
body.set_horizontal_bounds(Some(40.0), Some(920.0));
// 이동: body.set_velocity_x(±speed), 도약: body.jump(-720.0)
  • 원본과 다른 점: 경계의 null 은 None 이다. 착지 알림은 FnMut() 콜백이다.

Platformer physics component.

  • A port of the original misc/platformerbody.js. Holds the minimal physics (kinematics) that side-scrolling games kept rebuilding, like gravity integration, ground contact, and horizontal movement bounds. Moves the node's local position directly. (down is +Y)
  • Usage:
let body = node_tree[hero_id].add_component(PlatformerBody::new());
body.set_ground_y(560.0);
body.set_horizontal_bounds(Some(40.0), Some(920.0));
// move: body.set_velocity_x(±speed), jump: body.jump(-720.0)
  • Differences from the original: null bounds are None. The landing notification is an FnMut() callback.

PlatformerBodystruct traits Default, Component

짝Counterpart: vanilla.js PlatformerBody소스source

플랫포머 물리 컴포넌트.

  • tick 마다 integrate 로 다음 위치를 구해 노드 로컬 위치에 넣는다.

Platformer physics component.

  • Each tick computes the next position with integrate and puts it into the node's local position.
예제Example노드에 붙여 떨어뜨리고 바닥에서 뛰기Attach to a node, let it fall, and jump from the ground
use vanilla::{Engine, EngineConfiguration, NodeTree, PlatformerBody, Vector2};

let engine = Engine::new(EngineConfiguration::new());
let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let hero_id = node_tree.create_child(root_id, "hero");
node_tree[hero_id].set_local_position(Vector2::create(100.0, 0.0));
let body = node_tree[hero_id].add_component(PlatformerBody::new());
body.set_ground_y(560.0);
body.set_horizontal_bounds(Some(40.0), Some(920.0));

for _ in 0..60 {
	node_tree.tick(&engine, 1.0 / 60.0);
}
assert_eq!(node_tree[hero_id].get_local_position().y, 560.0);
let body = node_tree[hero_id].get_component_mut::<PlatformerBody>().unwrap();
assert!(body.is_grounded());
body.set_velocity_x(300.0);
assert!(body.jump(-720.0));
생성자constructorpub fn new() -> PlatformerBody#0.3.2 부터since 0.3.2소스source

생성. (중력 초당 2000, 바닥 0, 경계 없음, 공중)

Constructor. (gravity 2000 per second, ground 0, no bounds, airborne)

반환ReturnsPlatformerBody 속도가 0 인 새 플랫포머 물리.New platformer physics with zero velocity.

짝Counterpart: vanilla.js PlatformerBody.constructor

메서드methodpub fn integrate(&mut self, current_position: Vector2, time_delta: f64) -> Vector2#0.3.2 부터since 0.3.2소스source

한 걸음 적분. (속도에 중력 → 위치 → 접지 → 경계. 다음 위치를 돌려준다)

  • tick 이 부른다. 노드 없이도 시험할 수 있게 떼어 냈다.
  • 바닥에 닿으면 세로 속도를 0 으로 하고, 공중에 있다가 닿은 순간이면 착지 알림을 부른다.

Integrates one step. (velocity with gravity → position → ground contact → bounds. Returns the next position)

  • Called by tick. Separated out so it can be tested without a node.
  • On touching the ground, sets the vertical velocity to 0, and calls the landing notification at the moment it lands from the air.
current_positionVector2
지금 위치. (부모 영역 기준 로컬 좌표, 아래쪽이 +Y)Current position. (local coordinates relative to the parent area, down is +Y)
time_deltaf64
지난 시간. (초)Elapsed time. (seconds)

반환ReturnsVector2 다음 위치. (y 는 바닥 높이를 넘지 않고, x 는 좌우 경계 안으로 자른다)Next position. (y does not go past the ground height, and x is clamped within the left and right bounds)

예제Example노드 없이 한 걸음 적분하기Integrate one step without a node
use vanilla::{PlatformerBody, Vector2};

let mut body = PlatformerBody::new();
body.set_ground_y(100.0);
let next_position = body.integrate(Vector2::create(0.0, 0.0), 0.1);
assert_eq!(next_position, Vector2::create(0.0, 20.0));
assert_eq!(body.get_velocity().y, 200.0);
assert!(!body.is_grounded());
메서드methodpub fn jump(&mut self, jump_speed: f64) -> bool#0.3.2 부터since 0.3.2소스source

도약. (접지 중일 때만, 위쪽이 음수 속도. 뛰었으면 true)

Jumps. (only while grounded, up is negative velocity. true if it jumped)

jump_speedf64
세로 속도로 넣을 값. (픽셀/초, 위로 뛰려면 음수)Value to set as the vertical velocity. (pixels/second, negative to jump up)

반환Returnsbool 접지 중이라 뛰었으면 true. 공중이면 false 이고 속도를 바꾸지 않는다.true if it jumped because it was grounded. false if airborne, and the velocity is unchanged.

짝Counterpart: vanilla.js PlatformerBody.jump

메서드methodpub fn set_velocity_x(&mut self, velocity_x: f64)#0.3.2 부터since 0.3.2소스source

가로 속도 설정.

Sets the horizontal velocity.

velocity_xf64
가로 속도. (픽셀/초, 오른쪽이 +)Horizontal velocity. (pixels/second, right is +)

짝Counterpart: vanilla.js PlatformerBody.setVelocityX

메서드methodpub fn set_velocity_y(&mut self, velocity_y: f64)#0.3.2 부터since 0.3.2소스source

세로 속도 설정.

Sets the vertical velocity.

velocity_yf64
세로 속도. (픽셀/초, 아래쪽이 +)Vertical velocity. (pixels/second, down is +)

짝Counterpart: vanilla.js PlatformerBody.setVelocityY

메서드methodpub fn get_velocity(&self) -> Vector2#0.3.2 부터since 0.3.2소스source

속도 반환.

Returns the speed.

반환ReturnsVector2 지금 속도. (픽셀/초)Current velocity. (pixels/second)

짝Counterpart: vanilla.js PlatformerBody.getVelocity

메서드methodpub fn set_gravity(&mut self, gravity: f64)#0.3.2 부터since 0.3.2소스source

중력 설정. (초당 속도 변화량)

Sets the gravity. (velocity change per second)

gravityf64
중력. (픽셀/초², 아래쪽이 +, 기본 2000)Gravity. (pixels/second², down is +, default 2000)

짝Counterpart: vanilla.js PlatformerBody.setGravity

메서드methodpub fn get_gravity(&self) -> f64#0.3.2 부터since 0.3.2소스source

중력 반환.

Returns the gravity.

반환Returnsf64 지금 중력. (픽셀/초²)Current gravity. (pixels/second²)

짝Counterpart: vanilla.js PlatformerBody.getGravity

메서드methodpub fn set_ground_y(&mut self, ground_y: f64)#0.3.2 부터since 0.3.2소스source

바닥 높이 설정. (부모 영역 기준 로컬 Y)

Sets the ground height. (local Y relative to the parent area)

ground_yf64
바닥 높이. (로컬 Y, 이 값 아래로 내려가지 않는다)Ground height. (local Y, it does not go below this value)

짝Counterpart: vanilla.js PlatformerBody.setGroundY

메서드methodpub fn get_ground_y(&self) -> f64#0.3.2 부터since 0.3.2소스source

바닥 높이 반환.

Returns the ground height.

반환Returnsf64 지금 바닥 높이. (로컬 Y)Current ground height. (local Y)

짝Counterpart: vanilla.js PlatformerBody.getGroundY

메서드methodpub fn set_horizontal_bounds(&mut self, left_bound: Option<f64>, right_bound: Option<f64>)#0.3.2 부터since 0.3.2소스source

좌우 이동 경계 설정. (None 이면 그 쪽은 막지 않는다)

Sets the horizontal movement bounds. (None leaves that side unbounded)

left_boundOption<f64>
왼쪽 경계. (로컬 X, 이보다 작아지지 않는다)Left bound. (local X, it does not go below this)
right_boundOption<f64>
오른쪽 경계. (로컬 X, 이보다 커지지 않는다)Right bound. (local X, it does not go above this)

짝Counterpart: vanilla.js PlatformerBody.setHorizontalBounds

메서드methodpub fn is_grounded(&self) -> bool#0.3.2 부터since 0.3.2소스source

접지 중인지.

Whether it is grounded.

반환Returnsbool 지난 적분에서 바닥에 닿아 있었으면 true. 뛰면 false 가 된다.true if it was touching the ground in the last integration. Becomes false on jumping.

짝Counterpart: vanilla.js PlatformerBody.isGrounded

메서드methodpub fn set_landed_event(&mut self, landed_event: impl FnMut() + 'static)#0.3.2 부터since 0.3.2소스source

착지 알림 설정. (공중에서 바닥에 닿는 순간)

Sets the landing callback. (the moment it touches the ground from the air)

landed_eventimpl FnMut() + 'static
착지할 때 부를 함수. (앞의 알림은 바꾼다)Function to call on landing. (replaces the previous notification)

짝Counterpart: vanilla.js PlatformerBody.setLandedEvent

예제Example착지 횟수 세기Count landings
use std::cell::Cell;
use std::rc::Rc;
use vanilla::{PlatformerBody, Vector2};

let landed_count = Rc::new(Cell::new(0));
let landed_count_for_event = landed_count.clone();
let mut body = PlatformerBody::new();
body.set_ground_y(10.0);
body.set_landed_event(move || {
	landed_count_for_event.set(landed_count_for_event.get() + 1);
});
let mut position = Vector2::zero();
for _ in 0..30 {
	position = body.integrate(position, 0.1);
}
assert_eq!(landed_count.get(), 1);