• Hacker News
  • new|
  • comments|
  • show|
  • ask|
  • jobs|
  • iamcoder18 1 days

    It's hand written and should also work on microcontrollers like ESP32s or Pi Pico 2. Have you tried anything like that yet?

    The Pico in particular has enough RAM and supports floating point math.

  • songhonglei1985 23 hours

    single-file physics for old consoles is such a neat constraint to build under. reminds me of the n64 homebrew stuff i was looking at - wonder if this actually runs on real hardware or just emulators, the psx/n64 have such tight memory limits. anyone benchmarked it on a real dev kit?

  • chrisjj 21 hours

    Perhaps add README.md?

  • Attummm 1 days

    Sounds interesting, but there isn't any description or readme to go by.

    bouchard 1 days

    The README is in the header file (picophysics.h)

    monkpit 1 days

    why not in a readme

    Retr0id 1 days

    It's common enough to just copy single-file headers into a project. Now the docs come with it.

    abnercoimbre 1 days

    Yeah I didn’t think twice about clicking the header to read the docs. Standard practice.

  • klodolph 1 days

    Everything being static inline makes it hostile to these older systems which have limited RAM. Use of float makes it unlikely that you’d run it on the PlayStation, which has no FPU.

    wren6991 19 hours

    > Everything being static inline makes it hostile to these older systems which have limited RAM

    If you don't define `PICOPHYSICS_IMPLEMENTATION` then you just get prototypes and a few struct definitions. From what I could tell the static inline functions are just used as helpers by the non-static functions defined in the implementation TU. It's similar to the stb[1] implementation pattern.

    Also, inlining improves code size more often than you might think. There is a lot of ceremony in a function call, and the compiler has to make pessimistic assumptions about what is trashed by the call, causing unnecessary spills and fills. Things like the simple vec3 operations here should pretty much always be made available for inlining; whether the compiler actually decides to inline them is a different story.

    [1]: https://github.com/nothings/stb

    ColdStream 1 days

    I was going to say, even the readme file only mentions N64 and DC.

    yellowapple 23 hours

    Not just limited RAM, but limited RAM bandwidth, which seems like it would make this particularly painful on the N64 (ironic, given that the in-file README says that this came about specifically to support porting a game to the N64). That said, should be possible to wrap these with some non-inlined functions in a pinch, yeah?

    Re: floats, I wonder how well it'd perform if compiled with soft-float support?

    gsliepen 21 hours

    > Everything being static inline makes it hostile to these older systems which have limited RAM.

    Compilers can un-inline as well. In fact, since every function is defined in the header file, the static inline annotation means very little, it's more of a hint; the functions that were not marked static inline can be inlined just as well by the compiler.

    aaaronic 1 days

    Most of the older consoles don't have an FPU. Fixed point is king on those older systems.

    klodolph 1 days

    Only one of the consoles listed lacks an FPU, the other two (N64 and DreamCast) both have FPUs. Despite its age, the FPU on the N64 was plenty fast and it was used extensively.

    phire 22 hours

    The N64's FPU will usually be faster than doing fixed point on the CPU.

    You are actually multiplier/divider bound, and the CPU can multiply about 10 bits per cycle and only divide 1 bit per cycle. Since you aren't multiplying the sign/exponent bits, it's faster to multiply/divide the 24 mantissa bits of a 32-bit float than it is to multiply/divide the 32 bits of an int.

    And with fixed point, you then have to throw in the extra shift instruction (which floating point automatically does internally).

    Though, this only applies to fixed point on the CPU. The RSP has no FPU, but it does have a 128 bit vector unit with 8 16bit lanes which is pretty good at fixed point stuff. If you can vectorise your algorithm, it will be faster on the RSP.

    inigyou 18 hours

    According to Kaze Emanuar, the N64 is actually memory bandwidth bound in almost everything it does.

    embedding-shape 21 hours

    > Use of float makes it unlikely that you’d run it on the PlayStation, which has no FPU.

    I think this sentence just made me realize what made the PSX graphics look so "PSX", I'm guessing all the positions/translations and similar stuff were actually not floating point which they typically are (today at least), hence the classic look of triangles/meshes kind of "jumping"? Huh...

    ndepoel 19 hours

    The jumpy polygons are because the PSX's GPU only accepts vertex positions in screen coordinates, meaning that vertices always snap to hard integer values. There is no sub-pixel accuracy. That's not a limitation caused by the PSX's lack of an FPU though. A large part of the render pipeline and standard libraries are built around the use of fixed point math, i.e. using integers to simulate decimal values. The GTE co-processor which handles 3D math makes use of standardized fixed-point types. So the PSX would have been capable of calculating sub-pixel coordinate values, it's just that the communication between CPU and GPU was designed to use only integer screen coordinate values.

    anthk 20 hours

    Integers. You could almost emulate a PSX in a potato (Pentium MMX) with -ffast-math -O3 except for Alpha channel (transparencies) in textures.

    spicyjpeg 19 hours

    It's slightly more complicated than that. The PlayStation's geometry pipeline uses fixed-point coordinates with a decent amount of fractional precision (12 bits), however the GPU lacks support for subpixel rendering and operates entirely in screen space using integer X/Y pixel coordinates; all transformed vertices thus have to be rounded CPU-side before they reach the GPU. The GPU is also a 2D rasterizer only, with no perspective correction nor depth buffering, so transformed Z values are used on the CPU to sort polygons back to front (typically using hardware-assisted linked list bucket sorting) and dropped afterwards [1].

    [1] https://github.com/spicyjpeg/ps1-bare-metal/blob/main/src/08...

    meindnoch 18 hours

    No.

    The reason is that the PlayStation doesn't have perspective-correct texcoord interpolation.

    klodolph 56 minutes

    Well, that’s half of it, and the other half of it is no sub-pixel coordinates.

    unleaded 18 hours

    That's a separate issue from the wobbling.