GithubHelp home page GithubHelp logo

Comments (3)

viblo avatar viblo commented on August 21, 2024

Good question!
So, when you create a constraint you assign it to two bodies, like j = pymunk.PinJoint(body1, body2). At this point the constraint object will keep a reference to the bodies (this is not exposed in the API). For convenience its also possible to get the constraint(s) out from the body, with body.constraints as you write. If the constraint is not needed anymore and all references to it are deleted, then the reference between the constraint and body will also be cleaned up eventually (behind the scenes its implemented as a weakset, so this cleanup depends on the garbage collector).

>>> import pymunk
>>> b1 = pymunk.Body()
>>> b2 = pymunk.Body()
>>> j = pymunk.PinJoint(b1, b2)
>>> b1.constraints
{<pymunk.constraints.PinJoint object at 0x0000019DAC940F50>}
>>> del j
... some other code that triggers garbage collection
>>> b1.constraints
set()

Note that the space is not involved here. The constraint - body reference can exist outside the space. (but of course, the constraint wont be cleaned up while the constraint and body are in the space since the space will keep a reference to the constraint)

from pymunk.

BenjaminKerbal avatar BenjaminKerbal commented on August 21, 2024

Hello,

Thank you for the detailed explanation. This clarifies how constraints are managed in Pymunk, and why the constraints remain in body.constraints even though they have been removed from the space. I will take your advice and ensure all references to constraints are removed when they are no longer needed.

from pymunk.

viblo avatar viblo commented on August 21, 2024

Great! I made a small improvement to the docs as well.

from pymunk.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.