Movement code critique?

Can someone take a look at this code for me and make sure it all checks out OK? I’m getting random times where yCollision is equal to false when I’m standing on a surface.


	public void move(int delta){
		xCollision = false;
		yCollision = false;
		pastX = x;
		pastY = y;
		x = nextX;
		y = nextY;
		collisionBox = new CollisionBox(x,y, sprite.getWidth(),sprite.getHeight()).rotate(rot,getCenterPoint());
		velocity.y += (G_ACCELERATION * delta)/1000.0;
		nextX = x + (velocity.x * delta)/1000.0;
		if(doesCollideOnX( new CollisionBox(nextX, y, sprite.getWidth(),sprite.getHeight()).rotate(rot, getCenterPoint()))){
			nextX = x;
			velocity.x = 0.0; 
			xCollision = true;
		}
		nextY = y + (velocity.y * delta)/1000.0;
		if(doesCollideOnY(new CollisionBox(nextX, nextY, sprite.getWidth(),sprite.getHeight()).rotate(rot, getCenterPoint()))){
			nextY = y;
			velocity.y = 0.0;
			yCollision = true;
		}

	}

What are the types on pastX/pastY/x/y?
Is the object trembling around?
And why is delta an int? :persecutioncomplex:

My shallow knowledge tells me that something in line 9 and/or 16 is messing up your stew: If delta == 0, your Y position doesn’t change at all, and no collision is found (since you are checking collisions with your actual position), hence the yCollision = false statement.

Should provide the methods: doesCollideOnY

anything related to positions is a double, the object isn’t trembling, and I use an int because I feel like it’s easier to work with.
I can’t provide the doesCollideOnY method because this is part of an abstract class so it’s different for everything that extends it.
@Rorkien It’s supposed to run every 16 milliseconds. delta should always be 16. What do you suppose I do to fix this then?

No idea. ;D

Well, assuming your delta is always 16, your velocity.y should be constant whenever yCollision returns true.
Is yCollision changed somewhere else for this object? Or just when doesCollideOnY returns true?

I usually fill the method with println() to grab and compare all the possible data (mad debug skillz).
Try grabbing the nextY value right after line 16 and check if it’s changing right along with yCollision

Well, I threw some println statements in there and every once in a while, the velocity will be a little bit less than usual. the only way that that makes any sense would be if delta is lower than 16, which doesn’t really make any sense.

My bet is that there’s something wierd in doesCollideOnY(). I would start there and find out why it’s returning false when it’s not supposed to.

I usually just use standard Rectangle.intersects collisions in doesCollideOnY(), there’s nothing wrong with that section.

It could be that the lastposition push is further away than the gravitational pull in one cycle. That would explain why it´s only happening in the y-axis. How random is “yCollide == false”? Is it just once in a while or do the object sometimes get stuck in a false state?

Look for x-collisions with nextY as well.

It’s intermittent. I’ve never seen the object stuck in the false stage.

Did you have any luck with my suggestion?

From what I can tell, this occurs whenever my deltaTime is NOT 16. I really have no idea what to do about this.

If the delta gets too large and the objects are moving fast, it could also happen that they are tunneling through surfaces.
A simple solution is to break down the delta and put the collision check into a loop to check the whole movement path.