mini1012
mini1012

Reputation: 53

Jump and gravity LWJGL

I've been programming a game on LWJGL Java and JOML, about a platformer.

So the inputs are ok, i can use WASD and move arround the map, but i dont know how to convert that into a AD and W jumping with gravity.

So for setting the yPos of my player i have transform.pos.y = SOME YPOS INT. I don't have gravity or speed variables

package entity;

import org.joml.Vector2f;
import org.joml.Vector3f;
import org.lwjgl.glfw.GLFW;

import io.Window;
import render.Animation;
import render.Camera;
import world.World;

public class Player extends Entity{
    public static final int ANIM_IDLE = 0;
    public static final int ANIM_WALK = 1;
    public static final int ANIM_SIZE = 2;
    public Player(Transform transform) {
        super(ANIM_SIZE, transform);
        setAnimation(ANIM_IDLE, new Animation(1, 2, "player/idle"));
        setAnimation(ANIM_WALK, new Animation(2, 5, "player/walking"));
    }
    
    @Override
    public void update(float delta, Window window, Camera camera, World world) {
        Vector2f movement = new Vector2f();
        if(window.getInput().isKeyDown(GLFW.GLFW_KEY_A)) {
            movement.add(-10*delta, 0);
        }
        if(window.getInput().isKeyDown(GLFW.GLFW_KEY_D)) {
            movement.add(10*delta, 0);
        }
        
        if(window.getInput().isKeyDown(GLFW.GLFW_KEY_W)) {
            movement.add(0, 20*delta);
        }
        if(window.getInput().isKeyDown(GLFW.GLFW_KEY_S)) {
            movement.add(0, -10*delta);
        }
        
        move(movement);
        
        if(movement.x != 0 || movement.y != 0)
            useAnimation(ANIM_WALK);
        else
            useAnimation(ANIM_IDLE);
        
        camera.getPosition().lerp(transform.pos.mul(-world.getScale(), new Vector3f()), 0.1f);
    }
    public void checkOnGround() {
        
    }
}

Upvotes: 0

Views: 45

Answers (1)

Kevin Ghossoub
Kevin Ghossoub

Reputation: 81

LWJGL is a lightweight game development library that does not contain a built in physics library. In order to get jumping and gravity to work only using JOML and LWJGL, you will need to create your own collision detection and physics integration code which is too complicated to be explained here. If you want to do that, there are plenty of books on the subject. However, if you are new to programing or do not have much interest in physics programing, I recommend finding an additional physics package or using a full game engine which includes physics code.

Upvotes: 0

Related Questions