How to add gravity? + Collisoin Troubles

Started by ChaoticCactus, June 11, 2013, 00:43:01

Previous topic - Next topic

ChaoticCactus

Hello,

I was just curious how I would add gravity in a platform game.  I have a collision system, but I'm not sure how to add gravity.  Any ideas?

EDIT: Actually, speaking of my collision system, I'm having some trouble getting the "Enemy" to move at all.  This is what I have in the update class.

@Override
    public void update() {
        boolean move = true;
        boolean jump = false;

        ArrayList<GameObject> objects = Game.rectangleCollide(x, y, x + 48, y + 48);

        for(GameObject go : objects) {
            if(go.getSolid()) {
                move = false;
            }
        }

        if(!move) {  ****
            return;   ****
        }               ****

        x += velX;
        y += velY;
    }


And this is the rectangle Collide method that is called:

public static ArrayList<GameObject> rectangleCollide(float x1, float y1, float x2, float y2) {
        ArrayList<GameObject> res = new ArrayList<GameObject>();

        float sx = x2 - x1;
        float sy = y2 - y1;

        Rectangle collider = new Rectangle((int) x1, (int) y1, (int) sx, (int) sy);

        for(GameObject go : game.getObjects()) {
            if(Physics.checkCollision(collider, go) != null) {
                res.add(go);
            }
        }

        return res;
    }


When I run the game the Enemy just doesn't move.  I've narrowed it down to these line of code.  (Marked by ****)

Daslee

If you can't move when collided try this:

if(!move) {
            x -= velX;
            y -= velY;
            return;
        }

        x += velX;
        y += velY;


When I was making 2d game, so I were using something like that, but I actually done it with moving directions. For example when player collides, check to which direction he is moving if going left, then x += velX else if moving right then x -= velX and with Y same.

And gravity:
Quote from: Simon Felix on May 21, 2012, 02:02:53
Usually (simplified pseudo code):

final double TIME_STEP=1/60.0;
final double GRAVITY=9.81;
boolean jumping;
double yPosition;
double yVelocity;

void perFrame()
{
  if(jumpKeyIsPressed() && !jumping)
  {
    jumping=true;
    yVelocity=-1;
  }
  
  if(playerIsOnTheFloor())
  {
    jumping=false;
    yVelocity=0;
  }
  
  yPosition+=yVelocity*TIME_STEP;
  yVelocity+=GRAVITY*TIME_STEP;
}


ChaoticCactus

Thanks for replying!
I haven't implemented the jumping yet, but about that collision.

So when I changed the if(!move) statement to x -= velX;  The enemy just moves to the right, whereas it should move to the left.  And it's definitely not colliding with anything as all of this is happening right as the game begins.  What's going wrong here?

Daslee

Did you added return; after if(!move){ x -= xVel; ?

ChaoticCactus

Yes.  If I don't add the return it just doesn't move at all.

Daslee

Try this:

public void update() {
	x += velX;
        y += velY;

        ArrayList<GameObject> objects = Game.rectangleCollide(x, y, x + 48, y + 48);

        for(GameObject go : objects) {
            if(go.getSolid()) {
                x -= velX;
		return;
            }
        }
    }

ChaoticCactus

Nope, that doesn't work either.  The Enemy just stands still... could there be something wrong with the variables that I pass into for Game.rectangleCollide() ?

EDIT: Okay, so I figured out what the problem is and how to get the Enemy to move, but it doesn't collide with anything.
@Override
    public void update() {
        float newX = x + velX;
        float newY = y + velY;

        ArrayList<GameObject> objects = Game.rectangleCollide(newX, newY, newX + 48, newY + 48);

        velX = -0.03f * Time.getDelta();

        x = newX;
        y = newY;

        for(GameObject go : objects) {
            if(go.getSolid()) {
                velX = 0f; /* This is where the problem is.  It seems that the Enemy is ALWAYS colliding with something, because if I set this to
                                   0, than the Enemy doesn't move.  If I put return; than the Enemy moves but doesn't collide with anything.... Hmmm. =\ */
            }
        }
    }

Daslee

for(GameObject go : objects) {
            if(go.getSolid()) {
                x -= velX;
                velX = 0f; /* This is where the problem is.  It seems that the Enemy is ALWAYS colliding with something, because if I set this to
                                   0, than the Enemy doesn't move.  If I put return; than the Enemy moves but doesn't collide with anything.... Hmmm. =\ */
            }
        }


Replace this. And if not works, try to remove velX = 0f; line. If this code doesn't works so I don't know whats wrong here... You can try pm me in skype (I've send you pm with my skype) and then I can try to help you through teamviewer.