-
|
|
|
Okay, I know there's a lot of topics on this but I haven't seen a definitive answer.
What I want to do, is, of course, to jump in a 2D Platformer.
| if (keyState.IsKeyDown(Keys.Space)) | | { | | if (isJumping == false) | | { | | velocity.Y -= 50; | | isJumping = true; | | } | | } | | if (isJumping == false) | | { | | velocity.Y = -20; | | isJumping = true; | | { | | | | if (velocity.Y < 100) | | { | | velocity.Y = velocity.Y + acc; | | personPosition.Y = personPosition.Y - velocity.Y; | | } | | } | | } | | | This is part of my code. It works pretty well. But the biggest flaw in most of the platformer codes I see, is that when you press space, it IMMEDIATELY jumps up to some velocity, and then gradually comes down. What I want it to do is gradually come up AND down. How would I approach this? Thank you for all help!
|
|
-
|
|
Re: Jumping in Platformer
|
Uhh, i guess you could just change gravity, and the initial vertical velocity. If you want it to come up slower, lower the initial vertical velocity. But to make it reach the same height your gonna also have to lower the strength of gravity.
Or you could like set a constant for how high you want to jump and the strength of gravity, then use the equation for max height to determine the initial vertical velocity
M = (v*v)/(2G);
Then change that around to solve for the vertical velocity
const double GRAVITY = 9.8d; const double USER_JUMP_HEIGHT = 100; double JumpStrength=Math.sqrt(2*GRAVITY*USER_JUMP_HEIGHT); velocity.Y=-jumpStrength;
With that you could just tweak the values of gravity, assuming you already have a set height you want your character to jump
Does that help any?
Your code looks odd for some reason. Mostly where you apply the velocity to the persons position. I would think you would add the velocity (or at least thats what ive always done and seen by others), since when you jump you set it to negative,and to fall you add gravity.....But idk, if you say it works fine in that aspect, whatever.
Check out my blog for updates on my Game Dev: http://gmaker-gd.blogspot.com/
|
|
-
|
|
Re: Jumping in Platformer
|
Sorta. After I roughly added in the code, my player doesn't jump at all. If you'd be able to run up a quick example, that'd be great.
|
|
-
|
|
Re: Jumping in Platformer
|
Uhhh, well did you try add the velocity to the players position instead of subtracting. Try that first, if you havent already.
Besides that, i dont get the point in the second if statement where you test if isjumping is false, or rather i dont get what it was supposed to do in your opinion. | if (keyState.IsKeyDown(Keys.Space)) | { | if (isJumping == false) | { | velocity.Y = -50; | isJumping = true; | } | } | if (isJumping) velocity.Y = Math.Min(velocity.Y + acc,100); | | personPosition.Y = personPosition.Y + velocity.Y; | IDK, try that out
Check out my blog for updates on my Game Dev: http://gmaker-gd.blogspot.com/
|
|
-
|
|
Re: Jumping in Platformer
|
JayMan:Uhhh, well did you try add the velocity to the players position instead of subtracting. Try that first, if you havent already.
Besides that, i dont get the point in the second if statement where you test if isjumping is false, or rather i dont get what it was supposed to do in your opinion. | if (keyState.IsKeyDown(Keys.Space)) | { | if (isJumping == false) | { | velocity.Y = -50; | isJumping = true; | } | } | if (isJumping) velocity.Y = Math.Min(velocity.Y + acc,100); | | personPosition.Y = personPosition.Y + velocity.Y; | IDK, try that out
It didn't work, but thanks anyways. I'll try to find some examples on it.
|
|
-
|
|
Re: Jumping in Platformer
|
no luck??? well make sure your variables to right, and your collision detection if you have any, make sure it doesnt stop the users velocity unless they are moving downward.
Thats all i can say by just looking at that.
Check out my blog for updates on my Game Dev: http://gmaker-gd.blogspot.com/
|
|
-
|
|
Re: Jumping in Platformer
|
I know this is a lot to ask, but if you would make a sample for me and upload it, I would be very grateful.
|
|
-
|
|
Re: Jumping in Platformer
|
Sorry for the double post, but the main problem I am seeing is that sometimes when I jump, I land exactly where I need to be: other times, I land just a few pixel below, etc. Note that this is on the exact same platform (aka not jumping here to there).
|
|
-
|
|
Re: Jumping in Platformer
|
That seems like a common problem i have seen in platformers.
assuming you do collision detection on probably a certain point relative to the player
Uhh, basically since movement is kinda like rapid teleportation, if the ground is like 5 pixels down, and your moving 10 pixels downward, you'll end up five pixels in the ground.
Thus, taking the simple route, you can use a while statement, to move you up until your like only 1 pixel in the ground, or such
Check out my blog for updates on my Game Dev: http://gmaker-gd.blogspot.com/
|
|
-
|
|
Re: Jumping in Platformer
|
Sorry for being a noob, first of all :D
For the while statement, do I check for collisions IN the while statement? Or do while (touchingGround) {move up until 1 px above ground}?
|
|
-
|
|
Re: Jumping in Platformer
|
you could do either, you would ideally want to move up, until moving up anymore would put you above the ground
Check out my blog for updates on my Game Dev: http://gmaker-gd.blogspot.com/
|
|
-
|
|
Re: Jumping in Platformer
|
It seems to work, but the only problem is that you can visually tell it is being bumped up. I wonder if there is any thing that could calculate the changes before hand so you can't see that bump up to the platform?
|
|
-
|
|
Re: Jumping in Platformer
|
do it before you actually apply the velocity something like float nextY= position.y+velocity.y;
if(nextYIsTouchingGround){
while(nextYIsTouchingGround) { nextY--; }
}
position.y=nextY;
Check out my blog for updates on my Game Dev: http://gmaker-gd.blogspot.com/
|
|
-
|
|
Re: Jumping in Platformer
|
JayMan:do it before you actually apply the velocity something like float nextY= position.y+velocity.y;
if(nextYIsTouchingGround){
while(nextYIsTouchingGround) { nextY--; }
}
position.y=nextY;
Aww man, I forgot to put the while in the if loop. This actually makes sense xD
Thanks for all the help and all the time you wasted helping me. I'll mark this as the answer but there's still some good stuff up there.
EDIT: One quick question. Since the nextY-- is in the while loop, it will always slowly come back up, not just land correctly and stay there. Is there anyway around this? Anyways, here's my current source: http://willhostforfood.com/files4/1/0/6/1069268/WindowsGame1.zip
|
|
-
-
- (173)
-
premium membership
-
Posts
61
|
Re: Jumping in Platformer
|
MlkTea, you do know there's a perfectly functional example of jumping in a platformer in the.. Platformer Game Starter Kit, right? :)
|
|
-
|
|
Re: Jumping in Platformer
|
SiW:MlkTea, you do know there's a perfectly functional example of jumping in a platformer in the.. Platformer Game Starter Kit, right? :)
Yep, but I was looking for more of a simple.... solution :). Plus I don't really get the physics behind the Platformer one.
|
|
|