Skip to content

Commit

Permalink
use raycast to check ground for jump
Browse files Browse the repository at this point in the history
  • Loading branch information
redthing1 committed Jul 30, 2020
1 parent fbb8403 commit 5ee354f
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions demo/blocks/source/comp/body.d
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,33 @@ class Character : Component, Updatable {

private InputController controller;
private DynamicBody bod;
private PhysicsManager phys;

override void setup() {
controller = entity.get_component!InputController();
bod = entity.get_component!DynamicBody();
phys = entity.scene.get_manager!PhysicsManager().get;

bod.max_speed = move_speed;
}

override void update() {
if (controller.jump.is_pressed) {
// check if on ground
auto rc = phys.raycast_from(bod, Vector3(0, -1, 0), Vector3(0, -1, 0), 1);
if (!rc.isNull) {
// jump
bod.apply_impulse(Vector3(0, bod.mass * 16, 0), Vector3Zero);
if (controller.jump.is_pressed) {
bod.apply_impulse(Vector3(0, bod.mass * 16, 0), Vector3Zero);
}
}

// movement
if (raymath.Vector2LengthSqr(controller.move.value) > 0) {
bod.apply_impulse(Vector3(controller.move.value.x * move_speed, 0,
controller.move.value.y * move_speed), Vector3Zero);
}

// turning
if (raymath.Vector2LengthSqr(controller.turn.value) > 0) {
bod.apply_torque(Vector3(controller.turn.value.y * turn_speed * bod.mass,
0, -controller.turn.value.x * turn_speed * bod.mass));
Expand Down

0 comments on commit 5ee354f

Please sign in to comment.