nightshade-api 0.40.0

Procedural high level API for the nightshade game engine
Documentation
//! Forces, raycasts, and collision events for entities spawned with a
//! [`Body`](crate::prelude::Body).

use nightshade::ecs::physics::joints::{
    FixedJoint, JointAxisDirection, JointHandle, RevoluteJoint, RopeJoint, SpringJoint,
    create_fixed_joint, create_revolute_joint, create_rope_joint, create_spring_joint,
};
use nightshade::prelude::*;

/// Applies an instantaneous impulse to a dynamic entity. Good for jumps,
/// knockback, and explosions.
#[inline]
pub fn push(world: &mut World, entity: Entity, impulse: Vec3) {
    physics_world_apply_impulse(&mut world.resources.physics, entity, impulse);
}

/// Sets a dynamic entity's linear velocity directly. Good for launching
/// projectiles.
#[inline]
pub fn set_velocity(world: &mut World, entity: Entity, velocity: Vec3) {
    physics_world_set_linear_velocity(&mut world.resources.physics, entity, velocity);
}

/// Casts a ray against all physics colliders and returns the closest hit.
#[inline]
pub fn raycast(
    world: &World,
    origin: Vec3,
    direction: Vec3,
    max_distance: f32,
) -> Option<RaycastHit> {
    physics_world_cast_ray(
        &world.resources.physics,
        origin,
        direction,
        max_distance,
        None,
    )
}

/// The collision events from the last physics step.
#[inline]
pub fn collisions(world: &World) -> &[CollisionEvent] {
    physics_world_collision_events(&world.resources.physics)
}

/// Welds two bodies together rigidly.
#[inline]
pub fn attach_fixed(world: &mut World, parent: Entity, child: Entity) -> Option<JointHandle> {
    create_fixed_joint(world, parent, child, FixedJoint::new())
}

/// Hinges two bodies around an axis. Doors, levers, wheels.
#[inline]
pub fn attach_hinge(
    world: &mut World,
    parent: Entity,
    child: Entity,
    axis: JointAxisDirection,
) -> Option<JointHandle> {
    create_revolute_joint(world, parent, child, RevoluteJoint::new(axis))
}

/// Connects two bodies with a spring. Suspension, bouncy attachments.
#[inline]
pub fn attach_spring(
    world: &mut World,
    parent: Entity,
    child: Entity,
    rest_length: f32,
    stiffness: f32,
    damping: f32,
) -> Option<JointHandle> {
    create_spring_joint(
        world,
        parent,
        child,
        SpringJoint::new(rest_length, stiffness, damping),
    )
}

/// Tethers two bodies with a maximum separation. Chains, leashes, pendulums.
#[inline]
pub fn attach_rope(
    world: &mut World,
    parent: Entity,
    child: Entity,
    max_distance: f32,
) -> Option<JointHandle> {
    create_rope_joint(world, parent, child, RopeJoint::new(max_distance))
}