Skip to main content

Physics

Struct Physics 

Source
pub struct Physics {
    pub compute_shader: ComputeShaderType,
    pub collision_type: CollisionType,
    pub mass: f32,
    pub bounciness: f32,
    pub friction: f32,
    pub gravity_scale: f32,
    pub linear_velocity: [f32; 3],
    pub angular_velocity: [f32; 3],
}
Expand description

Core physic definition that describe all possible cases in physic world.

Physic can be very lightweight and very heavy in same time, its based on which physic shader you use ComputeShaderType and what is your physic tickrate for example Minecraft has 20 tickrate this engine is using 60 tickrate.

§Example

use rusting_engine::{ CollisionType, ComputeShaderType, Physics };
 
// Create floating cube that use maximal realistic shader
let floating_physic = &Physics::default()
    .compute_shader(ComputeShaderType::FullPhysics) // FullPhysics has all possible physics cases as rotation, gravity, collisions, impulse and etc.
    .mass(1.0) // equal to 1kg. More mass = more impulse when pushing other objects and etc.
    .collision_type(CollisionType::Box) // Box collision
    .gravity_scale(0.0); // Floating cube that is not pushed by gravity
 
// Create empty physic, no collision, no gravity, nothing, can be used to turn off physic or for debug
let empty_physic = &Physics::default()
    .compute_shader(ComputeShaderType::Empty);

Fields§

§compute_shader: ComputeShaderType§collision_type: CollisionType§mass: f32§bounciness: f32§friction: f32§gravity_scale: f32§linear_velocity: [f32; 3]§angular_velocity: [f32; 3]

Implementations§

Source§

impl Physics

Source

pub fn bounciness(self, val: f32) -> Self

Bounciness will describe how much speed object loses when bouncing.

  • 0.0 - No bounce (like a rock)
  • 0.5 - Medium bounce (like a basketball)
  • 1.0 - Perfect bounce (never stops)

Note: Values outside 0.0-1.0 are clamped automatically.

Source

pub fn friction(self, val: f32) -> Self

Friction is used to describe how good can object slide on other object Range ( 0.0 - 1.0 )

Source

pub fn gravity_scale(self, val: f32) -> Self

Gravity scale describe how strong does object effected by gravity, if 1, the gravity power will be same as World Gravity.

Gravity = 0 => floating object

Gravity < 0 => object will be pushed in opposite direction

Source

pub fn angular_velocity(self, val: [f32; 3]) -> Self

The speed of object rotating in radians per second.

§Example
// Rotate 180 degrees per second (π radians/sec)
let spinning = Physics::default()
    .angular_velocity([3.14159, 0.0, 0.0]);
Source

pub fn linear_velocity(self, v: [f32; 3]) -> Self

The speed of object displace in some amount of time

Source

pub fn mass(self, m: f32) -> Self

With bigger mass, object can create bigger push impulse, object is more hard to displace.

So mass is treated as real world mass.

Source

pub fn collision_type(self, c: CollisionType) -> Self

This will create invisible collision around object based on chosen shape.

This Function is using CollisionType

§Example
 // Invisible Box collision
 let box_collision_physic = &Physics::default()
    .collision_type(CollisionType::Box); 
 
// Invisible Sphere collision
let sphere_collision_physic = &Physics::default()
    .collision_type(CollisionType::Sphere); 
Source

pub fn compute_shader(self, c: ComputeShaderType) -> Self

Set shaders to describe how Object will interact with physical world.

Highly recommended to read about ComputeShaderType to choose right shader for right object and never lose performance or realism.

Trait Implementations§

Source§

impl Clone for Physics

Source§

fn clone(&self) -> Physics

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Physics

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Physics

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Copy for Physics

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.