Search code examples
javaopengllwjgl

Opengl java bouncing entities


Hello this is actually my first question here.. I have been developing a opengl lwjgl game and I'm working on the entities class.I have accomplished to make an entity jump(ill use this for animals) but the "issue" (99.9% sure its my fault) is that each time the entity touches the terrain and jumps again the jump is higher as seen in the video I recorded this is the code for jump thz =D I just want the tree to move up always the same.

Video

    double velocity = 0;
double initVelX;
double initVelZ;
double time = 0;
float x;
float y;
float z;

public void bounce() {

    double initialVelocity = 0.1;       
    double speed = 1/2500.0;

    if(time == 0) {
        velocity += initialVelocity;
    }
    time += speed;
    velocity = velocity - 9.8 * speed;
    if(y + velocity < 0.1){
        velocity *= -1;
    }
    y += velocity;
    setPosition(new Vector3f(getPosition().x,y,getPosition().z));
}

Solution

  • Fixed it just had to do a simple line of code and it even has the speed fluctuations correct:

    double velocity = 0;
        double initVelX;
        double initVelZ;
        double time = 0;
        float x;
        float y;
        float z;
    
        public void bounce() {
    
            double initialVelocity = 0.1;       
            double speed = 1/2500.0;
    
            if(time == 0) {
                velocity = initialVelocity;
            }
    
            time += speed;
            velocity = velocity - 9.8 * speed;
            if(y + velocity < 0.1){
                velocity *= -1;
                velocity = initialVelocity;
            }
            y += velocity;
            setPosition(new Vector3f(getPosition().x,y,getPosition().z));
        }