raylib-rs
raylib-rs copied to clipboard
Physac port
I'm new to rust (an programming in general), but can i try?
What are the formatting rules, naming conventions, stylistic choices, all that kind of stuff?
stupid question, sorry
Go ahead dude. Chat on discord if you need help.
Thanks!
Hey @dacode45 so i need to clarify some things:
- physac uses a lot of global vars, i'll put some into a
World - i need a place to store the bodies, physac uses a PhysicsBodies[MAX_PHYSAC_BODIES], should i use a Vector<PhysicsBodies> or should i use the array way?
- i want to make each PhysicsBody type (Circle, Rectangle, Polygon) its own type, should i or should i stick with PhysicsBody?
@theamazingwaffle @Dacode45 physac module was an user contribution and it requires some review (and probably some redesign), just keep that in mind.
@raysan5 i am no expert but i read bob martin's book and i could try and apply some rules to it, but if i'll be doing something wrong, just tell me please, thanks!
@theamazingwaffle you should create a new global struct for physac.
-
RaylibHandle has a lot of mutability rules so you'll be fighting the borrow checker if put physac stuff in there.
-
use a vector because arrays past size 32 dont have iterator trait implementations
-
if you are comfortable with generics make each it's own type otherwise use an enum to group each shape into one PhysicsBody type.
https://github.com/victorfisac/Physac/issues/40#issue-579413065
Do you know anything about this?
use a vector because arrays past size 32 dont have iterator trait implementations
It seems that i can do iterators with arrays:
fn main() {
use std::io::{ self, Write };
let buf = [0_u32; 480];
for (i, v) in buf.iter().enumerate() {
if (i % 12) == 0 { print!("\n") }
print!("{}", v);
}
io::stdout().flush().unwrap();
}
This code runs, i can use iterators with arrays, so is there any other reason not to use arrays?
Also how should i deal with giving users handles of bodies when using
world.create_physics_body(circle);?
In physac, you get pointers to that body, but in rust, you can't have multiple mutable references to an array/vector
That shouldn't work on stable. https://doc.rust-lang.org/std/primitive.array.html.
Are you using nightly? Raylib-rs is stable only right now.
Rust 14.2 just got released and i am using stable
~ $ rustup show
Default host: x86_64-unknown-linux-gnu
rustup home: /home/pavel/.rustup
stable-x86_64-unknown-linux-gnu (default)
rustc 1.42.0 (b8cedc004 2020-03-09)
Have you tried compiling the snippet? It compiles for me.
Oh and it also works with mutable iterators long_array.iter_mut()
I don't know what this code does.
struct timespec now;
if (clock_gettime(CLOCK_MONOTONIC, &now) == 0)
frequency = 1000000000;
I searched and apparently if getting the MONOTONIC clock is successful then the frequency is set to that value. Is there a rust thing for this?
I found out that std::time::Instant::now() calls clock_gettime(Monotonic Clock) but does it guarantee that it will not fail?
The windows version looks like this:
QueryPerformanceFrequency((unsigned long long int *) &frequency);
which just sets the frequency.
Monotonic time is an extremely hard problem. RUST actually ensures that this doesn't fail even when the underlying system call can fail. See https://doc.rust-lang.org/src/std/time.rs.html#203.
For rust, it might be best to just force the frequency to that value instead of looking for an equivalent if statement.
I was told to use std::time::Instant here
But i wonder if it replaces all of the timer variables.
If yes, then I'm going to use it instead.