0
\$\begingroup\$

I really can't work out whats wrong with my code. I have an input listener for gestures, when I Swipe the position goes up on the y axis fine but he doesn't come back down, if with Gravity added. I'll show you any code relevant to the Character.

World.java : updateRuben

private void updateRuben (float deltaTime, float accelX) {


        if (Ruben.getState() != Ruben.RUBEN_STATE_IDLE && ruben.position.y <= 0.5f) ruben.hitPlatform();//
        if (Ruben.getState() != Ruben.RUBEN_STATE_IDLE) ruben.velocity.x = -accelX / 10 * Ruben.RUBEN_MOVE_VELOCITY;

                // clamp the velocity to 0 if it's < 1, and set the state to standing
                if (Math.abs(ruben.velocity.y) < 1) {
                    ruben.velocity.y = 0;
                    Ruben.setState(Ruben.RUBEN_STATE_IDLE);
                }



        ruben.update(deltaTime);
        heightSoFar = Math.max(ruben.position.y, heightSoFar);
    }

Ruben.java

package com.wilson.game.Game;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.input.GestureDetector;
import com.wilson.game.System.DynamicGameObject;
import com.wilson.game.System.World;
import com.wilson.game.System.inputController;

public class Ruben extends DynamicGameObject {
    public static final int RUBEN_STATE_IDLE = 0;
    public static final int RUBEN_STATE_JUMP = 1;
    public static final int RUBEN_STATE_FALL = 2;
    public static final int RUBEN_STATE_HIT = 3;
    public static final int RUBEN_STATE_WALKING = 4;
    public static final int RUBEN_STATE_SPAWN = 5;

    public static final float RUBEN_MAX_VELOCITY = 11;
    public static final float RUBEN_JUMP_VELOCITY = 11;
    public static final float RUBEN_MOVE_VELOCITY = 20;
    public static final float RUBEN_WIDTH = 2f;
    public static final float RUBEN_HEIGHT = 2.5f;
    public static final float RUBEN_SPAWN_TIME = 0.4f;

    public static int state;
    static float stateTime;
    private static boolean grounded;
    public boolean facingRight;

    public Ruben (float x, float y) {
        super(x, y, RUBEN_WIDTH, RUBEN_HEIGHT);
        state = RUBEN_STATE_SPAWN;
        stateTime = 0;
        grounded = true;
        facingRight = true;

        Gdx.input.setInputProcessor(new GestureDetector(new inputController()));
        }

    public void update (float deltaTime) {
        processInput(); 
        position.add(velocity.x * deltaTime, velocity.y * deltaTime);
        velocity.add(World.gravity.x * deltaTime, World.gravity.y * deltaTime);

        bounds.x = RUBEN_WIDTH;
        bounds.y = RUBEN_HEIGHT;

        if (state == RUBEN_STATE_SPAWN) {
            if (stateTime > RUBEN_SPAWN_TIME) {
                state = RUBEN_STATE_IDLE;
                grounded = true;
            }
        }

        if (position.x * deltaTime < 0) position.x = World.WORLD_WIDTH;
        if (position.x * deltaTime > World.WORLD_WIDTH) position.x = 0;

        stateTime += deltaTime;
}

    private void processInput() {
        if (inputController.fling == true) {
            jump();
            }
        }

    public void hitSquirrel () {
        velocity.set(0, 0);
        state = RUBEN_STATE_HIT;
        stateTime = 0;
    }

    public void jump() 
    {
        if (state != RUBEN_STATE_JUMP)
        state = RUBEN_STATE_JUMP;
        velocity.y = velocity.y * RUBEN_JUMP_VELOCITY;
        grounded = false;
        stateTime = 0;
    }

    public void hitPlatform () {
        state = RUBEN_STATE_IDLE;
        stateTime = 0;
        grounded = true;
    }

    public void hitSpring () {
        velocity.y = RUBEN_JUMP_VELOCITY * 1.5f;
        state = RUBEN_STATE_JUMP;
        stateTime = 0;
        grounded = false;
    }

    public static int getState() {
        return state;
    }

    public static float getStateTime() {
        return stateTime;
    }

    public static void setState(int currentstate) {
        currentstate = state;
    }

    public static boolean isGrounded() {
        return grounded;
    }

    public static void setStateTime(float i) {

        i = stateTime;

    }
}

How do you apply physics to the actor in order for him to come back down? what am i missing?

\$\endgroup\$
1
\$\begingroup\$
//At the apex of the jump, Ruben's velocity.y is ~0
if (Math.abs(ruben.velocity.y) < 1) //If Ruben's velocity.y gets close to 0...
{
   ruben.velocity.y = 0; //stop him
   Ruben.setState(Ruben.RUBEN_STATE_IDLE);
}

//velocity.y = +1 //jumping
//velocity.y = 0 //no more jump left; begin falling (except the if activates)
//velocity.y = -1 //falling

Edit - Some other, non-related things:

void jump()...
//Add braces; no need to do any of this if already jumping
if (state != RUBEN_STATE_JUMP)
{
    state = RUBEN_STATE_JUMP;
    velocity.y = velocity.y * RUBEN_JUMP_VELOCITY;
    grounded = false;
    stateTime = 0;
}

void update()...
//This is more typical (reversed)
velocity.add(World.gravity.x * deltaTime, World.gravity.y * deltaTime);
position.add(velocity.x * deltaTime, velocity.y * deltaTime);

void update()...
if (position.x * deltaTime >...) //What?!? Position * deltaTime??

void update()...
//You are negating part of velocity if the player "wraps"
//This wraps them, but preserves their position delta.
if (position.x > World.WORLD_WIDTH) position.x -= World.WORLD_WIDTH;
if (position.x < 0) position.x += World.WORLD_WIDTH;
\$\endgroup\$
12
  • \$\begingroup\$ Thanks for your reply, i've already put that code inside the updateRuben() method inside the World class. Any idea why it couldn't be working? \$\endgroup\$
    – BIW
    Apr 18 '15 at 21:05
  • \$\begingroup\$ @BradleyWilson, please see edit; ruben "stops", momentarily, when gravity has, just now, countered his initial velocity. Then, he accelerates back toward 1 (although negative now). \$\endgroup\$
    – Jon
    Apr 18 '15 at 21:08
  • \$\begingroup\$ @BradleyWilson, not that it's THE problem but, typically, velocity is integrated (first), then applied to position. \$\endgroup\$
    – Jon
    Apr 18 '15 at 21:10
  • \$\begingroup\$ so you're saying i need to add more into the update ruben method instead just indicating the IDLE state, i need to add for Jump, finish jump and fall? \$\endgroup\$
    – BIW
    Apr 18 '15 at 21:14
  • 1
    \$\begingroup\$ @BradleyWilson, hey it's finally done... send me that git \$\endgroup\$
    – Jon
    Apr 19 '15 at 13:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.