fmod/core/geometry/
spatialization.rs

1// Copyright (c) 2024 Melody Madeline Lyons
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at https://mozilla.org/MPL/2.0/.
6
7use fmod_sys::*;
8use std::mem::MaybeUninit;
9
10use crate::{FmodResultExt, Result};
11use crate::{Geometry, Vector};
12
13impl Geometry {
14    /// Sets the 3D position of the object.
15    ///
16    /// Position is in world space.
17    pub fn set_position(&self, position: Vector) -> Result<()> {
18        unsafe {
19            FMOD_Geometry_SetPosition(self.inner.as_ptr(), std::ptr::from_ref(&position).cast())
20                .to_result()
21        }
22    }
23
24    /// Retrieves the 3D position of the object.
25    ///
26    /// Position is in world space.
27    pub fn get_position(&self) -> Result<Vector> {
28        let mut position = MaybeUninit::uninit();
29        unsafe {
30            FMOD_Geometry_GetPosition(self.inner.as_ptr(), position.as_mut_ptr()).to_result()?;
31            let position = position.assume_init().into();
32            Ok(position)
33        }
34    }
35
36    /// Sets the 3D orientation of the object.
37    ///
38    /// See remarks in [`crate::System::set_3d_listener_attributes`] for more description on forward and up vectors.
39    pub fn set_rotation(&self, forward: Vector, up: Vector) -> Result<()> {
40        unsafe {
41            FMOD_Geometry_SetRotation(
42                self.inner.as_ptr(),
43                std::ptr::from_ref(&forward).cast(),
44                std::ptr::from_ref(&up).cast(),
45            )
46            .to_result()
47        }
48    }
49
50    /// Retrieves the 3D orientation of the object.
51    pub fn get_rotation(&self) -> Result<(Vector, Vector)> {
52        let mut forward = MaybeUninit::uninit();
53        let mut up = MaybeUninit::uninit();
54        unsafe {
55            FMOD_Geometry_GetRotation(self.inner.as_ptr(), forward.as_mut_ptr(), up.as_mut_ptr())
56                .to_result()?;
57            let forward = forward.assume_init().into();
58            let up = up.assume_init().into();
59            Ok((forward, up))
60        }
61    }
62
63    /// Sets the 3D scale of the object.
64    ///
65    /// An object can be scaled/warped in all 3 dimensions separately using this function without having to modify polygon data.
66    pub fn set_scale(&self, scale: Vector) -> Result<()> {
67        unsafe {
68            FMOD_Geometry_SetScale(self.inner.as_ptr(), std::ptr::from_ref(&scale).cast())
69                .to_result()
70        }
71    }
72
73    /// Retrieves the 3D scale of the object.
74    pub fn get_scale(&self) -> Result<Vector> {
75        let mut scale = MaybeUninit::uninit();
76        unsafe {
77            FMOD_Geometry_GetScale(self.inner.as_ptr(), scale.as_mut_ptr()).to_result()?;
78            let scale = scale.assume_init().into();
79            Ok(scale)
80        }
81    }
82}