Jumping Using Gravity and Thrust?

Hello I was just thinking and I wondered if you could create a jumping character using gravity and thrust. You would have it so that when you press space it would create a thrust and then the gravity would push you back down. And when you collide with things you want to be able to stand on you would set the gravity to zero and when you stop colliding with the object it would set the gravity to -9.81 again. So what I want to know is if this would be a: possible and b: logical.


Yes, of course it’s possible!

Some logic I would use is that if you’re not colliding with the ground, then you are constantly being pulled downward. But if you are jumping, you are not pulled down until rising ends. Most of the logic for jumping can be found in my last post ([Solved] Jumping Actor). The same could be used for falling.

If you’re in need of more desperate measures of logic, I’d be glad to help you carry on. Just make sure you try and not rush through it all.

Indeed, what you describe is pretty close to what I believe that I usually do:

I generally give my objects a “velocity” variable and, on each update, move them by velocity*dt. Given this, a jump is simply an initial thrust that alters the velocity to have a significant upward component, along with a generally-applied downward force taking the place of gravity.

This gravitic force could either be applied every frame (and relying on collision to prevent the object from moving below the ground) or applied, as you suggest, only when the object is considered to no longer be on the ground - such as at the start of the jump.

In semi-pseudocode, presuming that the y-axis is “up”:
(Note that the exact numbers used, such as the “2.0” in the jump method, may vary according to your purposes, and indeed may be better parameterised rather than hard-coded.)

class myClass:
    def __init__(self):
        self.velocity = Vec3(0, 0, 0)
        self.falling = False
        # Further setup here

    # Note the uses of "dt" here; I presume that "dt"
    # is passed in from an external task 
    # or interval, although one could also
    # set up the object to have its own task and get
    # the "dt" from that.
    def update(self, dt):
        self.nodePath.setPos(self.nodePath.getPos() + self.velocity * dt)
        if self.falling:
            self.velocity += Vec3(0, -9.8 * dt, 0)

    # This might be called by the method that you
    # call when the jump-key is pressed.  You might also
    # be better served by having a general "push" method
    # that this calls.
    def jump(self):
        self.velocity += Vec3(0, 2.0, 0)
        self.falling = True

    # This may not be an actual method in your case;
    # it essentially just expresses "do this when your
    # object hits the ground."
    def hitTheGround(self):
        self.falling = False

cheers guys!, thanks :smiley: