#[repr(C)]pub struct Sphere {
pub center: Vec3,
pub radius: f32,
}
Expand description
Represents a sphere in 3D space! Composed of a center point and a radius, can be used for raycasting, collision, visibility, and other things! https://stereokit.net/Pages/StereoKit/Sphere.html
§Examples
use stereokit_rust::{maths::{Vec3, Matrix, Sphere, Ray}, system::Lines,
mesh::Mesh, material::Material, util::named_colors};
let sphere = Sphere::new(Vec3::ZERO, 0.5);
let sphere_mesh = Mesh::generate_sphere(sphere.radius * 2.0, Some(12));
let mut material_sphere = Material::pbr().copy();
material_sphere.color_tint(named_colors::GOLD)
.border_size(0.05);
let scale = 0.1;
let transform = Matrix::t(sphere.center);
let ray_x = Ray::new(Vec3::X, Vec3::NEG_X);
let ray_y = Ray::new(Vec3::Y, Vec3::NEG_Y);
let contact_x = sphere.intersect(ray_x).expect("X Should be there");
let contact_y = sphere.intersect(ray_y).expect("Y Should be there");
assert_eq!(contact_x, Vec3::X * sphere.radius);
assert_eq!(contact_y, Vec3::Y * sphere.radius);
filename_scr = "screenshots/sphere.jpeg";
test_screenshot!( // !!!! Get a proper main loop !!!!
sphere_mesh.draw(token, &material_sphere, transform, None, None);
Lines::add_ray(token, ray_x, 0.30, named_colors::RED, None, 0.04);
Lines::add_ray(token, ray_y, 0.30, named_colors::GREEN, None, 0.04);
);

Fields§
§center: Vec3
Center of the sphere
radius: f32
Distance from the center, to the surface of the sphere, in meters. Half the diameter.
Implementations§
Source§impl Sphere
impl Sphere
Sourcepub fn new<V: Into<Vec3>>(center: V, radius: f32) -> Sphere
pub fn new<V: Into<Vec3>>(center: V, radius: f32) -> Sphere
Creates a Sphere directly from the ax + by + cz + d = 0 formula! https://stereokit.net/Pages/StereoKit/Sphere.html
§Examples
use stereokit_rust::maths::{Vec3, Sphere};
let sphere = Sphere::new(Vec3::ZERO, 0.5);
let sphere_b = Sphere {center : Vec3::ZERO, radius : 0.5};
assert_eq!(sphere, sphere_b);
Sourcepub fn contains<V: Into<Vec3>>(&self, point: V) -> bool
pub fn contains<V: Into<Vec3>>(&self, point: V) -> bool
A fast check to see if the given point is contained in or on a sphere! https://stereokit.net/Pages/StereoKit/Sphere/Contains.html
see also sphere_point_contains
§Examples
use stereokit_rust::maths::{Vec3, Sphere};
let sphere = Sphere::new(Vec3::ZERO, 0.5);
assert!(sphere.contains(Vec3::ONE * 0.28), "Should be contained");
assert!(!sphere.contains(Vec3::ONE * 0.29), "Should not be contained");
Sourcepub fn intersect(&self, ray: Ray) -> Option<Vec3>
pub fn intersect(&self, ray: Ray) -> Option<Vec3>
Intersects a ray with this sphere, and finds if they intersect, and if so, where that intersection is! This only finds the closest intersection point to the origin of the ray. https://stereokit.net/Pages/StereoKit/Sphere/Intersect.html
- ray - A ray to intersect with
Returns the closest intersection point to the ray’s origin. Or None it there is no intersection.
see also sphere_ray_intersect
same as Ray::intersect_sphere
§Examples
use stereokit_rust::maths::{Vec3, Sphere, Ray};
let sphere = Sphere::new(Vec3::ZERO, 0.5);
assert_eq!(sphere.intersect(Ray::new(Vec3::Z, Vec3::NEG_Z)), Some(Vec3::Z * 0.5));
assert_ne!(sphere.intersect(Ray::new(Vec3::Z, Vec3::Z)), None);
Trait Implementations§
Source§impl Display for Sphere
impl Display for Sphere
Source§fn fmt(&self, f: &mut Formatter<'_>) -> Result
fn fmt(&self, f: &mut Formatter<'_>) -> Result
Creates a text description of the Sphere, in the format of “[center:X radius:X]”
§Examples
use stereokit_rust::maths::{Vec3, Sphere};
let sphere = Sphere::new([1.1, 2.0, 3.0], 4.0);
assert_eq!(format!("{}", sphere),
"[center:[x:1.1, y:2, z:3] radius:4]");