jump.......

i need a way to jump and set grav for the player
ya im a noob

I don’t think if there is a simple answer to your question. HOW you implement jumping depends on what design you have chosen so far, and how much is implemented by the libraries you utilize.

I will try to help anyway. First I present a small working example based on GravityWalker, an (outdated?) utility class provided by Panda3D. Second, I will give some hints on how character movement could be designed.

By the way: after jumping is working, you will have to think about walking up slopes and sliding down. An then the you might want to think about stairs or upward steps. Characters should be able to navigate small steps, but not larger ones.

Simple GravtyWalker Example:

Might be you have to adjust model path to make it work with out-of-the box Panda3D installation, or collect all models/textures in a folder “models”. Models are all from the “roaming ralph” demo.

Keyboard keys to move/turn/strafe, and y to jump. No tracking camera is implemented, so you have to move it yourself (default Panda3D camera controls).

You will notice that collision response is a bit ugly sometimes. The actor “jumps” sideways if touching obstacles, and doesn’t “slide” along the obstacle.

import direct.directbase.DirectStart
from direct.showbase.DirectObject import DirectObject
from direct.showbase.InputStateGlobal import inputState
from direct.actor.Actor import Actor
from direct.task import Task
from direct.controls.GravityWalker import GravityWalker

from pandac.PandaModules import BitMask32
from pandac.PandaModules import CollisionTraverser
from pandac.PandaModules import ActorNode

class World( DirectObject ):

    def __init__( self ):

        base.cTrav = CollisionTraverser( )
        base.cTrav.showCollisions( render )

        mask = BitMask32.bit( 1 )

        env = loader.loadModel( 'models/world' )
        env.reparentTo( render )
        env.setCollideMask( mask )

        self.actorNode = ActorNode( 'Avatar' )

        self.actorNodePath = render.attachNewNode( self.actorNode )
        self.actorNodePath.setPos( -20, 10, 10 )

        self.body = Actor( 'models/ralph' )
        self.body.reparentTo( self.actorNodePath )

        self.walker = GravityWalker( gravity=-9.81, standableGround=0.0 )
        self.walker.setAvatar( self.body )
        self.walker.setWalkSpeed( -10, 30, -6, 90 )
        self.walker.setWallBitMask( mask )
        self.walker.setFloorBitMask( mask )
        self.walker.initializeCollisions( base.cTrav, self.actorNodePath )
        self.walker.enableAvatarControls( )
        self.walker.placeOnFloor( )

        #self.walker.displayDebugInfo( )
        #onScreenDebug.enabled = True

        base.taskMgr.add( self.update, 'update' )

        inputState.watch( 'forward', 'arrow_up', 'arrow_up-up' )
        inputState.watch( 'reverse', 'arrow_down', 'arrow_down-up' )
        inputState.watch( 'turnLeft', 'arrow_left', 'arrow_left-up' )
        inputState.watch( 'turnRight', 'arrow_right', 'arrow_right-up' )
        inputState.watch( 'slideLeft', 'a', 'a-up' )
        inputState.watch( 'slideRight', 'd', 'd-up' )
        inputState.watch( 'jump', 'y', 'y-up' )

    def update( self, task ):
        self.walker.oneTimeCollide( )
        return Task.cont

world = World( )
run( )

Seems rather simple? Well, all the work is done behind the curtains. Not pleased with every aspect? Well, then you have to do it yourself, or adjust the Panda3D code. But now on to design:

Basic movement:
Nothing new probably, but I will start here. Each frame, and for each body, get the bodies position, add a position delta, and set the new bodies position:

pos --> pos + delta

The problem is: what is delta?

Physics Engine, or not:
Physics engines are made to calculate this delta. So you can use a physics engine, or do the computation yourself. Possible physics engines for Panda3D are: Panda3D’s own integrated physics engine, ODE, or maybe soon PhysX.

In case you use a physics engine: There will be two classes of objects:
(1) dynamic objects: movement is controlled by the physics engine, according to the laws of physics.
(2) kinematic objects: movement is controlled by some kind of input. Input can be the user (keyboard/mouse/joystick), or AI. Usually this is done by finding out what the desired delta is, which will be applied to the object and then checked for collisions. If collisions happen, the desired delta is adjusted to a real delta. ALL CHARACTERS (PLAYER OR NPC) ARE KINEMATIC OBJECTS!

In case you don’t want to use a physics engine: all objects are kinematic objects.

Jump - abstract look:
From an abstract point of view jumping can be described like this: give the body an upward impulse (resulting in an initial upward velocity), and then let gravity pull the body down until it touches the ground again. Gravity means that each frame the bodies velocity is modified like this:

v --> v - G * dt

where G is the gravitational constant (9.81 m/s2 for cgs units), and dt the elapsed time since the last frame.

Jump - possible design:

Control would be like this:

  • Player: InputUser --> Controller --> KinematicObject
  • NPC: InputAI --> Controller --> KinematicObject
  • Other (e.g. crate): PhysicsEngine --> DynamicObject
class InputUser:
	# each frame: read keyboard/mouse status and
	# determine a desired velocity and heading,
	# also start actions like jump etc.

class InputAI:
	# each frame: do some AI calculations and
	# determine desired velocity and heading,
	# also start actions like jump etc.

class Controller:
	# each frame: take desired velocity and heading
	# and move the kinematic body. Also handle jumping,
	# sliding, stair stepping etc.
	# After moving check for collisions and adjust desired 
	# movement to real movement.

class KinematicObject:
	# implements "collide & slide" or similar algorithm,
	# and update visual representation once a frame

class DynamicObject:
	# implements movement and collision based on forces and
	# other physical properties like *softness* or friction,
	# and update visual representation once a frame.

Jump - pseudo controller code:
This pseudo code could be methods of a CharacterController class which handle jumping.

def startJump:
    self.isJumping = True
    self.vUp = v0   (e.g. +5.0 m/s)
    # start task processJump
    # set jump animation

def endJump:
    self.isJumping = False
    # remove task processJump
    # set other animation

def processJump( dt ):
    self.vUp -= dt * G   (G=9.81 m/s2 e.g.)
    delta = ( 0, -self.vUp, 0 )
    oldPos = self.np.getPos( )
    newPos = oldPos + delta
    # check for collision with ground
    # if collision: endJump

enn0x

thanks dude. interesting read

Hey I made a character of mine able to walk jump around uneven terrain using the Panda physics engine for gravity and jumping and using my own custom walking and turning. I’ll post it in a while hopefully.

I just applied an upwards force for a split second when the character jumped and the physics engine gravity took care of the rest.

thanks that would really help