diff --git a/Cargo.lock b/Cargo.lock index a693f8e..9462a79 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -46,11 +46,17 @@ dependencies = [ "wasi", ] +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + [[package]] name = "indoc" -version = "1.0.9" +version = "2.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bfa799dd5ed20a7e349f3b4639aa80d74549c81716d9ec4f994c9b5815598306" +checksum = "1e186cfbae8084e513daff4240b4797e342f988cecda4fb6c939150f96315fd8" [[package]] name = "libc" @@ -70,9 +76,9 @@ dependencies = [ [[package]] name = "memoffset" -version = "0.8.0" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d61c719bcfbcf5d62b3a09efa6088de8c54bc0bfcd3ea7ae39fcc186108b8de1" +checksum = "5a634b1c61a95585bd15607c6ab0c4e5b226e695ff2800ba0cdccddf208c406c" dependencies = [ "autocfg", ] @@ -106,6 +112,12 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "portable-atomic" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7170ef9988bc169ba16dd36a7fa041e5c4cbeb6a35b76d4c03daded371eae7c0" + [[package]] name = "ppv-lite86" version = "0.2.17" @@ -123,15 +135,16 @@ dependencies = [ [[package]] name = "pyo3" -version = "0.18.1" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06a3d8e8a46ab2738109347433cb7b96dffda2e4a218b03ef27090238886b147" +checksum = "53bdbb96d49157e65d45cc287af5f32ffadd5f4761438b527b055fb0d4bb8233" dependencies = [ "cfg-if", "indoc", "libc", "memoffset", "parking_lot", + "portable-atomic", "pyo3-build-config", "pyo3-ffi", "pyo3-macros", @@ -140,9 +153,9 @@ dependencies = [ [[package]] name = "pyo3-build-config" -version = "0.18.1" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "75439f995d07ddfad42b192dfcf3bc66a7ecfd8b4a1f5f6f046aa5c2c5d7677d" +checksum = "deaa5745de3f5231ce10517a1f5dd97d53e5a2fd77aa6b5842292085831d48d7" dependencies = [ "once_cell", "target-lexicon", @@ -150,9 +163,9 @@ dependencies = [ [[package]] name = "pyo3-ffi" -version = "0.18.1" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "839526a5c07a17ff44823679b68add4a58004de00512a95b6c1c98a6dcac0ee5" +checksum = "62b42531d03e08d4ef1f6e85a2ed422eb678b8cd62b762e53891c05faf0d4afa" dependencies = [ "libc", "pyo3-build-config", @@ -160,9 +173,9 @@ dependencies = [ [[package]] name = "pyo3-macros" -version = "0.18.1" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bd44cf207476c6a9760c4653559be4f206efafb924d3e4cbf2721475fc0d6cc5" +checksum = "7305c720fa01b8055ec95e484a6eca7a83c841267f0dd5280f0c8b8551d2c158" dependencies = [ "proc-macro2", "pyo3-macros-backend", @@ -172,11 +185,13 @@ dependencies = [ [[package]] name = "pyo3-macros-backend" -version = "0.18.1" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc1f43d8e30460f36350d18631ccf85ded64c059829208fe680904c65bcd0a4c" +checksum = "7c7e9b68bb9c3149c5b0cade5d07f953d6d125eb4337723c4ccdb665f1f96185" dependencies = [ + "heck", "proc-macro2", + "pyo3-build-config", "quote", "syn", ] @@ -243,9 +258,9 @@ checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0" [[package]] name = "syn" -version = "1.0.109" +version = "2.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +checksum = "79d9531f94112cfc3e4c8f5f02cb2b58f72c97b7efd85f70203cc6d8efda5927" dependencies = [ "proc-macro2", "quote", @@ -266,9 +281,9 @@ checksum = "e5464a87b239f13a63a501f2701565754bae92d243d4bb7eb12f6d57d2269bf4" [[package]] name = "unindent" -version = "0.1.11" +version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e1766d682d402817b5ac4490b3c3002d91dfa0d22812f341609f97b08757359c" +checksum = "c7de7d73e1754487cb58364ee906a499937a0dfabd86bcb980fa99ec8c8fa2ce" [[package]] name = "wasi" diff --git a/Cargo.toml b/Cargo.toml index e2ba62f..0d20614 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,5 +10,5 @@ crate-type = ["cdylib"] [dependencies] float_eq = "1" -pyo3 = "0.18.1" +pyo3 = "0.20.3" rand = "0.8.5" diff --git a/pyproject.toml b/pyproject.toml index 8b6a363..89b79fd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,5 +1,5 @@ [build-system] -requires = ["maturin>=0.14,<0.15"] +requires = ["maturin>=1.4.0,<1.5.0"] build-backend = "maturin" [project] diff --git a/python/arcade_accelerate/__init__.py b/python/arcade_accelerate/__init__.py index 7c00d00..e74fa08 100644 --- a/python/arcade_accelerate/__init__.py +++ b/python/arcade_accelerate/__init__.py @@ -24,12 +24,12 @@ def patch_hitboxes(patches): def patch_spritelist_collision(patches): - patches[ - "arcade.sprite_list.collision" - ].check_for_collision_with_list = arcade_accelerate.check_for_collision_with_list - patches[ - "arcade.sprite_list.collision" - ].check_for_collision_with_lists = arcade_accelerate.check_for_collision_with_lists + patches["arcade.sprite_list.collision"].check_for_collision_with_list = ( + arcade_accelerate.check_for_collision_with_list + ) + patches["arcade.sprite_list.collision"].check_for_collision_with_lists = ( + arcade_accelerate.check_for_collision_with_lists + ) def patch_math(patches): @@ -37,6 +37,6 @@ def patch_math(patches): def patch_geometry(patches): - patches[ - "arcade.geometry" - ].are_polygons_intersecting = arcade_accelerate.are_polygons_intersecting + patches["arcade.geometry"].are_polygons_intersecting = ( + arcade_accelerate.are_polygons_intersecting + ) diff --git a/src/geometry.rs b/src/geometry.rs index eb512f5..6d8b33e 100644 --- a/src/geometry.rs +++ b/src/geometry.rs @@ -2,6 +2,10 @@ use pyo3::prelude::*; #[pyfunction] pub fn are_polygons_intersecting(poly_a: Vec<(f32, f32)>, poly_b: Vec<(f32, f32)>) -> bool { + // If either polygon is empty, we should just return False + if poly_a.is_empty() || poly_b.is_empty() { + return false; + } let polygons = [poly_a, poly_b]; for polygon in &polygons { for i1 in 0..polygon.len() { @@ -171,6 +175,14 @@ mod tests { assert!(!result); } + #[test] + fn test_empty_polygons_intersecting() { + let poly_a: Vec<(f32, f32)> = vec![]; + let poly_b: Vec<(f32, f32)> = vec![]; + let result = are_polygons_intersecting(poly_a, poly_b); + assert!(!result); + } + #[test] fn test_is_point_in_box() { // point inside diff --git a/src/hitbox.rs b/src/hitbox.rs index 5b35dfc..34b3c84 100644 --- a/src/hitbox.rs +++ b/src/hitbox.rs @@ -158,8 +158,8 @@ impl RotatableHitBox { let rad_cos = rad.cos(); let rad_sin = rad.sin(); for point in super_.points.iter() { - let x = ((point.0 * rad_cos - point.1 * rad_sin) * super_.scale.0) + super_.position.0; - let y = ((point.0 * rad_sin + point.1 * rad_cos) * super_.scale.1) + super_.position.1; + let x = ((point.0 * rad_cos + point.1 * rad_sin) * super_.scale.0) + super_.position.0; + let y = ((-point.0 * rad_sin + point.1 * rad_cos) * super_.scale.1) + super_.position.1; new_points.push((x, y)); } diff --git a/src/math.rs b/src/math.rs index 385336f..8637e93 100644 --- a/src/math.rs +++ b/src/math.rs @@ -14,8 +14,8 @@ pub fn rotate_point(x: f32, y: f32, cx: f32, cy: f32, angle: f32) -> (f32, f32) let temp_y = y - cy; // rotate point - let xnew = (temp_x * c - temp_y * s) + cx; - let ynew = (temp_x * s + temp_y * c) + cy; + let xnew = (temp_x * c + temp_y * s) + cx; + let ynew = (-temp_x * s + temp_y * c) + cy; let precision = 10i32.pow(_PRECISION) as f32; let x_rounded = (xnew * precision).round() / precision; @@ -232,6 +232,11 @@ mod tests { use super::*; use float_eq::assert_float_eq; + fn round_float(n: f32, decimals: u32) -> f32 { + let nn = 10i32.pow(decimals) as f32; + (n * nn).round() / nn + } + #[test] fn test_clamp() { let mut result = clamp(1.2, 1.0, 2.0); @@ -392,4 +397,52 @@ mod tests { assert_eq!(result.x, 0.0); assert_eq!(result.y, 0.0); } + + #[test] + fn test_rotate_point() { + let mut x: f32 = 0.0; + let mut y: f32 = 0.0; + let mut cx: f32 = 0.0; + let mut cy: f32 = 0.0; + let mut angle: f32 = 0.0; + let mut point: (f32, f32) = rotate_point(x, y, cx, cy, angle); + assert_float_eq!(round_float(point.0, 2), 0.0, abs <= 1.0e-3); + assert_float_eq!(round_float(point.1, 2), 0.0, abs <= 1.0e-3); + + x = 0.0; + y = 0.0; + cx = 0.0; + cy = 0.0; + angle = 90.0; + point = rotate_point(x, y, cx, cy, angle); + assert_float_eq!(round_float(point.0, 2), 0.0, abs <= 1.0e-3); + assert_float_eq!(round_float(point.1, 2), 0.0, abs <= 1.0e-3); + + x = 50.0; + y = 50.0; + cx = 0.0; + cy = 0.0; + angle = 0.0; + point = rotate_point(x, y, cx, cy, angle); + assert_float_eq!(round_float(point.0, 2), 50.0, abs <= 1.0e-3); + assert_float_eq!(round_float(point.1, 2), 50.0, abs <= 1.0e-3); + + x = 50.0; + y = 0.0; + cx = 0.0; + cy = 0.0; + angle = 90.0; + point = rotate_point(x, y, cx, cy, angle); + assert_float_eq!(round_float(point.0, 2), 0.0, abs <= 1.0e-3); + assert_float_eq!(round_float(point.1, 2), -50.0, abs <= 1.0e-3); + + x = 20.0; + y = 10.0; + cx = 10.0; + cy = 10.0; + angle = 180.0; + point = rotate_point(x, y, cx, cy, angle); + assert_float_eq!(round_float(point.0, 2), 0.0, abs <= 1.0e-3); + assert_float_eq!(round_float(point.1, 2), 10.0, abs <= 1.0e-3); + } }