# Double jump 3d

This topic is 949 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

HI everyone

Ive been experimenting with gravity in my project and with much input from others Ive managed to

add a jumping element, and now I'm working on implementing a double jump to the method but so

far Ive run into many brick walls trying to do so, and was hoping if anyone had any suggestions as to

how to go about doing it.

Thankyou

The Methods I used are below

The jump method

void DudeGravity2(GameTime gameTime)
{

float treshold = 0.0f;
float terrainHeight = GetExactHeightAt(avatarPosition2.X, -avatarPosition2.Z);
if (avatarPosition2.Y < terrainHeight + treshold)
{
newPos = avatarPosition2;
newPos.Y = terrainHeight + treshold * (float)gameTime.ElapsedGameTime.TotalSeconds;

avatarPosition2 = newPos;

gravityVelocity = 0;// On ground so not falling.

//jump
if (keyState.IsKeyDown(Keys.L))
{
gravityVelocity = 1f;       // Adjust this value to control height of jump.
}

}
else
{
// Not on ground so must be falling.

//Add in effect of gravity on current velocity.
// m/sec^2
float g = -1.10f;
gravityVelocity += g * (float)gameTime.ElapsedGameTime.TotalSeconds;

//Add effect of velocity to position.
newPos = avatarPosition2;
newPos.Y += gravityVelocity * (float)gameTime.ElapsedGameTime.TotalSeconds;
avatarPosition2 = newPos;

}



The method I used to get the height of the terrain

(GetExactHeightAt)

 public float GetExactHeightAt(float xCoord, float zCoord)
{
bool invalid = xCoord < 0;
invalid |= zCoord < 0;
invalid |= xCoord > floorPlan.GetLength(0) - 1;
invalid |= zCoord > floorPlan.GetLength(1) - 1;
if (invalid)
return 10;

int xLower = (int)xCoord;
int xHigher = xLower + 1;
float xRelative = (xCoord - xLower) / ((float)xHigher - (float)xLower);

int zLower = (int)zCoord;
int zHigher = zLower + 1;
float zRelative = (zCoord - zLower) / ((float)zHigher - (float)zLower);

float heightLxLz = floorPlan[xLower, zLower];
float heightLxHz = floorPlan[xLower, zHigher];
float heightHxLz = floorPlan[xHigher, zLower];
float heightHxHz = floorPlan[xHigher, zHigher];

bool cameraAboveLowerTriangle = (xRelative + zRelative < 1);

float finalHeight;
if (cameraAboveLowerTriangle)
{
finalHeight = heightLxLz;
finalHeight += zRelative * (heightLxHz - heightLxLz);
finalHeight += xRelative * (heightHxLz - heightLxLz);
}
else
{
finalHeight = heightHxHz;
finalHeight += (1.0f - zRelative) * (heightHxLz - heightHxHz);
finalHeight += (1.0f - xRelative) * (heightLxHz - heightHxHz);
}

return finalHeight;
}


The terrain I used is from riemers flightsim tutorial(link below)

##### Share on other sites

when they press jump, if they are on the ground, set vy to initial upward velocity. else do nothing.

then simply apply gravity over time. they will automatically jump again when they touch the ground (assuming jump is pressed).

##### Share on other sites

Thanks for the response Norm but I have one more question, is there a way to apply this

technique while in mid air like in such games as mario 64( I provided sample examples below)

Thanks again :)

(Mario does a mid air double jump at about 21 seconds into the video)

here's a 2d version of the midair double jump

here's a 2d jpeg illustration. The character does a mid air jump avoiding the water

[attachment=31758:IMG_1356.jpg]

Edited by football94

##### Share on other sites

Problem solved I had much help from the link below

http://xboxforums.create.msdn.com/forums/t/113607.aspx

modified code

bool doubleJump; //logs whether the player has done a double jump yet

void DudeGravity2(GameTime gameTime)
{
float treshold = 0.0f;
float terrainHeight = GetExactHeightAt(avatarPosition2.X, -avatarPosition2.Z);
if (avatarPosition2.Y < terrainHeight + treshold)
{
newPos = avatarPosition2;
newPos.Y = terrainHeight + treshold * (float)gameTime.ElapsedGameTime.TotalSeconds;

avatarPosition2 = newPos;

gravityVelocity = 0;// On ground so not falling.

//jump
if (keyState.IsKeyDown(Keys.L))
{
doubleJump = false;
gravityVelocity = 1f;       // Adjust this value to control height of jump.
}
}
else
{
// Not on ground so must be falling.

//Add in effect of gravity on current velocity.
// m/sec^2
float g = -1.10f;
gravityVelocity += g * (float)gameTime.ElapsedGameTime.TotalSeconds;

//Add effect of velocity to position.
newPos = avatarPosition2;
newPos.Y += gravityVelocity * (float)gameTime.ElapsedGameTime.TotalSeconds;
avatarPosition2 = newPos;

//jump
if (!doubleJump && keyState.IsKeyDown(Keys.L))
{
doubleJump = true;
gravityVelocity = 1f;       // Adjust this value to control height of jump.
}
}


another way of doing the same thing separate from the dudegavity2 method

KeyboardState oldState;

KeyboardState newState = Keyboard.GetState();  // get the newest state

if (newPos.Y <= 1f)
{

//jump
if (oldState.IsKeyUp(Keys.R) && newState.IsKeyDown(Keys.R))
{
doubleJump = false;
gravityVelocity = 1f;      // Adjust this value to control height of jump.
}

else
{
//jump
if (!doubleJump && keyState.IsKeyDown(Keys.R))
{

doubleJump = true;
gravityVelocity = 1f;
}
}
oldState = newState;

}


1. 1
2. 2
3. 3
4. 4
Rutin
12
5. 5

• 26
• 10
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633694
• Total Posts
3013378
×