Rigidbody2D
2D rigid body physics component bound to the native engine runtime.
Namespace:
namespace Lenga\Engine\Core;
class Rigidbody2D
Methods
isTouching
public function isTouching(bool $includeTriggers = true, ?int $layerMask = null)
Rigid body mode used by the physics solver (for example: Dynamic, Kinematic, Static).
isGrounded
public function isGrounded(float $minSupportDot = 0.5,
bool $includeTriggers = false,
?int $layerMask = null,)
Returns true when this body is resting on a supporting surface.
getContacts
public function getContacts(bool $includeTriggers = true, ?int $layerMask = null)
Returns all current contacts that match the query.
addForce
public function addForce(Vector2 $force, ForceMode2D $mode = ForceMode2D::Force)
Example
use Lenga\Engine\Core\Behaviour;
use Lenga\Engine\Core\Rigidbody2D;
use Lenga\Engine\Core\Vector2;
use Lenga\Engine\Core\ForceMode2D;
class PlayerMovement2D extends Behaviour
{
public float $jumpForce = 10.0;
public function update(): void
{
$rb = $this->getComponent(Rigidbody2D::class);
// Apply horizontal movement
$moveInput = Input::getAxis('Horizontal');
$rb->velocity = new Vector2($moveInput * 5.0, $rb->velocity->y);
// Jump when grounded
if (Input::getKeyDown(KeyCode::SPACE) && $rb->isGrounded()) {
$rb->addForce(new Vector2(0, $this->jumpForce), ForceMode2D::Impulse);
}
}
}