raylib-rs icon indicating copy to clipboard operation
raylib-rs copied to clipboard

Physac port

Open filipencopav opened this issue 5 years ago • 14 comments
trafficstars

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?

filipencopav avatar Feb 25 '20 15:02 filipencopav

stupid question, sorry

filipencopav avatar Feb 25 '20 15:02 filipencopav

Go ahead dude. Chat on discord if you need help.

Dacode45 avatar Feb 25 '20 17:02 Dacode45

Thanks!

filipencopav avatar Feb 26 '20 16:02 filipencopav

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?

filipencopav avatar Feb 28 '20 18:02 filipencopav

@theamazingwaffle @Dacode45 physac module was an user contribution and it requires some review (and probably some redesign), just keep that in mind.

raysan5 avatar Feb 28 '20 19:02 raysan5

@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!

filipencopav avatar Feb 28 '20 19:02 filipencopav

@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.

Dacode45 avatar Feb 28 '20 20:02 Dacode45

https://github.com/victorfisac/Physac/issues/40#issue-579413065

Do you know anything about this?

filipencopav avatar Mar 11 '20 17:03 filipencopav

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

filipencopav avatar Mar 14 '20 12:03 filipencopav

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.

Dacode45 avatar Mar 14 '20 17:03 Dacode45

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()

filipencopav avatar Mar 14 '20 17:03 filipencopav

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.

filipencopav avatar Mar 25 '20 09:03 filipencopav

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.

Dacode45 avatar Mar 25 '20 15:03 Dacode45

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.

filipencopav avatar Mar 25 '20 19:03 filipencopav