Note that there are some explanatory texts on larger screens.

plurals
  1. PObox2d: How to prevent a character from being able to jump in midair?
    primarykey
    data
    text
    <p>So I've been playing around with box2d on iOS (thought this question isn't really limited to iOS), and I've got a small demo going where a player character jumps when the user taps the screen via <code>ApplyLinearImpulse</code>.</p> <p>For the most part, it works. Except if I tap while the player is in midair, <code>ApplyLinearImpulse</code> is called again, and the player "jumps" again, but in midair. Now, it'd be pretty neat to do that in real life, but you can't, and I'd prefer my player not be able to do that either.</p> <p>So I've been trying to come up with a decent way to prevent jumping while a player is already jumping, and I'm not sure where to go from here - my best thought is to try something like this:</p> <ul> <li>Attach a body that is just a sensor to the bottom of my player with a fixed joint.</li> <li>When the player jumps, disable jumping until the aforementioned sensor detects a collision (i.e., when the player "lands" on something, which may or may not be the ground).</li> <li>Once a collision has been detected, re-enable jumping.</li> </ul> <p>I'm not sure how much I like this idea, for one, I'm not sure if attaching a sensor to the bottom of my player is the "smartest" way of detecting collisions on the bottom of my player sprite. I haven't given this a try yet, I wanted to instead get SO's input on it and see if anyone could provide a better alternative. Any ideas?</p> <p><strong>Edit:</strong> mafutrct had good a suggestion: try some sort of downward hit test, using a ray straight downward from my player's body, and see if that ray intersects another object within a short distance (i.e., right below my player) - is that type of ray-casting hit test possible with box2d?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload