bmx-ng / physics.mod

Physics simulations
0 stars 0 forks source link

Thanks (struct box2d) #1

Open GWRon opened 2 years ago

GWRon commented 2 years ago

As we still try to play around with "box2D" various impacts I used "wookie's" (@luk22) sample code to try out things.

It does some box2d stuff --- and the interesting part I added was the simple allocation of various TBanks in a global (not more not less)

So in essence I had this in the top area of the test file:

Global bankMode:int = 1
'modes
'0= 0 banks
'1= 120 * (1920 * 1080 * 4)
'2= 1200 * (1920 * 108 * 4)
'3= 12000 * (192 * 108 * 4)

Global banks:TBank[]
Global banksSize:int
Global banksAmount:int = 0
Select bankMode
    case 0
        banksSize = 0
        banksAmount = 0
    case 1
        banksSize = 1920*1080*4
        banksAmount = 120
    case 2
        banksSize = 1920*108*4
        banksAmount = 1200
    case 3
        banksSize = 192*108*4
        banksAmount = 12000
End Select
banks = new TBank[banksAmount]
For Local i% = 0 To banks.length - 1
    banks[i] = CreateBank(banksSize)
Next 

I modified the classic box2d in another module copy to have additional GetPosition2(vec:b2vec var) and the likes to save some "object creations" by just being able to reuse existing vectors.

Another version used @HurryStarfish 's version which skipped the required bvec2-object deletion too (think by having a bit of memory in a struct in which the box2d-lib writes their stuff or so). This truncated down the GC object allocation count quite a lot

The following list contains the maximum FPS the box2d sim had on my computer (ryzen 5 3600x)

'   box2d    box2d"reuse vecs"   box2d Fireball   physics.box2d
'0= 1090fps  1700                2050             2250
'1= 960fps   1580                2250             2310
'2= 720fps   1280                2200             2310
'3= 1600fps  2360                2230             2320

So: physics.box2d is now faster than before. And the test somehow exposes, that the GC has issues with certain object amounts (almost same "total memory" required but somehow the "small blocks but many objects" gives more FPS

So the first two columns expose that there might be "sweet spots" in which the GC does change behaviour or so.

This might be the reason for the issue @luk22 has with his game (https://github.com/bmx-ng/bmx-ng/issues/132)

I made a sample there: https://gist.github.com/GWRon/d478c10fd007d1e60364de05f4cbe295 (the first comment is adjusted for physics.mod/box2d.mod)

GWRon commented 2 years ago

Aside of that: awesome to see it improved - surely handy for the bmx-box2d users around. Good job.