Robertvokac (talk | contribs) No edit summary |
Robertvokac (talk | contribs) m Robertvokac moved page Development of Open Eggbert to Gravity without leaving a redirect |
||
(14 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
== | == Implementing jumping == | ||
To implement a jump mechanic, you can follow these steps: | |||
* | 1. **Define Variables:** | ||
- Create variables for the player's velocity, gravity, and whether the player is on the ground. | |||
```java | |||
= | float velocityY = 0; | ||
= | final float gravity = -0.5f; | ||
= | boolean onGround = true; | ||
``` | |||
2. **Jump Logic:** | |||
- Implement the jump by applying an upward velocity when the jump key is pressed and the player is on the ground. | |||
```java | |||
if (Gdx.input.isKeyPressed(Input.Keys.SPACE) && onGround) { | |||
velocityY = 10; // Jump strength | |||
onGround = false; | |||
} | |||
``` | |||
3. **Apply Gravity:** | |||
- Continuously apply gravity to the player's vertical velocity. | |||
```java | |||
velocityY += gravity; | |||
``` | |||
4. **Update Player Position:** | |||
- Update the player's Y position using the current velocity. | |||
```java | |||
player.y += velocityY; | |||
``` | |||
5. **Ground Detection:** | |||
- Check if the player has landed back on the ground to reset the jump. | |||
```java | |||
< | if (player.y <= groundLevel) { | ||
player.y = groundLevel; | |||
velocityY = 0; | |||
onGround = true; | |||
} | |||
``` | |||
By following these steps, you create a basic jumping mechanic where the player can jump when on the ground, and gravity pulls them back down. This can be expanded with more complex mechanics like double jumps or variable jump heights. | |||
== | == Gravity == | ||
https://gamedev.stackexchange.com/questions/111314/writing-my-own-gravity | |||
https://gamedev.stackexchange.com/questions/ | |||
Latest revision as of 14:54, 19 April 2025
Implementing jumping
To implement a jump mechanic, you can follow these steps:
1. **Define Variables:**
- Create variables for the player's velocity, gravity, and whether the player is on the ground.
```java
float velocityY = 0;
final float gravity = -0.5f;
boolean onGround = true;
```
2. **Jump Logic:**
- Implement the jump by applying an upward velocity when the jump key is pressed and the player is on the ground.
```java
if (Gdx.input.isKeyPressed(Input.Keys.SPACE) && onGround) {
velocityY = 10; // Jump strength
onGround = false;
}
```
3. **Apply Gravity:**
- Continuously apply gravity to the player's vertical velocity.
```java
velocityY += gravity;
```
4. **Update Player Position:**
- Update the player's Y position using the current velocity.
```java
player.y += velocityY;
```
5. **Ground Detection:**
- Check if the player has landed back on the ground to reset the jump.
```java
if (player.y <= groundLevel) {
player.y = groundLevel;
velocityY = 0;
onGround = true;
}
```
By following these steps, you create a basic jumping mechanic where the player can jump when on the ground, and gravity pulls them back down. This can be expanded with more complex mechanics like double jumps or variable jump heights.
Gravity
https://gamedev.stackexchange.com/questions/111314/writing-my-own-gravity