Smooth Jump

Blitz3D Forums/Blitz3D Beginners Area/Smooth Jump

IKG(Posted 2006) [#1]
deleted because of change


Steven Noyce(Posted 2006) [#2]
I use a gravity variable. Every frame, no matter what, I translate my character in the y direction by this variable. When my character is touching the ground, I have this set to about -.4. I have an if statement that says If gravity>-.4 Then gravity=gravity-.1. Then, if the jump key is hit, I say that gravity=.5, or something like that. All these numbers are just off the top of my head, I don't remember what I realy used, but that is the basic idea.

Hope I was of some help!


Jams(Posted 2006) [#3]
Here's a quick code sample off the top of my head:

Const GRAVITY# = 0.005

Local PlayerYVel# = 0 ;Players vertical velocity
Local PlayerYPos# = 0 ;Players vertical position

Repeat ;Begin looping

;Deduct gravity from the players vertical velocity.
PlayerYVel# = PlayerYVel# - GRAVITY#

;If the player is touching the ground, set vertical velocity
;to 0 (since upward force from the ground would equal
;downward force from gravity).
If (PlayerIsOnGround) then PlayerYVel# = 0.0

;If the player jumps, set the vertical velocity to a positive value.
If (PlayerJumps) then PlayerYVel# = 5.0

;Adjust the player vertical position based on the vertical velocity.
PlayerYPos# = PlayerYPos# + PlayerYVel#

Until KeyHit( 1 );End loop


That should give you a basic idea :)


IKG(Posted 2006) [#4]
Thanks Blitznerd. You just gave me an idea!


Pongo(Posted 2006) [#5]
Do a search for "jump" also,... this question seems to be asked once a month, so there will be a lot of answers.

Here is a link to one of them with a simple example

http://www.blitzbasic.com/Community/posts.php?topic=58265#648256


IKG(Posted 2006) [#6]
Well I already searched before posting this. I wanted help with my code, not someone else's.


Pongo(Posted 2006) [#7]
Ok, let's start there then. I see a number of errors that will need to be fixed.

first, start with your jump function.
Function jump()
If KeyHit(57) Then
	jump = True	
	For i=1 To 100
		TranslateEntity camera,0,0.01,0
	Next
	jump = False
EndIf
End Function


When you enter the function, you set the jump flag to true, then at the end you set it to false. There is no reason to do this in the code you provided, since you do not refer to this variable. You do have it in your main loop, but it will always be false because of the above code.

next, you move the camera 100 times in small steps, but there is nothing else going on (no frame updates, so this will appear as one movement when you do update the frame.

A much better way to do this is with a velocity and a gravity variable. Then all you need to do is add gravity to the value each loop through (even when jumping) When you wish to jump, you set the vertical velocity to an amount that will give you the proper jump height, and then let gravity do it's thing. (The gravity will slow the jump, and eventually return the player to the ground in a nice arc.) You will simply need to detect for a ground collision so you do not continue to fall once you hit the ground.


IKG(Posted 2006) [#8]
deleted


octothorpe(Posted 2006) [#9]
You have failed to show how "force" affects the rest of your code, and how ycam is set.

Also, where have your tabs gone?


Pongo(Posted 2006) [#10]
Nope, still not right yet.

without complete code it can be difficult to see what you are doing, but here goes.

first of all, since the jump function is only handling the jump, you should not have to check if updatejump = true. Instead, do that check first, and if it is true, then call the function, rather than calling the function and then checking.

Next, you are checking for if ycam < 7 and applying different forces. What are you doing here? Are you trying to make the jump go up and then down? I assume that is what you are doing, but this is a very bad way to do it. Use velocities, and let gravity forces create the proper arc for you.

Let me try to explain better.
Lets say gravity force = 5.
Lets say Vertical velocity = 0.

Now, when you jump, you set the vertical velocity to 20.

Next, you need to add the velocity to the y position, and then subtract gravity (This will make the vertical velocity now 15)

loop through the last step and you will see that the velocity will eventually equal 0 and start to go into negative numbers. This will mean that you will begin the downward part of the jump.

Look at the example I linked above, it's quite short and should help you.


Jams(Posted 2006) [#11]
Agreed with Pongo, my first post in this thread demonstrates this technique but seems to have been missed...


IKG(Posted 2006) [#12]
Actually, my way worked :P

All I had to do was move
If ycam = 7 Then
force = -0.04
updatejump = False
EndIf

to the main loop.

Now I'm just tweaking some settings so that I don't hit the top too hard before coming back down. Thanks anyway.


Pongo(Posted 2006) [#13]
*sigh* this is my last post in this thread.

USE VELOCITIES!

by using them you won't "hit the top too hard" it will be a nice snappy jump with proper slowdown at the top.


Jams(Posted 2006) [#14]
it's great when people appreciate your help...


IKG(Posted 2006) [#15]
I said "thanks anyway" as in thank you for your time. It's not that I don't appreciate your help, it's that I am happy with my way and it works perfectly fine. I tweaked it much more and now it's perfect.

Don't get mad at me just because I figured out how to do it my way. I had already said I figured it out after Blitznerd gave me an idea.


Damien Sturdy(Posted 2006) [#16]
Velocities will work much much better, and is way easier to implement :)


Jams(Posted 2006) [#17]
it doesn't bother me that you figured out on your own - it bothers me a little that you completely ignored my post...

nevermind..

Also, in future there's no need to edit your posts to delete your old code when you fix it - this thread could have been a good resource for other beginners to learn about jumping.


IKG(Posted 2006) [#18]
I didn't ignore you Jams. I just didn't understand your post. I know that sounds stupid, but I'm a beginner to this all and at the time my idea just seemed easier.

And about me deleting my code, I usually never do that. The reason for that this time, was because of how "wrong" it was. I completely tweaked and modified it after posting it. Last thing I want to do is confuse noobs like me ;)

But once again, I DO appreciate all of your help and I really want to thank you for that. I apologize if I gave the wrong impression.

Edit: Now that I think of it, my idea is similar to yours. I made it so that I slow down the gravity for a split second, after I hit the top and start to come down. After that split second is done, the regular negative gravity is put into place.