Jay_PC
Senior Newbie 
|
 |
«
Posted
2010-11-19 13:11:09 » |
|
Im working on a Platforming shooter and for the Life of me right now I cant figure out how to make the player jump! granted Im working in acceleration as well so it complicates things... My code is somthing like : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| byte gravity = 1; byte jumpspeed = 10; byte verticalAcceleration;
public void jump(){ int y = player.getY;
player.setY(y-verticalAcceleration); if(verticalAcceleration <= 0){ isFalling = true; isJumping = false; }
verticalAcceleration = jumpSpeed - gravity;
} |
I know that dosent quite work but if I had if figured out I wouldnet be asking for help!
|
|
|
|
|
ryanm
« League of Dukes » Senior Member    Projects: 1
Used to be bleb
|
 |
«
Reply #1 - Posted
2010-11-19 13:47:05 » |
|
There seems to some confusion between speed and acceleration in your code. - Location - where your character is
- Speed - rate of change of location
- Acceleration - rate of change of speed
try something like this: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| float location = 0; float speed = 0;
float accel = -9.8f; public void advance( float deltaSeconds ) { speed += accel * deltaSeconds; location += speed * deltaSeconds;
if( location < 0 ) { location = 0; speed = 0; } }
public void jump() { if( location == 0 ) { speed = 10; } else { } } |
|
|
|
|
|
dishmoth
|
 |
«
Reply #2 - Posted
2010-11-19 13:49:25 » |
|
This may be closer to what you want. If nothing seems to happen after calling startJump(), try increasing the jumpspeed value. And if it's still not working, try printing out the player's y-position each frame (or step through the code in a debugger). Simon Edit: Posted before seeing Ryan's reply. His looks better than mine.  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| final int gravity = 1; final int jumpspeed = 10; int verticalSpeed = 0;
public void startJump() { verticalSpeed = jumpspeed; isJumping = true; isFalling = false; }
public void jump() { int y = player.getY; player.setY(y-verticalSpeed); if(verticalSpeed <= 0){ isFalling = true; isJumping = false; }
verticalSpeed -= gravity; } |
|
|
|
|
Games published by our own members! Check 'em out!
|
|
Jay_PC
Senior Newbie 
|
 |
«
Reply #3 - Posted
2010-11-20 12:33:48 » |
|
Im having trouble understanding your code... I feel like the Falling action and the Jumping are combined. I also have a state based collision system touching GREEN isGrounded = true touching blue isHitCeiling = true ######### ##### ##### ######so the player falls if isGrounded is false as soon as it turns true the player stop falling, and if isHitCeiling becomes true the player will immediately start falling. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| float jumpSrartOne = 0; float jumpStartTwo = 0;
boolean isGrounded; boolean isJumpOne; boolean isJumpTwo; boolean isFloating; boolean isHitCeiling; boolean jumpOneOut; boolean jupTwoOut;
final float GRAVITY = 9.8; final float SINGLE_JUMP_POWER = 10; final float DOUBLE_JUMP_POWER = 5;
public void update(){ if(player.getY >= jumpStartOne+maxSingleJump){ jumpOneOut = true; }
if(player.getY >= jumpStartTwo+maxDoubleJump){ jumpTwoOut = true; }
if(isGrounded){ }else if(isJumpOne && !jumpOneOut){ singleJump(); }else if(isJumpTwo && !jumpTwoOut){ doubleJump(); }else if(isFloating){ }else{ } }
public void jump(){ if(isGrounded){ isJumpOne = true; }else if(isJumpOne){ isJumpTwo = true }else if(isJumpTwo){ isFloating = true; }else if(isFloating){ isFloating = false; } }
public void groundPlayer(){ isJumpOne = false; isJumptwo = false; isFloating = false; isGrounded = true; }
public void singleJump(){ }
public void doubleJump(){ }
public void float(){ }
public void fall(){ } |
also fun fact my game takes a heavy influence from Vectorman for the sega genesis and My art style looks closer to the Scott Pilgrim game for the 360 Arcade. if you want to know what my inspiration was, and want to look em up.
|
|
|
|
|
dishmoth
|
 |
«
Reply #4 - Posted
2010-11-21 11:21:51 » |
|
Im having trouble understanding your code... I feel like the Falling action and the Jumping are combined.
When you reach the top of a jump, and you start moving down instead of up, then the behaviour is identical to falling. At least that's how it works in the real world. How it works in your game is entirely up to you. Beyond that, it's not really clear to me what you're stuck on (or if you're stuck at all). It's probably stating the obvious, but you should get the code for a basic jump working before you start adding code for double-jumps, floating, hitting-the-ceiling, etc. Simon
|
|
|
|
Jay_PC
Senior Newbie 
|
 |
«
Reply #5 - Posted
2010-11-22 01:24:34 » |
|
When you reach the top of a jump, and you start moving down instead of up, then the behaviour is identical to falling.
I got it! hahahaha fall is cause by Gravity which constantly increases verticalSpeed unless player isGrounded, then jump only needs to change verticalSpeed's value in order to jump. then gravity will slowly change it back to falling, until isGrounded which sets verticalSpeed to 0. Thank you for putting up with my crazy, I think better when I try to explain what im thinking. I get this part now. 1 2
| speed += accel * deltaSeconds; location += speed * deltaSeconds; |
My thing is, how do I make That translate to pixels per frame?
|
|
|
|
|
Riven
|
 |
«
Reply #6 - Posted
2010-11-22 01:28:31 » |
|
Thank you for putting up with my crazy, I think better when I try to explain what im thinking. I get this part now. 1 2
| speed += accel * deltaSeconds; location += speed * deltaSeconds; |
My thing is, how do I make That translate to pixels per frame? You don't, you simply render the sprite at (int)location.x If you want to jump to a certain height, you have to either calculate the initial acceleration, or do some trial and error to find the value.
|
|
|
|
Jay_PC
Senior Newbie 
|
 |
«
Reply #7 - Posted
2010-11-22 01:40:43 » |
|
You don't, you simply render the sprite at (int)location.x
If you want to jump to a certain height, you have to either calculate the initial acceleration, or do some trial and error to find the value.
Edit: Ok I tried to implement this. This is my code: 1 2 3 4 5 6 7 8
| public void update(float deltaMilliseconds){ float deltaSeconds = (deltaMilliseconds/100); verticalSpeed += GRAVITY * deltaSeconds; float location = verticalSpeed * deltaSeconds; r.setY(location); System.out.println(location); } |
My results are interesting, because the delta value waivers back and forth.. sometimes making the location change in the opposite direction rapidly Example: 1 2 3 4 5 6 7 8
| 0.66247994 0.11759999 0.72128 1.9315802 0.63504 28.92176 0.21364 3.4251 |
|
|
|
|
|
SimonH
|
 |
«
Reply #8 - Posted
2010-11-22 02:57:07 » |
|
Check out the maths here then think of your character as a real thing in real time with real weight & gravity - that way it'll move like it's real! Don't worry about frames; when you draw transform the location from 'real' coordinates to 'screen' coordinates (pixels) and draw as it is in real time. PS use real numbers! (float)
|
|
|
|
Jay_PC
Senior Newbie 
|
 |
«
Reply #9 - Posted
2010-11-22 03:31:10 » |
|
Ill look into it, certain things I might leave out for simplicity sake, if anything I use Phys2D or somthing
|
|
|
|
|
Games published by our own members! Check 'em out!
|
|
Eli Delventhal
|
 |
«
Reply #10 - Posted
2010-11-22 04:21:48 » |
|
Going a little further with Ryan's code:
Position The object's X/Y location at any given time, draw the object and calculate collision from here. Velocity How fast the object is moving, in X/Y. Acceleration Forces that change the velocity, like gravity or jumping.
Position is never directly updated. Velocity changes position. Velocity is never directly updated. Acceleration changes velocity.
This means that you change an object's position only be applying an acceleration. Jumping is a big upward acceleration. Gravity is a constant downward acceleration.
|
|
|
|
Jay_PC
Senior Newbie 
|
 |
«
Reply #11 - Posted
2010-11-22 08:02:05 » |
|
Going a little further with Ryan's code:
Position The object's X/Y location at any given time, draw the object and calculate collision from here. Velocity How fast the object is moving, in X/Y. Acceleration Forces that change the velocity, like gravity or jumping.
Position is never directly updated. Velocity changes position. Velocity is never directly updated. Acceleration changes velocity.
This means that you change an object's position only be applying an acceleration. Jumping is a big upward acceleration. Gravity is a constant downward acceleration.
I get that, I actually have something working now. AND it includes Double Jumping and Floating... i'm working on Understanding how to play with it now and trying to make it more.... friendly... its state based but Shoot me. My issue is how to make the player not Fall Halfway through the floor. and how to make the jumps have longer hang time. I also Realized I COMPLETELY failed in my last code snipit... I have the Values Initializing in the function. resetting them every time
|
|
|
|
|
Jay_PC
Senior Newbie 
|
 |
«
Reply #12 - Posted
2010-11-22 08:54:59 » |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| private float x = 0f; private float y = 0f; private boolean isGrounded = false; private boolean isJumpOne; private boolean isJumpTwo; private boolean isHovering; private boolean isHitCeiling; private Vector2f position = new Vector2f(x,y); private Rectangle boundingBox = new Rectangle(x,y,64,128); private final float MAX_LEFT_FORCE = 9.8f; private final float MAX_RIGHT_FORCE = 9.8f; private final float MAX_SINGLE_JUMP_FORCE = 9.8f; private final float MAX_DOUBLE_JUMP_FORCE = 9.8f;
private final float GRAVITY = 9.8f; private float gravityMod; private float jumpForce = 0; private float leftForce = 0; private float rightForce = 0; private float verticalVelocity = 0f; private float horizontalVelocity = 0f;
public void update(float deltaMiliseconds){ float deltaSeconds = (deltaMiliseconds/1000); verticalVelocity += (GRAVITY * gravityMod) * deltaSeconds; verticalVelocity += jumpForce * deltaSeconds; position.y += verticalVelocity *deltaSeconds; horizontalVelocity += leftForce * deltaSeconds; horizontalVelocity += rightForce * deltaSeconds; position.x += horizontalVelocity *deltaSeconds; boundingBox.setX(position.x); boundingBox.setY(position.y); } public void jump(){ if(isGrounded){ jumpForce = -2; isGrounded = false; isJumpOne = true; }else if(isJumpOne){ jumpForce = -2; isJumpOne = false; isJumpTwo = true; }else if(isJumpTwo){ gravityMod = .1f; isJumpTwo = false; isHovering = true; }else if(isHovering){ gravityMod = 1f; isHovering = false; }else{ } } |
Any Opinions? Suggestions?
|
|
|
|
|
|