Jumping?( please help, i'm desperate)

Blitz3D Forums/Blitz3D Beginners Area/Jumping?( please help, i'm desperate)

Cubed Inc.(Posted 2010) [#1]
For 4 months, I have desperately tried to add jumping to my game.

Every time I have tried, the jumping is whether horribly executed or just doesn't work.

I just most recently watched a video on a game called Anubis 2, and now I am terrified of adding bad jumping into my game.

This forum is my last fallback, so I hope that I learn something from it.

So can someone please teach me how to add reliable jumping in my game.

I would EXTREMELY appreciate it. So can someone please help?

Please Reply.

Thanks


Matty(Posted 2010) [#2]
Here is a simple 2d example. Fairly simple, play around with the numbers to experiment with it.

Graphics 500,500,0,2

Const gravity#=-0.05,groundheight#=400.0

Type actor

Field x#,y#,vy#,vx#

End Type

player.actor = New actor
player\x = Rand(200,300)
player\y=Rand(0,50)
player\vx=0
player\vy=0


Repeat

	Cls
	Color 255,255,255
	Line 0,groundheight,500,groundheight

	If player\y >= groundheight
		;on ground
		player\vy=0
		player\vx=0
		player\y=groundheight
		If KeyDown(203) Then player\vx = -0.5	;left arrow pressed while on ground, so generate velocity in negative x direction
		If KeyDown(205) Then player\vx = 0.5	;right arrow pressed while on ground so generate velocity in x direction
		If KeyHit(57) Then ;jump
			player\vy = 2.0
		EndIf 
	Else
		;in air
		player\vy=player\vy + gravity
	EndIf
	
	player\x=player\x+player\vx
	player\y=player\y-player\vy ;minus sign used because 2d coordinates are from low to high down the screen whereas 3d is from low to high up the axis.
	
               ;draw the player's avatar
	Color 255,0,255
	Oval player\x-1,player\y-20,2,2,1
	Line player\x,player\y-18,player\x,player\y-10
	Line player\x-3,player\y-16,player\x+3,player\y-16
	Line player\x,player\y-10,player\x-3,player\y
	Line player\x,player\y-10,player\x+3,player\y
	


	Flip

Until KeyDown(1)
Delete Each actor
End



stanrol(Posted 2010) [#3]
g = 9 ? newton


Serpent(Posted 2010) [#4]
The key to simple jumping is acceleration. Matty has already posted an example of this. When the player 'jumps', their Y velocity is set to 2 in his example. Then, in each frame gravity is applied - the Y velocity is reduced according to gravity. In each frame, the Y velocity is added to the Y position, and you have jumping.


Nate the Great(Posted 2010) [#5]
http://www.blitzbasic.com/Community/posts.php?topic=89265

from the tutorial forums


Cubed Inc.(Posted 2010) [#6]
Matty
Your example was good, but since it's in 2d, it's sort of difficult for me to understand it and translate it into 3d.

Is there any 3d example that and can have a look at?

Your example is very good, but I think that I would understand a 3d example much better.

please reply


Who was John Galt?(Posted 2010) [#7]
My advice is to forget 3D and forget your game for now. If anyone gives you what you are asking for, you will just plug the code into your game and gain no understanding.

Play about with Matty's code and try to understand it. Come back here with any questions. Once you have it figured, you will find it easy to extrapolate to 3D.

There is also Mark's Castle demo that comes with B3D if you really need a 3D example.


Charrua(Posted 2010) [#8]
i extracted the jumping part of the castle demo in another post for you, explaining that types has nothing to do with jumping, if you want a 3d example you only have to read your own posts!

http://blitzbasic.com/Community/posts.php?topic=91477#1040858

as john said, play with the code sugested and try to figure out what is happening and then come back with a new or more advanced question, not just "please give me some code to do that", normally i found that there are no code to do that, only ideas that must be translated to our particular case, in advance we have to understand the technique (some one else helped with if posible)

(sorry if my bad english sound weird, hope you understood what i'm trying to say)

Juan


Cubed Inc.(Posted 2010) [#9]
John Galt
Although it did sound like you were judging me, I suppose you are right.

I'm not going to forget about 3d and my game, I'll just try to understand it better.

P.S I'm not a copy and paster coder, I just want to make that clear;)


Who was John Galt?(Posted 2010) [#10]
Not saying forget it forever, just don't try to run before you can walk.


Cubed Inc.(Posted 2010) [#11]
John Galt
I understand.

It's just that I do understand jumping, it's just that whenever I try to add jumping to my game (add, not copy and paste) it never seems to work.

I feel like i've been chasing my own tail for these 4 months. I've had about 4 post asking the same question on how to jump.

All of the posts have taught me of how to do it, but whenever I try to do it, it never works.

I'm sorry if i'm sounding whiny and bitchy (pardon my english) it's just such a important part in my game, that I can't help but to stress it.


Who was John Galt?(Posted 2010) [#12]
You don't sound bitchy; no apology required, however, the fact that you are not coming back and asking targeted questions hints to me that you are not thinking about the problem enough. I mean, did you even read Juan's post in the other thread where he pulled out all the relevant 3D code for you? Have you tried playing with the Castle demo?

If you come back with a targeted question based on the code that has been provided, even if you are concerned it is a 'dumb' question, people will respect it and want to help. If you post another thread asking 'how to jump' in a slightly different way, people will lose interest.

Help us to help you.


Cubed Inc.(Posted 2010) [#13]
John Galt
I completely understand what your talking about.

I've actually been thinking not to long ago that if I did keep on posting the same thread, people would lose interest, and then there would be no hope.

I actually do have an issue with the jumping codes that I add (not copy and paste) in my game.

They just never seem to work correctly.

Should I put up the code?


Matty(Posted 2010) [#14]
Perhaps post your own code and someone may have a look at what is going wrong?


Rob the Great(Posted 2010) [#15]
Give us some code. Over the years, I've become better and better about troubleshooting the bugs in my game (40+ pages, that can be a scary thing), and if I'm unable to help, there is a vast ocean of very experience programmers here who could certainly figure out the problem. If you're on to something with the jumping code, we might be able to point you in the right direction.

If your game requires media, maybe you can upload the whole game somewhere and we can take a look at it. That way, you won't have to write up a demo for the forums which probably won't tell us what's going wrong anyways.


Cubed Inc.(Posted 2010) [#16]
Well here's the game.

The jumping was from charrua, so credit goes to him.



That's the whole game itself.

This code sets up the global variables for y_vel and player_y


This code assigns player_y to the EntityY of the player(CUBE)


This code sets up the jumping.


Whenever you start it up, the player goes flying up into the air, then you have to press space(which is also the jump key) to make him stop and fall and land.

When you land , whenever you jump, the camera gets very choppy and has trouble keeping up and when you land, the camera fidgets in a very ugly way.

Is there any way to fix this?

I apologize if the code is long, but I just want the game to be as professional as it can be.

I appreciate the help from you guys.

So is there any way of fixing this issue?

please reply.


Rob the Great(Posted 2010) [#17]
Wow...Reading someone else's code is worse than driving someone else's car.

I've never used the TForm commands, and I haven't the slighest idea what their purpose is, but I can tell from the way your code is arranged that the camera problems are probably forming in that chunk of code somewhere.

When I see camera problems like this, I normally try to shuffle the order in which I move the entities and cameras around. Sometimes, you could mistakenly be telling the camera to continue to point downward, even after the player has hit the ground.

As far as jumping when the program first starts, it's not doing that on rough version that I cut from your code, so I can't answer that either. I am not for sure on this, but I think there is a problem with the If statements for keyboard checking. I've never seen If statements used like that, but it seems to work for the most part, so if it makes sense to you, keep it that way. Just be sure that Blitz doesn't assume that the spacebar is being pressed by default when the program starts. Also, you may want to set the "y_vel#" = 0.0 starting off. That might fix the problem?

Sorry I couldn't be more help. I'll give it another shot tomorrow and see if I'm missing something. I haven't programmed in a couple of months, and on top of being rusty right now, I'm exhausted from not enough sleep last night. Hopefully, someone can help you out.


Who was John Galt?(Posted 2010) [#18]
First off, the indentation on your code is all wrong. This makes it a nightmare to read. Fix that and post it back. It will also provide you another opportunity to think through your code.

I can't make head nor tail of what you are trying to achieve with all those nested if statements around the keydown commands. Stick some comments in there so we can see what you are intending them to do.

Also, if you prefer us all to work with your code rather than working with, say, Matty's simple code to discuss the problem, you need to provide something that runs. We don't have access to your media.


Cubed Inc.(Posted 2010) [#19]
ok, let me fix my code up.


Cubed Inc.(Posted 2010) [#20]
Well, it was tedious, but I think that now my game should be readable.



I hope that now someone can help me out with my problem.


Who was John Galt?(Posted 2010) [#21]
Okay, you couldn't be bothered to post a version people can run without your media. That would have helped. You didn't bother to comment the piece of code a couple of us have mentioned... the keydown section. Like, why do you have a bunch of different key events doing the same thing? If you want multiple keys performing the same action, it is a lot cleaner to say:

if ((keydown(100)) or (keydown(101))
   'do the action here
endif


The indenting is still wrong in some places, like that keydown area, which suggests you didn't follow the logic through. Why do you have a comment like 'Jump?' The ? suggests a cut and paste job rather than understanding the principle.

Anyway, as it's not runnable and I can't be bothered to re-engineer what you posted to run with my media, I'll give you my best guess to the phantom jump. Try putting "player_y=EntityY(cube)" right after you declare player_y a global.

The camera search me without runnable code.

Rez2?


Cubed Inc.(Posted 2010) [#22]
John Galt
I remove all fastextensions, textures and 3d models. Now anyone can run the game.


The reason why I added so much if statements was becuase I just found it better than way. I'm sorry if that's my opinion.

There is no and I mean absolutly no media in the game.

So now can you please help me with my problem?

please reply.


Who was John Galt?(Posted 2010) [#23]
Not disappointed, but you have to put the effort in to learn. TBH, I don't even have B3D here, but I guarantee you will get more help from the other guys if you remove the media.

I thought you were going to end up like Rez, but you are starting to listen, so fair play to you.

Meanwhile, do you understand what Matty's code is doing? Any questions about that?


Who was John Galt?(Posted 2010) [#24]
Okay, good stuff. What I need now is feedback to show you are reading people's suggested fixes and implementing them.

Does the suggested fix for the ghost jump work or not? What are you seeing now?

Does Rob's idea for sorting the camera work?


Who was John Galt?(Posted 2010) [#25]
The reason why I added so much if statements was becuase I just found it better than way. I'm sorry if that's my opinion.
Better how? Do you want multiple keys to perform the same action?


Cubed Inc.(Posted 2010) [#26]
whoa, so many question:)
Let me look through them first:)


Cubed Inc.(Posted 2010) [#27]
John Galt
Ok, i've got some good news and some bad news.
Good news first.

From the help of you and rob, I was able to fix the problem where the player flies up into the air whenever I start the game.

Now for the bad news.
The bad news is that whenever the player jumps, the camera still fidges and still has trouble keeping up. Also, when ever you land, the camera fidges upwards.

Here's how the game is like now(theres no media, so anyone can run it)


The problem isn't solved, but we are making progress.

please reply


Charrua(Posted 2010) [#28]
hi again

i found so many things:

you declare some constants that never use (even in the long version first posted) like TYPE_WATER, then you define many Collisions and only two are relvant and if you ask me, probably only one.

you create a sky based on a sphere of: 39600 triangles! but never see it, because: is scaled 90.000 times and the camera range is 30.000 and you flip the mesh as if you wish to see it, and you Turn the sky each loop as if you want to see the sky rotating...

but only has a big and hig detailed sphere that no one sees!

also, you have a gravity that pulls your player down all the time. and so on..

first, i think that to have a so much scaled player, scenary, sky, camera range etc is not a good thing.

i do some tests and using small scales it works fine for me. Another thing is that if the camera movement isn't as smooth as you like, you can try to smooth it using something like a funtion called CurveValue , let me find it...

Function CurveValue#(newvalue#,oldvalue#,increments# )

	If increments>1 Then oldvalue#=oldvalue#-(oldvalue#-newvalue#)/increments
	If increments<=1 Then oldvalue=newvalue
	Return oldvalue#

End Function




and, AntiAlias mus be inside the main loop : mus be called each loop to work (see the blitz3d help example!)

hope that help!

Juan


Cubed Inc.(Posted 2010) [#29]
Charrua
You solved all of my other problems, but my fidgety jumping problem still remains. I'm still vey grateful. The game looks much better with a sky.

Can the curvevalue function that you listed above fix the fidgety camera while jumping?

If so, than can you explain it?

Please reply


Charrua(Posted 2010) [#30]
i really dont know it it should solve the problem but you can give it a try

basically you have in each loop code for repositioning the camera and to mantain it pointing to the CUBE/player entity. CurveValue is a function to smooth transition from an old value to a new one in some steps.

you can use it for smooth the x,y,z position of the camera

Juan


Charrua(Posted 2010) [#31]
it's good to know you are seen the sky now.

it seems to me that the Y position of the camera is what is going wrong so, try to smooth the y position with something like that:


Const SmoothFactor = 10
CubeY# = EntityY(cube)

While Not KeyDown(1)

	
	PositionEntity CAMERApivot , EntityX( CUBE ) , CubeY, EntityZ( CUBE )
	CubeY = CurveValue(EntityY(CUBE), CubeY, SmoothFactor)
	


play with the SmoothFactor, 1 is no smooth at all

Juan


Charrua(Posted 2010) [#32]
this is my scaled down version:



Juan


Cubed Inc.(Posted 2010) [#33]
Charrua
I tried your method. Jumpings alot smoother, but the player still fidgets for about a very ugly second when he lands (I inserted you method in the version of the game with media).

I need to find out why the camera fidgets when the player lands

please reply.


Charrua(Posted 2010) [#34]
hi

i can't reproduce your problem, i'm working with a plane and if you test the code i posted, nothing wrong seems to be happen.

i think that you may have some unbalanced values: to much gravity or player speed with some other variables not matched. There are many points in the code with some "scale factors" i think sugested in other posts and the problem you have now is how to melt all these pieces of code.

First you must understand what does each separately and how interacts with each other.

An small y_vel decrease will make the player fly when he reach the top of a hill, as an example, but an strong y_vel decrease will make the player go down fast, both behaves are unnatural!

I don't know how smooth is your terrain and the problem could be there, and so.

please try to match your scales,ratios, values. I think the key of the problem is there.

try to load your terrain in my example, be careful with scale, yStart position of the Cube and Camera and modify the value asigned to y_vel
if SPACE is pressed and the value that decreases y_vel.
The first one say how much impuse it has when jump and the second dictates how much it accelerates down.



Juan


Who was John Galt?(Posted 2010) [#35]
i found so many things:

you declare some constants that never use (even in the long version first posted) like TYPE_WATER, then you define many Collisions and only two are relvant and if you ask me, probably only one.

you create a sky based on a sphere of: 39600 triangles! but never see it, because: is scaled 90.000 times and the camera range is 30.000 and you flip the mesh as if you wish to see it, and you Turn the sky each loop as if you want to see the sky rotating...

but only has a big and hig detailed sphere that no one sees!

also, you have a gravity that pulls your player down all the time. and so on..
This, plus the fact that you make no attempt to enquire why the fixes work, tell me that you are indeed a cut and paster merchant. You will never learn jack that way. GIMME DA CODEZ!!!!!!


Cubed Inc.(Posted 2010) [#36]
John Galt
Funny you reply to me that way, becuase I have actually found out the reason why the camera fidgets. The reason why is becuase the camera is pointed at the player, so landing fidgets. The problem for me is that the camera needs to be pointed at the player or else the camera problem will not improve. I'm thinking of creating a pivot titled "pointpivot" and parenting the pivot to the player. Afterwards, I would point the camera at that pivot.

Since your obviously more experienced with blitz3d (despite not even owning the program anymore), do you think that this would solve my issue?

I am learning and becoming more realized with my code, and your judging is seriously discouraging that.

Despite this, you are teaching me something for the better, so that's good enough for me.

So what's your opinion on my idea, teacher:)


Who was John Galt?(Posted 2010) [#37]
The best way to fight the discouragement is to prove me wrong. The camera thing would be tough to for me to diagnose without B3D.

I will keep my disparaging comments out of the thread now. I hope you get your game working, and more importantly, understand why it works. Good luck!


Cubed Inc.(Posted 2010) [#38]
John Galt
I'm gonna need it.


Warner(Posted 2010) [#39]
First of all, I would break up the world into 3 parts:
1. ceilings
2. walls
3. floors

Then, each part could have it's own collision id:
Collisions player, floor, 2, 3
Collisions player, walls, 2, 3
Collisions player, ceilings, 2, 3


Then, you could maybe make a function that moves the player, and checks if a collision occurs. Although it is not absolutely neccesary.

Anyway, pseudocode for jumping would be this:
if jump > 0 then

   if move(player, up, jumpspeed#) = collided_with_ceiling then 
          jump = 0 
   else 
          jump = jump - 1
          jumpspeed = jumpspeed * 0.98
   end if

else

   if move(player, down, fallspeed#) <> collided_with_floor then

          fallspeed# = fallspeed# * 1.02

   else
  
          fallspeed# = 0
          if keydown(space)
               jump = 128
               jumpspeed# = 5.0
          endif

   end if

end if


An example of this can be found here:
http://www.blitzmax.com/codearcs/codearcs.php?code=2583


Rob the Great(Posted 2010) [#40]
Wow...I leave for a couple of days and the forum exploded! lol.

TAGames, DO NOT create another pivot to point the camera at, unless there is a specific reason why.

Even if the pivot is a couple of units away from the player, it will only create what's called a jump cut when you point to it, and will only make the camera shaking worse.

I'm going to try and tackle the camera issue again with some of the cleaner code and see what I can come up with. It may be another couple of days before I respond again, so in the meantime, try texting some of the jumping variables to the screen (y_vel,EntityY(player), ect). This is a great first step in the debugging process, and helps you visually see what's happening internally in Blitz. Also, if the program is moving too quickly to see exactly what the wrong movements are, try using the "Delay" command to slow down execution. This will allow you to pinpoint where the problem is occurring.


Rob the Great(Posted 2010) [#41]
Charrua's version works flawlessly. Is there something I'm missing?


Warner(Posted 2010) [#42]
Most jumping routines allow you to climb up walls or stick to the ceiling. Splitting up the environment in walls/ceilings/floors could help to prevent that.
When you point a camera towards a player involved in collisions, you should to that between UpdateWorld and RenderWorld. Else, the camera is pointed towards the player, then the player is moved backwards a bit (by UpdateWorld), and then the world is rendered, resulting in the camera pointing in the wrong direction.


Rob the Great(Posted 2010) [#43]
No kidding? I never knew that before, but it makes complete sense. I'm going to try a sample and see what you're talking about.

I never knew that a lot of camera problems were as simple as calling the PointEntity command between UpdateWorld and Renderworld.


Cubed Inc.(Posted 2010) [#44]
wow
For all these days I have been trying to fix this issue when all I had to do was that?