### Author Topic: Need help with Jumping  (Read 3006 times)

0 Members and 1 Guest are viewing this topic.

#### narabuster

• LV1 Newcomer (Next: 20)
• Posts: 18
• Rating: +0/-0
##### Need help with Jumping
« on: September 27, 2014, 11:28:00 am »
I've been recently programming using Axe, and I have made myself a few programs that mostly involve small little picture animations. Not too long ago, I wanted to start my own side-scrolling platformer. I have been using previous topics and videos about making a line border and everything, and I made a simple moving engine. The only problem is jumping. I made what looked like a jump engine, but the problem is that its... static.

Maybe my code from the program source can give some insight:

Code: [Select]
.PLATFORM...X X valueY y value...40-> X5-> YClrHome[FF99BDFFFFBD99FF]-> Pic1Repeat getKey (15)ClrDrawPt-On (X, Y, Pic1)Line (0,62,95,62)Line (0,0,0,62)DispGraphIf getKey (1)Y+1-> YIf Y> 5454-> YEndEndIf getKey (4)Y-1-> Y40-> YIf Y <40Y+1-> YEndEndIf getKey (2)X-1-> XIf X <01-> XEnd EndIf getKey (3)X+1-> XEndIf Y <54Y+1-> YEndEnd *The general layout is based on Scout greyscale tutorial and from an author of an AXE Parser video.

When I compile and run the program, the movement and the simple gravity engine work fine, but my jumping seems to be erratic. I was hoping for a fluid jump and the sprite going back down even if I held down the jump key, but instead, the sprite stays up as long as I hold the jump key and the sprite doesn't show its animation for going up to the bound of 40 that I gave to the Y var.

I've read other topics on the issues of jumping, but they don't seem to help me. An explanation with a fix/optimization code is what I need, because for now, I'm kinda stuck.         Any help would be appreciated.

#### shmibs

• しらす丼
• LV11 Super Veteran (Next: 3000)
• Posts: 2132
• Rating: +281/-3
• try to be ok, ok?
##### Re: Need help with Jumping
« Reply #1 on: September 27, 2014, 03:07:52 pm »
without getting into the code itself, here is a good way to handle jumping:

check if the jump key is pressed. if so, check if your character is immediately on top of a platform and if vertical velocity is not negative (meaning the character is not rising). if this is all true, set the character's velocity to some negative value, depending on how high you want to make him jump.

each frame, acceleration should be a positive constant, causing the velocity to change constantly and the end result to be a clean arc, assuming no things get in the way. while the velocity is negative, check above your character's head, from the topmost pixel's starting position to the position it would be in at the end of the frame. if there is a platform in the way, set the character's vertical position so that that topmost pixel is immediately below the obstruction and set vertical velocity to 0 (or maybe a positive amount, if you want him to bounce off). similarly, when velocity is positive, check beneath the character in the range between the bottommost pixel's starting position and ending position during a frame.

EDIT: if you want to allow jumping higher by holding down the key, set a "timer" variable to some positive value. decrement it by one every frame until it is zero, and, while the value is non-zero and the jump key is held down, maintain a constant vertical velocity rather than adjusting it due to gravity.

EDIT 2: also, if your platforms are tilemap-based (i.e. have constant-ly sized blocks making up platforms), that collision detection gets lots easier, as you can just check if the projected position is inside of the space taken up by a tile.
« Last Edit: September 27, 2014, 03:15:00 pm by shmibs »

#### Hayleia

• Programming Absol
• Coder Of Tomorrow
• LV12 Extreme Poster (Next: 5000)
• Posts: 3367
• Rating: +393/-7
##### Re: Need help with Jumping
« Reply #2 on: September 28, 2014, 01:55:22 pm »
What shmibs said, but you might want to add some inflation to this or you'll see your character skip a lot of pixels (doesn't prevent collision detection but makes things a lot less pretty).
For example, if you are currently drawing/testing/etc at X;Y, draw/test/etc at X/256;Y/256. That basically allows you to have 256 positions between two pixels without having floating point numbers. You can obviously use something else than 256, it's just an operation that's super-easy to do in Axe.
I own: 83+ ; 84+SE ; 76.fr ; CX CAS ; Prizm ; 84+CSE
Sorry if I answer with something that seems unrelated, English is not my primary language and I might not have understood well. Sorry if I make English mistakes too.

#### narabuster

• LV1 Newcomer (Next: 20)
• Posts: 18
• Rating: +0/-0
##### Re: Need help with Jumping
« Reply #3 on: September 28, 2014, 02:36:26 pm »
Thanks guys.

Taking from what you said shmibs, I actually did find a way to make jumping better. I simply took out the code "If Y<55, Y+1->Y", because the code I implemented was better.

Code: [Select]
If getKey(54) and (pxl-Test(X,Y+8))15->JEndIf J>0Y-1->YJ-1->JEndIf (pxl-Test(X,Y+8)=0) and (J=0)Y+1->YEnd
It was similar to the code that I saw earlier in the Axe forums, except that I needed to adjust within my code, which promptly made me remove the "Y<55" thing.

Hayleia, I sort of don't know much about inflation, so I don't really know if I can do the operation about making the positions different than 256 (unless I already did do it, but I didn't notice). I think I do understand what you're saying, however, about my character skipping a lot of pixels. When I made a 8x8 square on the screen, my character couldn't even stay on half of the square without falling through. I hope that that was what you meant. I guess I could just search through the forums again.

One question though: How could I change the code so that my character doesn't have to be on a line in order to jump? One part of my game deals with this (kinda).

#### Hayleia

• Programming Absol
• Coder Of Tomorrow
• LV12 Extreme Poster (Next: 5000)
• Posts: 3367
• Rating: +393/-7
##### Re: Need help with Jumping
« Reply #4 on: September 28, 2014, 03:49:53 pm »
Here's an example of what shmibs and I were talking about (notice that you can scroll down, I am not saying you are an idiot and you didn't see you could, but I personally took me 4 seconds to notice the scroll bar explaining why my 48 line code seemed so short -.-)

Code: [Select]
.AAL5+0->°VY1L5+2->°Y1L5+4->°VY2L5+6->°Y2Fix 51->P0->A0->VY156->Y10*256->VY256*256->Y2While 1 VY1+1->VY1 Y1+VY1->Y1 If Y1>=56  56->Y1  If getKey(4)   ~10->VY1  Else   0->VY1  End End Rect(10,Y1,8,) VY2+3->VY2 Y2+VY2->Y2 If Y2/256>=56  56*256->Y2  If getKey(4)   ~256->VY2  Else   0->VY2  End End Rect(0,Y2/256,8,) DispGraphClrDraw min(20,getKey(54)?A+1)->A-1??100,0 Pause  xor P->P "NoSlowmo"->°NoSlowmo Text(0,,(P-1?2)+°NoSlowmo)EndIf getKey(15)ClrHome(obviously unoptimized for reading purposes)

Only look at the VY and Y variables, the rest is to get a coherent program. When you launch that, you notice that both squares have a "realistic" jump. But one of both moves one pixel at a time (the one with some /256 in its code) while the other one sometimes move 10 pixels at a time. And you can easily imagine the one moving one pixel at a time making a lower jump but for the other one, if you play around with constants you won't manage to get a lower jump except with a ridiculous teleporting jump.

(note that you can press "2nd" to toggle between "normal speed" and "slow motion").
I own: 83+ ; 84+SE ; 76.fr ; CX CAS ; Prizm ; 84+CSE
Sorry if I answer with something that seems unrelated, English is not my primary language and I might not have understood well. Sorry if I make English mistakes too.

#### narabuster

• LV1 Newcomer (Next: 20)
• Posts: 18
• Rating: +0/-0
##### Re: Need help with Jumping
« Reply #5 on: September 29, 2014, 08:58:38 pm »
Unfortunately,  the program won't run on my calculator (TI-83+).  ???There were some minor errors on there when I compiled the code with the parser, but I fixed them. I assume that just had to do with the un-optimized code you noted. Either way, I always have to reset RAM whenever I run the compiled program, which pretty much freezes the calc instantly. Did I have to involve it in my original code?

Anyways, I did sort of get your reasoning with the 10 vs 256 pixel realistic jumping. Without running the program, it already seems to make sense. The VY and Y vars are a bit complex to understand. Though to get the full idea, I guess I have to run the program, which, well...

I'm sorry if I'm not picking up things quickly,  it's just that I'm not that experienced in Axe as I should be. I know what each of the commands in my jumping program do, it's just that learning new commands or routines can be frustrating for me.

Thanks for the help you're you (and shmibs) are giving me. Like I said, I pick up concepts a bit slowly sometimes.

#### Hayleia

• Programming Absol
• Coder Of Tomorrow
• LV12 Extreme Poster (Next: 5000)
• Posts: 3367
• Rating: +393/-7
##### Re: Need help with Jumping
« Reply #6 on: September 30, 2014, 12:08:06 am »
There's no error in that code (well, maybe there is but nothing that prevents it from running), it's just that it comes from TokenIDE so maybe there are some things that are written weirdly in ASCII but I tried it and it works. I attach it to my post here so you don't have to type it yourself (I don't know why I didn't do that earlier).

So yeah, run that and notice how the "10" square skips pixels while the "256" square doesn't. The Y vars are just the Y coordinates of both squares while the VY vars are just their Y-speed.
I own: 83+ ; 84+SE ; 76.fr ; CX CAS ; Prizm ; 84+CSE
Sorry if I answer with something that seems unrelated, English is not my primary language and I might not have understood well. Sorry if I make English mistakes too.

#### narabuster

• LV1 Newcomer (Next: 20)
• Posts: 18
• Rating: +0/-0
##### Re: Need help with Jumping
« Reply #7 on: September 30, 2014, 03:39:26 pm »
The program works now. I just didn't know that you had to space the section of your While command.
I also learned more optimization tricks, like DispGraphClrDraw (even though you said the code was unoptimized; I think I know what you mean by that)

Now I really understand the difference. I guess this would be of some use to my game, because it does look more smooth than the 10 pixel jump (I'm talking about the 256 pixel inflation).

Thanks for the help. Now one last thing before I consider the topic solved: How do you make your character jump if there is no "platform" (Line) beneath him? Taking from my code a couple replies back, I don't know if I should just change the entire routine or not.

#### Hayleia

• Programming Absol
• Coder Of Tomorrow
• LV12 Extreme Poster (Next: 5000)
• Posts: 3367
• Rating: +393/-7
##### Re: Need help with Jumping
« Reply #8 on: September 30, 2014, 03:51:02 pm »
The program works now. I just didn't know that you had to space the section of your While command.
I don't see what you mean by that.

Now I really understand the difference. I guess this would be of some use to my game, because it does look more smooth than the 10 pixel jump (I'm talking about the 256 pixel inflation).
Just note that here, inflation by 256 makes the jump a lot slower but by changing some constants you can get a pretty fast jump "even" with inflation by 256.

Thanks for the help. Now one last thing before I consider the topic solved: How do you make your character jump if there is no "platform" (Line) beneath him? Taking from my code a couple replies back, I don't know if I should just change the entire routine or not.
I guess you'd just change the "If getKey(54) and (pxl-Test(X,Y+8))" into If getKey(54)" ?
I own: 83+ ; 84+SE ; 76.fr ; CX CAS ; Prizm ; 84+CSE
Sorry if I answer with something that seems unrelated, English is not my primary language and I might not have understood well. Sorry if I make English mistakes too.

#### narabuster

• LV1 Newcomer (Next: 20)
• Posts: 18
• Rating: +0/-0
##### Re: Need help with Jumping
« Reply #9 on: September 30, 2014, 04:59:16 pm »
Quote
...inflation by 256 makes the jump a lot slower but by changing some constants you can get a pretty fast jump "even" with inflation by 256.
Cool... I'll have to look more into that later. In the program, the jump was rather slow, but if there is a way to make it go faster, its definitely something I'll look into. Thanks for showing me that.

Taking out the pxlText(X,Y+8) won't help. My character just falls through the screen if I place him on no platform. If I do place him on a platform, however, my character jumps infinitely as long as I hold the "2nd" button. I tried altering more of the code that had to do with jumping, but no luck. I think I'm getting somewhere though, so it shouldn't take too long before I can find a solution. But if it involves a completely new command, then I'll be stuck again.

Is it possible to render a line invisible but still in the game? If so, I could just make an invisible line, and make the code treat it as a platform that my character can jump on, but in-game the player won't see it.