Olliebrown

Phaser P2 Physics - Apply Force

Recommended Posts

I am attempting to apply force manually to a sprite that has a P2 physics body.  My hope is to achieve the designable jump described by Kyle Pitmann in his GDC Talk below.  In this he derives equations for a jump arc that are controlled by the height, footspeed, and how long the jump should last.  It will give you a gravitational force and an initial velocity to achieve those parameters.

Everything I do to try and set a manual force on the P2 body is being ignored.  Here is what I have:

  • Gravity on the body is disabled via body.data.gravityScale = 0
  • I then try and initiate a jump using my precomputed initial velocity and gravitational constant as follows:

let vx = config.WALK_SPEED
sprite.body.force.y = this._jump_grav_start * vx * vx
sprite.body.velocity.y = this._jump_vel_start * vx
 

Afterwards I dump 'force' to the console and it is always zero no matter what I try to set it to.  Am I just missing something fundamental here about how you apply a force in P2?

Seth B.

)

Share this post


Link to post
Share on other sites

the force property should not return A zero value, but A point object. Are you sure the variables are not zero or undefined: config.WALK_SPEED/vx and this._jump_grav_start ?

Normally gravity should always be applied. You can use player.body.force, within  update() to make sure it is always applied,. The velocity can be set once, via an input event for example and does not require updates in your case. An applied example of the situation:

 

Share this post


Link to post
Share on other sites

Well that certainly confirms that it works!!  Thank you for the concise and well organized example.  I need to see if I can integrate it better for myself.  I think the key idea you are doing that I am not (and should be) is applying the gravity force at all times.  I am curious to figure out why that makes a difference but it certainly seems more sound from both an integration and physics standpoint.  Changing it in the middle of the game like I am might prove problematic!

I'll work on this when I have time and check back with results.  Thanks again!

Seth B.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.