Jumping in Panda3D[Solved]

ok so I guess I’ll focus on this, first of all I need to find a way for the actor to move towards the ground again and stop falling. Do you know how I can accomplish this?

Thanks for your feedback!

What you have right now should accomplish the “moving towards the ground” bit–it’s really the "start moving upwards bit that’s missing from that element of the jump.

As to stopping when you hit the ground, what sort of ground do you have? Is it just a flat plane? Uneven ground, as in Roaming Ralph? Flat platforms at various heights? Something else?

Ah I see, so then how do I do the upwards bit? And to answer your question, it’s just a flat environment not the roaming ralph one.

When the player indicates that the character should jump, just set the character’s jump-speed to some positive value!

Ah, that’s easy, then: you know the height of your “ground”, so the character has hit the ground once they’ve fallen to that height, or less than it.

So right now the jump speed is

 self.jumpSpeed -= 10*dt

how would I set it to a positive value then?
Thank you!

self.jumpSpeed = <some positive value>

Note that you wouldn’t replace the code that you presented there–that’s important. The setting of a positive value is just a once-off assignment, and done only as part of the action of the player pressing the space-bar.

self.accept("space", self.spacePressed)


def jumpingAction(self, task):
    dt = globalClock.getDt()
    self.jumpSpeed -= 10*dt
    self.pandaActor.setZ(self.pandaActor, self.jumpSpeed )
    self.jumpSpeed = 10

def spacePressed(self):
    
    self.taskMgr.add(self.jumpingAction, "jump")

So like this?

No: what will happen there is the following:

  • The player presses space. This starts a task.

Then, every frame, the following will happen:

  • The “jumpSpeed” variable will be reduced.
  • The player-character will be moved somewhat
  • The “jumpSpeed” variable will be set to 10.

This means that, while the variable will be reduced, it will be reduced from 10 every time.

With one caveat: since the task isn’t returning any of the expected values, it will run only once (I think).

I’m a little confused then. How do I fix it?

what are you running your game in? 3d or 2d?

hi,
let me make it simple for you:
when the user presses the space key, you set a jumping speed and create a task for handling that (syubtracting the speed etc). when the user is in gground, you will return task.Done, which indicates that the task should not be called anymore.

Yes I am well aware of the tasks, I just don’t know how to accomplish them. Right now I’m trying to set the jumpspeed to a positive value as said by Thaumaturge.

Thaumaturge could you help me out to set the jumpSpeed to a positive value and overall fix my problems?

Thank you!

Patience, please–I was asleep when you posted this and your last message! ^^;

The sequence of actions that @brightening-eyes gave is accurate, I think; what I suggest, then, is going through them step-by-step, and implementing just that. Perhaps even start this section of your code from scratch, so that the code that you have at the moment doesn’t cause the current state of the implementation to be unclear.

For example, the first step given above is this:

when the user presses the space key, you set a jumping speed …

(Save the rest of the sentence for later; that would be the second step.)

You know how to have something happen when the user presses the space-key. And you know how to set the value held in a variable. So do the latter in the former.

And with that done, move on to the next step. In this case, that’s creating a task–which you’ve already done!

So, move on to the next step–and so on.

Otherwise… perhaps it’s worth considering taking a step back, and picking up a tutorial that might teach the fundamentals that could help you to build a feature like this.

Hi, hope you had a great nap! This is my current code

self.accept("space", self.spacePressed)


def jumpingAction(self, task):
    dt = globalClock.getDt()
    self.jumpSpeed -= 10*dt
    self.pandaActor.setZ(self.pandaActor, self.jumpSpeed )

def spacePressed(self):
    
    self.taskMgr.add(self.jumpingAction, "jump")

Earlier you said to make the actor jump upwards you would need to set the value to a positive number. So I wanted to focus on that, and I don’t think it’s necessary to start over and scrap everything as I have already made progress. So what I’m asking is how to do the upwards bit as when I did

   self.jumpSpeed = 10

it was wrong. I am already trying to focus on one thing bit by bit.

Thank you for your patience!

Heh, not a nap: I was asleep for the night. :stuck_out_tongue:
(Well, not technically the night, as I keep odd hours; but it was my “main sleep of the day”, so to speak.)

As to the jump-speed: the problem isn’t what you’re doing–you are, presumably, successfully setting the value of “self.jumpSpeed” to 10. The problem is where you do it.

Since you’re doing it in a task (and presumably one that you intend to have repeat), that assignment will be done over and over again. But for jumping, we really just want that initial burst of upwards speed to happen once, at the start of the jump. After that, gravity reduces the speed until it’s negative (as you have happening when you subtract from “self.jumpSpeed” elsewhere in the task).

So, simply move that assignment of “self.jumpSpeed” to 10; place it somewhere that will be executed only once per jump.

Also, did you see my edit above about a tutorial?

Well I hope you had a good sleep then😁
Sorry if I am completely misinterpreted your advice but do you mean this, because you said to move it?

self.accept("space", self.spacePressed)
self.jumpSpeed = 10

def jumpingAction(self, task):
    dt = globalClock.getDt()
    self.jumpSpeed -= 10*dt
    self.pandaActor.setZ(self.pandaActor, self.jumpSpeed )
  

def spacePressed(self):
    
    self.taskMgr.add(self.jumpingAction, "jump")

Not a bad sleep, and thank you for the well-wish. :slight_smile:

As to your code: Right idea, wrong location! In that location, I would expect the line to be run only once per run of the game, meaning that your jumping will work only once.

Let me ask: why did you select that location for the line? And is there a location in your code that will be run exactly once per jump?

Well it’s getting late for me, gotta also hit the sack. Before I go here is my best guess as where to put it:

self.accept("space", self.spacePressed)


def jumpingAction(self, task):
    
    dt = globalClock.getDt()
    self.jumpSpeed -= 10*dt
    self.pandaActor.setZ(self.pandaActor, self.jumpSpeed )
  

def spacePressed(self):
    self.jumpSpeed = 10
    self.taskMgr.add(self.jumpingAction, "jump")

Anyways take care and I’ll see ya tommorow! :stuck_out_tongue:

And as it happens, you’re quite correct! :smiley: Well done. :slight_smile:

Sleep well, and I hope that development goes well when you return to it. :slight_smile: