PhysicsLab+

I hope Codea beginners and beginners to 2D physics alike find this helpful.

It’s called PhysicsLab+.

It basically turns the original PhysicsLab into a class you can use in your own projects.

Almost everything demonstrated in the original has been made into an easy-to-use function. For example, the very first test in PhysicsLab creates random polygons anywhere you tap. PhysicsLab+ simply wraps that code in a makeRandomPolygon(…) function that you can easily access from any other project.

And PhysicsLab+ goes a few steps farther too:

  • There are two all-new tests
  • Two of the existing tests have been almost completely revamped.
  • There’s also a bonus test that @John had intended to include in the original project, but had to leave out due to time constraints.
  • Every test now includes short explanatory text in the overlay panel, so you know what you’re looking at.
  • Walls have been added to tests that use a ground, so it’s much harder to accidentally lose objects by dropping them off the edges.
  • The functions for creating joints have been given names that clearly describe what they do, so you no longer have to search the web for the definition of PRISMATIC.
  • Each of the tests is now a self-contained class that can be run on its own from any other project.
  • Potentially its most helpful contribution, PhysicsLab+ includes a category manager that simplifies categories by a jillion percent. Not only does it completely pave over many annoying pitfalls that can trip you up, with PhysicsLab+ you never even have to learn what a ‘map’ is. You just put objects into named categories, using the putIn(…) command. Once you’ve done that, you simply tell some objects to detect them using the detect(…) command, and other objects to ignore them using the ignore(…) command. That’s basically it!
  • Lastly, most of the abilities of PhysicsLab+ are modularized into stand-alone classes, so you can use those features on their own even if you don’t need to use the whole thing. The joint maker, the shape drawer, the category manager, etc—all can be used as part of the PhysicsLab class or on an as-needed basis.

I hope this is a help to some people at the beginning of the 2D physics learning curve! With luck it will turbo-boost the development of your own projects.

@UberGoober Very nice! Great to see this in the wild now :smile:

I love this! Super useful for if I ever make a 2D game. Thank you!

@UberGoober - very neat demos, good reference on physics. Thanks for putting this together.

This is very, very good… and should replace the existing Physics Lab. The added documentation/notes are particularly useful.

Okay, I take it back, because the code is more complex and generalized (not badly written, just more complex), this can’t necessarily replace the existing Physics Lab. It’s better to run, and probably (I haven’t looked yet) has more details to glean, but the simpler code in the basic lab is probably better for a newbie to start with.

But I do think it should be added to the Example projects.

@blacatena do you think it could be refactored to be useful the way you want it to? I tried to rewrite the examples so that it would be more obvious how they worked, were there ones in particular that you found confusing?

@blacatena I updated the first two tests to be much better commented and more simply organized—please take a look and let me know if that helps, because if it does I’ll do the same with the other tests too.

While the original examples are simpler in the sense that the code is shorter, when I was a newbie myself I found them often quite confusing; that very brevity made it hard for me to understand how it all worked.

My intention was for my examples to be simpler in the sense that they take smaller steps, which does make them longer, but also hopefully makes them more easy to follow.

Sorry, haven’t been back for a bit. I’ll take a look; my memory of this is that the code was much better structured and generalized, which is great, but for a beginner, it means that more things are happening outside of the lines of code you are looking at, and so it takes some jumping around and holding things in your head (or thinking “I don’t know what’s going on, what am I missing?” because they don’t even realize things are going on elsewhere).

@UberGoober I love this. This is how Physics Lab should be done, but I agree with @blacatena that more abstractions can make things tough for beginners (who are even struggling with basic language concepts, not just the API)

I would love to include this as an example project if you are open to it? Perhaps we can find a way to make Codea reference a git repository where contributors can push changes and have the app automatically fetch the latest examples in a custom example section. I’ll think about this some more

@Simeon I’d love to have it included! @John mentioned it as a possibility to me too.

Thank you! We can include it statically, but it might be good to move all the examples to git and have Codea update them on load. I’ll look into it more and let you know what we work out

@Simeon @blacatena I’ve now updated the examples 1-5 (up through categories):

  • all now extremely thoroughly commented
  • the unit pattern has been removed from them, so all sizes are now expressed directly as percentages of width or height
  • outside functions almost completely removed, so the entire logic of the demonstration is visible right in the setup method

Please check out the updated project (it’s attached to the OP) and let me know if these changes have been effective.

@UberGoober I notice that on iphone the play/pause buttons overlap the area you want to interact with. I wonder if we should add an extra layout guide to the layout module to help you avoid the buttons in a consistent manner

@Simeon in which test/s?

@UberGoober pretty much all of them in portrait mode, anything that lands at the bottom of the screen as the viewer buttons take up most of the bottom region

That’s more our failing though for not providing an API to avoid drawing in the button area

@Simeon I’ve updated the project to have a thicker floor that keeps everything clear of the buttons on my iPhone 8.

Can you confirm it is sized correctly for the device you were testing on?