Grav2D: Attraction causes LÖVE

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
User avatar
Notsure
Prole
Posts: 7
Joined: Fri Mar 03, 2017 11:35 pm

Grav2D: Attraction causes LÖVE

Post by Notsure »

Hiya everybody! I've got something pretty cool to show you!

This library adds the normal love.physics by allowing bodies to be influenced by another body's mass, called gravity, as well as some small other things. The following is a demonstration of what Grav2D can do in 38 lines of code:
Image
(This example is attached with the library as an example)

Currently, Grav2D has 5 major functions:

Code: Select all

Grav2D.new(body,shape,mass)
>Returns a new Grav2D Object for you to play around with

Code: Select all

[Grav2D Object]:applyGrav()
>Applies the appropriate gravitational forces to the body of the object
>Returns a debug table with the average force, angle (in degrees), and distance, as well as a world body count

Code: Select all

[Grav2D Object]:draw(mode)
>Draws the Shape of the Object in the World so you don't have to.

Code: Select all

Grav2D.toCartesian(r,θ)
--AND--

Code: Select all

Grav2D.toPolar(x,y)
>Converts polar coordinates (distance, angle in radians) to Cartesian (x, y) and vice versa. Good for setting forces and impulses as vectors.

TODO:
  • Make Blacklisting/Whitelisting body arguments to :applyGrav()
  • Optional make :applyGrav only return debugTable
  • Allow bodies to be attracted to arbitrary points in space or axes
  • Give support to non-polygon or circular shapes
  • Give option for polygons to attract object based on which side the object is closest to
  • Anything you guys suggest that sounds neato
Criticism and or suggestions are welcomed needed, this is a W.I.P.
Attachments
Grav2D.love
Library and basic example project for Grav2D
(2.62 KiB) Downloaded 151 times
:crazy: kek :crazy:
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Grav2D: Attraction causes LÖVE

Post by ivan »

Hello there, it's an interesting idea, although I'm not 100% sold on the implementation.
Personally I like pure Lua - it's fine if you want to make an extension to love.physics but then you have to keep it decoupled from other stuff like love.graphics. This is a physics lib right, so IMO there shouldn't be any drawing functionality.
To keep the code clean, I recommend moving all of the rendering outside of the library (and just draw your objects in main.lua).
Creating a new Lua table ("debugReturn") each time you call the "applyGrav" function is not ideal.
Let's say I have 1000 objects that I'm updating them each frame, that would be 1000x60 tables per second.
The math code doesn't look super robust, ie: "if (not(xDist==0 and yDist==0))" could become "if dist > 0".
Some objects in Box2D could have a mass and density of 0 so you'll need an extra check with "Pmass/mass".
"math.sqr" should be "math.sqrt"...
Calling "applyGrav" on each body manually is not great either.
Let's say you have 1000 objects, that would be 1000 calls, where each call iterates all bodies so that's 1000x1000 checks.
If you calculate the effects of gravity in pairs where A=>B and B=>A are solved together, the number of checks would drop significantly:

Code: Select all

function Grav2D.update()
  for i = 1, #nbodies do
    for j = i + 1, #nbodies do
      -- apply gravity bewteen: bodies[i] => bodies[j]
It's a good try, but it needs a lot of work.
Good luck!

PS. Oh ya, and the API is not super clear either, it seems to enforce 1 fixture/shape per body.
I'm not sure why you are going through the trouble of creating new fixtures in your code, when love.physics already does that for you.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Grav2D: Attraction causes LÖVE

Post by raidho36 »

TBH I'd rather have you patched Box2D to have body gravity functionality instead.
User avatar
Notsure
Prole
Posts: 7
Joined: Fri Mar 03, 2017 11:35 pm

Re: Grav2D: Attraction causes LÖVE

Post by Notsure »

raidho36 wrote: Sun Mar 05, 2017 2:34 pm TBH I'd rather have you patched Box2D to have body gravity functionality instead.
I would like to do that but frankly I don't know how.

I've tried adding a function at love.physics.Body:[newFunc] but that's a nil value :ehem:
If there's something like a super table that all worlds, bodies, shapes, and fixtures inhere from, can you give me some code pointing to that? It'd be much apricated
:crazy: kek :crazy:
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Grav2D: Attraction causes LÖVE

Post by raidho36 »

In the wrapper C++ file there are Lua interface functions defined, and on the bottom there is a list of function names and function pointers, which then gets auto-pushed into Lua.
User avatar
Notsure
Prole
Posts: 7
Joined: Fri Mar 03, 2017 11:35 pm

Re: Grav2D: Attraction causes LÖVE

Post by Notsure »

raidho36 wrote: Mon Mar 06, 2017 7:03 pm In the wrapper C++ file there are Lua interface functions defined, and on the bottom there is a list of function names and function pointers, which then gets auto-pushed into Lua.
Can't find it :L
I'm kinda new to this whole thing. Could you guide me on how exactly I extend LOVE2D's native libraries?

And the wrapper, is it here? https://bitbucket.org/rude/love/src/7b5 ... at=default
:crazy: kek :crazy:
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Grav2D: Attraction causes LÖVE

Post by raidho36 »

Wrappers are called just that, *_wrap.cpp.
User avatar
Notsure
Prole
Posts: 7
Joined: Fri Mar 03, 2017 11:35 pm

Re: Grav2D: Attraction causes LÖVE

Post by Notsure »

*everything click*
oooh.
oh.
So what you're saying is I can't extend LOVE's physics library via a lua lib?
:crazy: kek :crazy:
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest