Skip to content

Commit

Permalink
Added math functions to Rust bindings. [skip CI]
Browse files Browse the repository at this point in the history
  • Loading branch information
pigpigyyy committed Jan 4, 2025
1 parent c24169c commit a7480d9
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Tools/dora-rust/dora-test/src/tests/contact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ pub fn test() {
let radius = 300.0;
let mut vertices = vec![];
for i in 0..=count {
let angle = 2.0 * std::f32::consts::PI * i as f32 / count as f32;
vertices.push(Vec2::new(radius * angle.cos(), radius * angle.sin()));
let angle = 2.0 * Math::PI * i as f32 / count as f32;
vertices.push(Vec2::new(radius * Math::cos(angle), radius * Math::sin(angle)));
}
terrain_def.attach_chain(&vertices, 0.4, 0.0);
terrain_def.attach_disk_with_center(&Vec2::new(0.0, -270.0), 30.0, 1.0, 0.0, 1.0);
Expand Down
7 changes: 4 additions & 3 deletions Tools/dora-rust/dora-test/src/tests/entity_move.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ pub fn test() {
get_scene()
) {
if let Some(mut sprite) = Sprite::with_file(&image) {
sprite.set_size(&Size::new(300.0, 300.0));
sprite.add_to(&scene);
sprite.run_action_def(ActionDef::scale(0.5, 0.0, 0.5, EaseType::OutBack), false);
sprite.run_action_def(ActionDef::scale(0.5, 0.0, 1.0, EaseType::OutBack), false);
entity.set("sprite", sprite.obj());
}
}
Expand Down Expand Up @@ -73,7 +74,7 @@ pub fn test() {
return false;
}
let dir = (target - position).normalize();
let angle = dir.x.atan2(dir.y).to_degrees();
let angle = Math::deg(Math::atan2(dir.x, dir.y));
let new_pos = position + dir * speed;
let new_pos = new_pos.clamp(&position, &target);
entity.set("position", new_pos);
Expand Down Expand Up @@ -162,7 +163,7 @@ pub fn test() {
if let Some(mut sprite) = get_sprite(&entity) {
sprite.run_action_def(
ActionDef::sequence(&vec![
ActionDef::scale(0.5, 0.5, 0.0, EaseType::InBack),
ActionDef::scale(0.5, 1.0, 0.0, EaseType::InBack),
ActionDef::event("Destroy", "")
]), false);
sprite.slot("Destroy", Box::new(move |_| {
Expand Down
75 changes: 75 additions & 0 deletions Tools/dora-rust/dora/src/dora.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5702,6 +5702,81 @@ impl Nvg {
}
}

// math

extern "C" {
fn math_abs(v: f64) -> f64;
fn math_acos(v: f32) -> f32;
fn math_asin(v: f32) -> f32;
fn math_atan(v: f32) -> f32;
fn math_atan2(y: f32, x: f32) -> f32;
fn math_ceil(v: f32) -> f32;
fn math_cos(v: f32) -> f32;
fn math_deg(v: f32) -> f32;
fn math_exp(v: f32) -> f32;
fn math_floor(v: f32) -> f32;
fn math_fmod(x: f32, y: f32) -> f32;
fn math_log(v: f32) -> f32;
fn math_rad(v: f32) -> f32;
fn math_sin(v: f32) -> f32;
fn math_sqrt(v: f32) -> f32;
fn math_tan(v: f32) -> f32;
}

pub struct Math {}

impl Math {
pub const PI: f32 = std::f32::consts::PI;
pub fn abs(v: f64) -> f64 {
unsafe { math_abs(v) }
}
pub fn acos(v: f32) -> f32 {
unsafe { math_acos(v) }
}
pub fn asin(v: f32) -> f32 {
unsafe { math_asin(v) }
}
pub fn atan(v: f32) -> f32 {
unsafe { math_atan(v) }
}
pub fn atan2(y: f32, x: f32) -> f32 {
unsafe { math_atan2(y, x) }
}
pub fn ceil(v: f32) -> f32 {
unsafe { math_ceil(v) }
}
pub fn cos(v: f32) -> f32 {
unsafe { math_cos(v) }
}
pub fn deg(v: f32) -> f32 {
unsafe { math_deg(v) }
}
pub fn exp(v: f32) -> f32 {
unsafe { math_exp(v) }
}
pub fn floor(v: f32) -> f32 {
unsafe { math_floor(v) }
}
pub fn fmod(x: f32, y: f32) -> f32 {
unsafe { math_fmod(x, y) }
}
pub fn log(v: f32) -> f32 {
unsafe { math_log(v) }
}
pub fn rad(v: f32) -> f32 {
unsafe { math_rad(v) }
}
pub fn sin(v: f32) -> f32 {
unsafe { math_sin(v) }
}
pub fn sqrt(v: f32) -> f32 {
unsafe { math_sqrt(v) }
}
pub fn tan(v: f32) -> f32 {
unsafe { math_tan(v) }
}
}

use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
Expand Down

0 comments on commit a7480d9

Please sign in to comment.