1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
//!
//! Provides some useful 2d geometry functions.
//!
#[macro_use]
extern crate more_asserts;


extern crate axgeom;

pub use dists;

///2d bot library where a bot is a 2d particle with mass.
pub mod bot;


///2d grid library with the ability to the raycast to detect
///which cell a ray hits.
pub mod grid;

use axgeom::num_traits::Float;
use axgeom::num_traits::NumAssign;
use axgeom::num_traits::Zero;
use axgeom::ordered_float::NotNan;
use axgeom::vec2;
use axgeom::Rect;
use axgeom::Vec2;
use core::ops::Neg;

///Basic Number Trait
pub trait MyNum: Zero + Copy + NumAssign + PartialOrd + Neg<Output = Self> {}
impl<T: Zero + Copy + NumAssign + PartialOrd + Neg<Output = Self>> MyNum for T {}

///NotNan f64
pub type F64n = NotNan<f64>;
///NotNan f64
pub type F32n = NotNan<f32>;

pub fn f32n(a: f32) -> F32n {
    NotNan::new(a).unwrap()
}

///convert an array of elements of type B to type A.
pub fn array2_inner_into<B: Copy, A: From<B>>(a: [B; 2]) -> [A; 2] {
    let x = A::from(a[0]);
    let y = A::from(a[1]);
    [x, y]
}

use core::convert::TryFrom;

///convert an array of elements of type B to type A.
pub fn array2_inner_try_into<B: Copy, A: TryFrom<B>>(a: [B; 2]) -> Result<[A; 2], A::Error> {
    let x = A::try_from(a[0]);
    let y = A::try_from(a[1]);
    match (x, y) {
        (Ok(x), Ok(y)) => Ok([x, y]),
        (Ok(_), Err(e)) => Err(e),
        (Err(e), Ok(_)) => Err(e),
        (Err(e1), Err(_)) => Err(e1),
    }
}


///Returns the force to be exerted to the first object.
///The force to the second object can be retrieved simply by negating the first.
pub fn gravitate<N:Float+MyNum >(
    bots:[(Vec2<N>,N,&mut Vec2<N>);2],
    min: N,
    gravity_const: N,
) -> Result<(), ErrTooClose> {
    let [(p1,m1,f1),(p2,m2,f2)]=bots;

    let diff = p2 - p1;
    let dis_sqr = diff.magnitude2();

    if dis_sqr > min {
        //newtons law of gravitation (modified for 2d??? divide by len instead of sqr)
        let force = gravity_const * (m1 * m2) / dis_sqr;

        let dis = Float::sqrt(dis_sqr);

        let final_vec = diff * (force / dis);

        *f1+=final_vec;
        *f2-=final_vec;
        Ok(())
    } else {
        Err(ErrTooClose)
    }
}

///If we repel too close, because of the inverse square we might get overlow problems.
pub struct ErrTooClose;

///Repel one object by simply not calling add_force on the other.
pub fn repel_one<N:Float+MyNum>(
    pos1: Vec2<N>,
    force_buffer:&mut Vec2<N>,
    pos2: Vec2<N>,
    closest: N,
    mag: N,
) -> Result<(), ErrTooClose> {

    let diff = pos2 - pos1;

    let len_sqr = diff.magnitude2();

    if len_sqr < closest {
        return Err(ErrTooClose);
    }

    let len = Float::sqrt(len_sqr);
    let mag = mag / len;

    let force = diff.normalize_to(mag);

    *force_buffer-=force;

    Ok(())
}


pub fn linear_push<N: Float+MyNum>(
    bots:[(Vec2<N>,&mut Vec2<N>);2],
    closest: N,
    mag: N,
) -> Result<(), ErrTooClose> {
    let [(bot1_pos,bot1_force_buffer),(bot2_pos,bot2_force_buffer)]=bots;

    let diff = bot2_pos - bot1_pos;

    let len_sqr = diff.magnitude2();

    if len_sqr < closest {
        return Err(ErrTooClose);
    }

    let len = len_sqr.sqrt();
    let mag = mag / len;

    let force = diff.normalize_to(mag);

    *bot1_force_buffer-=force;
    *bot2_force_buffer+=force;

    Ok(())
}

///Repel two objects.
///First vector is position. Second vector is force buffer
pub fn repel<N: Float+MyNum>(
    bots:[(Vec2<N>,&mut Vec2<N>);2],
    closest: N,
    mag: N,
) -> Result<(), ErrTooClose> {
    let [(bot1_pos,bot1_force_buffer),(bot2_pos,bot2_force_buffer)]=bots;

    let diff = bot2_pos - bot1_pos;

    let len_sqr = diff.magnitude2();

    if len_sqr < closest {
        return Err(ErrTooClose);
    }

    let len = len_sqr.sqrt();
    let mag = mag / len;

    let force = diff.normalize_to(mag);

    *bot1_force_buffer-=force;
    *bot2_force_buffer+=force;

    Ok(())
}



///Collides and bounces an object with a border
pub fn collide_with_border<N:MyNum>(
    pos:&mut Vec2<N>,
    vel:&mut Vec2<N>,
    rect2: &axgeom::Rect<N>,
    drag: N,
) {
    let xx = rect2.get_range(axgeom::XAXIS);
    let yy = rect2.get_range(axgeom::YAXIS);

    if pos.x < xx.start {
        pos.x = xx.start;
        vel.x = -vel.x;
        vel.x *= drag;
    }
    if pos.x > xx.end {
        pos.x = xx.end;
        vel.x = -vel.x;
        vel.x *= drag;
    }
    if pos.y < yy.start {
        pos.y = yy.start;
        vel.y = -vel.y;
        vel.y *= drag;
    }
    if pos.y > yy.end {
        pos.y = yy.end;
        vel.y = -vel.y;
        vel.y *= drag;
    }
}

///Forces a position to be within the specified rect.
#[inline(always)]
pub fn stop_wall<N: MyNum>(pos: &mut Vec2<N>, rect: Rect<N>) {
    let start = vec2(rect.x.start, rect.y.start);
    let dim = vec2(rect.x.end, rect.y.end);
    if pos.x > dim.x {
        pos.x = dim.x;
    } else if pos.x < start.x {
        pos.x = start.x;
    }

    if pos.y > dim.y {
        pos.y = dim.y;
    } else if pos.y < start.y {
        pos.y = start.y;
    }
}

///Wraps the first point around the rectangle made between (0,0) and dim.
pub fn wrap_position<N: MyNum>(a: &mut Vec2<N>, dim: Rect<N>) {
    let ((a_, b), (c, d)) = dim.get();

    let start = vec2(a_, c);
    let dim = vec2(b, d);

    if a.x > dim.x {
        a.x = start.x
    } else if a.x < start.x {
        a.x = dim.x;
    }

    if a.y > dim.y {
        a.y = start.y;
    } else if a.y < start.y {
        a.y = dim.y;
    }
}

///Describes a cardinal direction..
pub enum WallSide {
    Above,
    Below,
    LeftOf,
    RightOf,
}

///Returns which cardinal direction the specified rectangle is closest to.
pub fn collide_with_rect<N: Float>(
    botr: &axgeom::Rect<N>,
    wallr: &axgeom::Rect<N>,
) -> Option<WallSide> {
    let wallx = wallr.get_range(axgeom::XAXIS);
    let wally = wallr.get_range(axgeom::YAXIS);

    let center_bot = botr.derive_center();
    let center_wall = wallr.derive_center();

    let ratio = (wallx.end - wallx.start) / (wally.end - wally.start);

    //Assuming perfect square
    let p1 = vec2(N::one(), ratio);
    let p2 = vec2(N::zero() - N::one(), ratio);

    let diff = center_bot - center_wall;

    let d1 = p1.dot(diff);
    let d2 = p2.dot(diff);
    let zero = N::zero();

    use std::cmp::Ordering::*;

    let ans = match [d1.partial_cmp(&zero)?, d2.partial_cmp(&zero)?] {
        [Less, Less] => {
            //top
            WallSide::Above
        }
        [Less, Equal] => {
            //topstart
            WallSide::Above
        }
        [Less, Greater] => {
            //start
            WallSide::LeftOf
        }
        [Greater, Less] => {
            //end
            WallSide::RightOf
        }
        [Greater, Equal] => {
            //bottom end
            WallSide::Below
        }
        [Greater, Greater] => {
            //bottom
            WallSide::Below
        }
        [Equal, Less] => {
            //top end
            WallSide::Above
        }
        [Equal, Equal] => {
            //Directly in the center
            WallSide::Above
        }
        [Equal, Greater] => {
            //bottom start
            WallSide::Below
        }
    };
    Some(ans)
}