Disable/Enable gravity for individual objects? [solved]

0 favourites
  • 4 posts
From the Asset Store
Gravity Square is a game where I aim to reach the square at the polka dot door :)
  • Is there a way to disable or enable gravity for individual objects?

    [Solved]

    Set global gravity to 0.

    Use downward force on individual objects to replicate gravity.

    The amount of force should be ( original gravity value * 0.02 ) * the object's mass.

    (The "0.02" just converts C2-pixel-units to Box2D-units.)

    When applying the force, specify image point "0", to use the center-of-mass.

    (When applying forces, specifying image point "0" will apply to the center-of-mass, and image point "-1" will apply to the origin point set in the image editor.)

    e.g. If your original global gravity was "10", then to recreate it with force on an object, you apply a force of 0.2 * the object's mass.

    ---- (original text continued) ----

    Currently two iffy approximations come to mind, but they both have weird issues that separate them from being actual gravity.

    Downward force - as gravity?

    Globally disable gravity,

    Then, for objects that should experience gravity, apply a downward force proportional to object mass.

    This is kind of close to gravity, except that in the physics behavior I can only find actions that apply a force vector to a single point on the object. If this point is not the center of mass (or does not lie on a line through the center of mass), then the object will experience torque.

    I figured there would be a way to retrieve the center of mass coords from the physics behavior, and there is, but I can't find an action in the physics behavior to apply a force on an object that uses XY cords as parameters. The only point parameter is an image point selection.

    For simple symmetrical objects where the center of mass is always dead center, you can just put an image point there and use that, but it's not a generally applicable solution. By contrast the built-in global gravity will work correctly regardless of the specific shape of the collision polygons of the affected objects. I would like to use something that general if it exists.

    Direct velocity manipulation?

    The other option would be to directly add to the object's velocity, which has it's own issues. Framerate independence being the main one that comes to mind. Even with the incorporation of "dt" with an integration step I'd be concerned about mixing both force and velocity level alterations to the object's state, as well as mixing by-hand framerate compensation and integration (as in mathematical integral) with whatever the Box2D asm.js physics engine is doing internally to handle framerate independence and integration. I think this might also bypass friction and linear damping.

    Any thoughts or suggestions are welcome. :)

    (Minor update)

    Image point "0" & "-1" for joints

    In the Physics behavior manual page, in the "Physics actions: Joints" section, there is a mention that, for "Create distance joint" and "Create revolute joint" actions, image point "0" specifies the center of gravity instead of the origin image point, and "-1" specifies the origin image point. I don't know if this is also the case for applying forces, as nothing to that effect is mentioned for force actions. I'll test it out and report back.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Disabling global gravity and applying downward forces is the way to go. Write some code to re-balance objects that rotate or receive other torque.

    If Sprite1 is NOT between -5 and 5 degrees --> set Sprite1.Angle to 0

    Also add values to the angular dampening and density of the object that may rotate, the higher the values, the less rotation you will see. Hope this helps.

  • With full access to the physics library you can disable gravity for certain objects by applying a force to cancel the acceleration of gravity. However it must be applied before the gravity is applied. In the chipmunk behavior you can do this in the "pre step" trigger.

    For the physics behavior disabling gravity and manually is the way to go. I haven't tried the apply force action for a bit but if it's not possible to not apply a force to the com then you could instead calculate the torque and cancel it out. The formula for that is easily found with Google and the only snag is converting the distances and angles to the units used by the plugin.

    Edit:

    Here's info on converting units for the physics behavior:

  • Good news everyone!

    [Solved]

    When applying forces, specifying image point "0" will apply to the center-of-mass, and image point "-1" will apply to the origin point set in the image editor.

    I'm not sure if there's a good way to get in touch with Ashley, but it would be handy to have this added to the "Physics behavior" manual page for the "Physics actions: Forces" section, since it's a pretty nice feature.

    :)

    Thanks for the replies makotto and R0J0hound.

    I appreciate the suggestion makotto, and it turns out that due to the special image point addressing system for joints and forces, it should work without even having to worry about imparting problem torque.

    Likewise, thanks for the article link R0J0hound, as it will help with answering another question I was going to bring up relating to the conversion between gravity units and force units, which appear to be different. For my project, the acceleration caused by "gravity = 10" seems to be equivalent to the acceleration of "force = 0.2 * objectMass". That seems to agree with the "0.02" scaling factor you gave in the linked post.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)