-
|
|
Sprite movement: max jump height
|
I want to create a max height my sprite can jump.
I thought I created a startposition (just before jumping) and compared it with the position during the jump.
When the startposition - jumpposition > 100 the Sprite must go down.
Unfortunately this doesn't work because I use this kind of movement: "Position -= 2;"
How would I make it so that there is a max jump height using "Position -= 2; ?
Thanks!!
|
|
-
|
|
Re: Sprite movement: max jump height
|
Position -= 2? Is that the Y position? A Vector? How much is 2? 2 Pixels? Can you post some more code so we know how you are doing your movement?
Regards, Louis Ingenthron Fortis Venaliter Lead Developer of FV Productions
|
|
-
|
|
Re: Sprite movement: max jump height
|
If you use a counter (an int variable for instance) you can set it to the maximum time allowed for a jump (in your case 50, maximum jump height units / units per game update, 100 / 2). While the counter is over 0 you update the jump movement, decrement the jump counter (minus 1) then check if you've hit a ceiling (if so set the jump counter to 0) and repeat.
IndivisibleMan
|
|
-
-
- (466)
-
premium membership
MVP
-
Posts
349
|
Re: Sprite movement: max jump height
|
Or you could add some simple physics..
So your sprite would have the following fields: (texure I am using is 32x32)
Vector2 velocity; Vector2 lastFrame; Vector2 accVelocity; Vector2 position; float mass = 1;
float damp = .5f;
Vector2 Gravity = new Vector2(0,9.45f); // Earth like gravity.
and in each update you can then do something like this:
// Apply the gravity to the accumilated velocity
accVelocity += Gravity * mass;
// Probably put this in it's own Jump method off the class. if (Keyboard.GetState().IsKeyDown(Keys.Space) && !jumping) { jumping = true; accVelocity += new Vector2(0, -250); }
// If I am at the bottom of the screen, then I am not jumping... you will have different logic for this..
if (position.Y == GraphicsDevice.Viewport.Height - 32) jumping = false;
// Not using acceleration so set the last frame to Zero lastFrame = Vector2.Zero; // Apply the accumilated velocity and multiply by the inverse mass lastFrame += accVelocity * (1.0f / mass); float time = (float)gameTime.ElapsedGameTime.TotalSeconds; // Apply to the velcity over time.
velocity += lastFrame * time;
// Drag
velocity *= (float)Math.Pow(damp, time);
// update the position with the velocity
position += velocity;
// Drag
velocity *= (float)Math.Pow(damp, time);
// Keep it on the screen.
position.X = MathHelper.Clamp(position.X, 0, GraphicsDevice.Viewport.Width - 32); position.Y = MathHelper.Clamp(position.Y, 0, GraphicsDevice.Viewport.Height - 32);
// Reset the accumilated velocity.
accVelocity = Vector2.Zero;
As I say this is a pretty simple bit of physics, but should give you something to expand on.
|
|
-
|
|
Re: Sprite movement: max jump height
|
@chris
As the default behaviour for time in an XNA game is using a fixed timestep, why not work out the timestep at initialization (1000 milliseconds / 60 frames per second) and set the variable float time to be that. Unless you decide, for some crazy reason, to have a non-fixed timestep for the game physics. The physics in Chris's post would look nicer than the simple counter, use it if possible.
|
|
-
-
- (466)
-
premium membership
MVP
-
Posts
349
|
Re: Sprite movement: max jump height
|
Because the gameTime is there so I used that and it will also be constant regardless of time step. I presume you you meam me anyway, my name is Charles, not Chris lol
|
|
-
|
|
Re: Sprite movement: max jump height
|
Wow, this is more then I hoped for!! Thx all for helping me out!
I thought this was a simple question, so thats way I didn't give more information.
The code looks like this:
//class Sprite: Movement
Vector2 Position = new Vector2(150, 480); // drawn on screen
Vector2 Direction = Vector2.Zero;
//Update the Sprite and change it's position based on the passed in speed, direction and elapsed time.
public void Update(GameTime theGameTime, Vector2 theSpeed, Vector2 theDirection)
{
Position += theDirection * theSpeed * (float)theGameTime.ElapsedGameTime.TotalSeconds;
}
---
// class Movement:
Vector2 Speed = Vector2.Zero;
const int Sprite_Speed = 0; // I put it on zero, because my sprite moved very very fast!! ( now normal speed)
Vector2 StartPosition= Vector2.Zero;
Vector2 Position = new Vector2(0, 0);
enum Direction
{
Idle,
Walking,
Up
}
Direction CurrentDirection = Direction.Idle;
---
public void Update(GameTime theGameTime)
{
GamePadState CurrentGamePad = GamePad.GetState(PlayerIndex.One);
KeyboardState CurrentKeyboard = Keyboard.GetState();
UpdateUp(CurrentKeyboard);
base.Update(theGameTime, Speed, Position);
}
public void UpdateUp(KeyboardState CurrentKeyboard)
{
if (CurrentDirection == Direction.Idle)
{
if (CurrentKeyboard.IsKeyDown(Keys.Up) == true)
{
StartPosition = Position; // The current position of the sprite just before jump becomes the Startposition
Snelheid = new Vector2(Eekhoorn_Speed, Eekhoorn_Speed);
Position.Y -= 2;
Position.X -= 0; //may not jump right or left
SpriteSource = new Rectangle(230, 0, 95, 160); //show the small part of the big image where the sprite is jumping
}
}
if (CurrentDirection == Direction.Up)
{
if (StartPosition.Y - Position.Y > 150) // make a max hight the sprite kan jump
{
Position.Y += 2;
}
if (Position.Y > StartPosition.Y) // if on the ground, stop and show sprite image idle
{
CurrentDirection = Direction.Idle;
}
}
}
I had before another code for movement ( Position.Y = +1) and then the coordinates of my Sprite
went just 1 higher on X or Y as by every movement. I changed it because my BoundingBox didn't want to work.
It seems to me that " if (StartPosition.Y - Position.Y > 150)" doesnt work. I have to find a way around this, I thought maybe
count the times the player hits the Up key ( after 75 times, stop, or something?) but I am an beginner, is there an simple way of doing this?
|
|
-
-
- (466)
-
premium membership
MVP
-
Posts
349
|
Re: Sprite movement: max jump height
|
If you use the method I posted you can limit the jump height in this line:
accVelocity += new Vector2(0, -250);
by changing the value -250 to something else. What I would do would have something like a jump strength field that I can set, so then I could have a pick up that increased this value and make me jump higher or a nasty pickup that will lower the jump height.
I personally think you are better of using a velocity and managing the direction of motion that way.
|
|
-
-
- (5798)
-
premium membership
MVP
-
Posts
3,118
|
Re: Sprite movement: max jump height
|
I also have a tutorial where I implement some very basic jumping code. It does use a max height however to determine how high the jump should go. It should be noted however that isn't the most natural way to do a jump. Really I should be decreasing and increasing his vertical speed to give it a more natural arc. But it definitely works as an intro to making a character jump.
|
|
-
-
- (3213)
-
premium membership
-
Posts
1,516
|
Re: Sprite movement: max jump height
|
Zomba:It seems to me that " if (StartPosition.Y - Position.Y > 150)" doesnt work.
If this doesn't work, set a breakpoint at this line and see what values you have when you expect it to work. It's much easier to figure out how something works when you actually know what it's doing rather than guessing or making assumptions.
I would go with one of the physics based approaches already suggested here... sure you can set your code that that after a max height is reached, the player starts falling, but it's gonna be jerky and unrealistic looking (player will instantly go from moving up to moving down)... using a physics based approach, the player will gradually slow down and then eventually go from moving up to moving down.
Another advantage to this route is that you don't have to check how high the player has jumped... you just apply gravity as long as the player is in the air (it doesn't matter if they're moving up or down, it's always the same).
|
|
-
|
|
Re: Sprite movement: max jump height
|
@ George: My script is partly based on the tutorial you mentioned. I could not use that particular movement, because it did not
work with the boundingbox I created. Now there is collision, but the max jump height was failing for a couple of days...en that brought me here :)
@ Charles: thank you for the example you gave me to experimenting with :)
Now I know what I have to do, so I wanna thank you all for your time and your advice! I'm going back to experimenting on my game with new energy
to add some gravity to my sprite.
Thx :)
|
|
-
-
- (5798)
-
premium membership
MVP
-
Posts
3,118
|
Re: Sprite movement: max jump height
|
Not sure why my tutorial wasn't working with a bounding box collision, but either way, glad you got it working and I'm glad you found my tutorial useful as well. Always good to here that it's helping someone get started!
|
|
-
-
- (466)
-
premium membership
MVP
-
Posts
349
|
Re: Sprite movement: max jump height
|
Zomba: @ George: My script is partly based on the tutorial you mentioned. I could not use that particular movement, because it did not work with the boundingbox I created. Now there is collision, but the max jump height was failing for a couple of days...en that brought me here :)
@ Charles: thank you for the example you gave me to experimenting with :)
Now I know what I have to do, so I wanna thank you all for your time and your advice! I'm going back to experimenting on my game with new energy to add some gravity to my sprite.
Thx :)
Great! As George says, it's always nice to know that people benefit from what we put up. Good luck :)
|
|
-
|
|
Re: Sprite movement: max jump height
|
George Clingerman:Not sure why my tutorial wasn't working
with a bounding box collision, but either way, glad you got it working
and I'm glad you found my tutorial useful as well. Always good to here
that it's helping someone get started!
@ George: Your tutorials are advised and used by my highschool teachers in Holland during a school course. You already know, but here again: they are VERY useful.
As an exercise we had to complete all your tutorials within 1,5 hours, then we get the assignment to build an 2d xbox game with 3 levels with level of difficulty in 5 weeks, completely object orientated. Without your tutorials we were thrown in the deep accourding to C#, so keep on the good work!! :D
And the BoundingBox: I don't know why it is not working. I tried for a week to make collision work...Then I gave up and found on your website a game called Movethegrowingcreature withs I analysed, played with the collision part and take that as an example for my game.
Omg, guys...I didn't had to asked this question in the first place! I just found the error why my boundingbox didn't work in the old situation. This is super!! I learned a lot by playing with the that game Movethegrowingcreature that I forgot to implement this in the old file..I just realise it now.. my game is playable :D
Hahaha: this forum, you guys are great: just that simple remark saved me hours of work impelementing gravity :D
|
|
-
-
- (5798)
-
premium membership
MVP
-
Posts
3,118
|
Re: Sprite movement: max jump height
|
That's amazing that your teachers used my tutorials! I wish they would have contacted me, I would have loved to have known that was going on! And congrats on getting your problem all figured out.
Best of luck as you keep on learning and well, you know where to go when you have more questions :)
|
|
|