[Slick2D] Strange collision glitch.

So heres the glitch:

The player runs right through the zombie. Then after a few seconds it responds to the collision. That looks terrible for one, and also you could still shoot after you were obviously dead. It only happens when you walk directly towards the zombie though. Otherwise its quite responsive.

I’m using the slick rectangles along with the slick intersects.

So whats the problem. What am I doing wrong? Is it a bunch of lag?

I don’t know what is making it do this but I do know that it’s not going to work.

Code:
Player:


...
public Rectangle getBounds() {
return new Rectangle((int)x, (int)y, main.getWidth(), main.getHeight());
}
...

Zombie:


...
public void update(int delta) {
		
	collision();
		
	main.update(delta);
		
	if(right) {
		x += .13;
	}else{
		x -= .13;
	}
}
	
public void collision() {
	if(getBounds().intersects(p.getBounds())) {
		System.exit(0); //This is not final
        }
}

public Rectangle getBounds() {
return new Rectangle((int)x, (int)y, main.getWidth(), main.getHeight());
}
...

Edit: And if anyone cares… I’m using Eclipse KEPLER with the Dark UI mod.

Please post your code! We are going to need to see how you assign collision boxes to the zombies and player. Do you update the position of the collision boxes?

Added the code tht mattered to the collisions. If you need more ill just post the source. WARNING: IT IS REALLY MESSY

Try rendering your collision box, I suspect its not actually centered on the player. :o

EDIT: Or not centered on the zombies.

Good idea ray I’ll do it right now… I’ll post the result in a .gif

I suggest adding a debug mode to your game to toggle your collision boxes (or any other data you want) on keypress. Thats what I usually do, it’s really helpful :smiley:

Example:

in render();
(This is the code ripped right out of my game, so obviously it won’t all work for you. Just change everything in the IF statement to whatever you want to display)


		//If debug mode is turned on, render the following data.
		if(debugMode){
			font.drawString(5, 15, "FPS: " + gc.getFPS());
			font.drawString(5, 25, "Player X: " + playerX + " Y: " + playerY,0,255,0);
			font.drawString(5, 35, "Map X: " + mapX + " Y: " + mapY,0,0,255);
			font.drawString(5, 45, "Tile X: " + getPlayerTileLocationX() + " Y: " + getPlayerTileLocationY(),0,255,255);
			font.drawString(5, 55, "Map: " + getMapName(),255,255,0);
			font.drawString(5, 65, "Map Layer 0: " + getTileID(getPlayerTileLocationX(), getPlayerTileLocationY(), 0));
			font.drawString(5, 75, "Map Layer 1: " + getTileID(getPlayerTileLocationX(), getPlayerTileLocationY(), 1));
			font.drawString(5, 85, "Map Layer 2: " + getTileID(getPlayerTileLocationX(), getPlayerTileLocationY(), 2));
			font.drawString(5, 95, "Map Layer 3: " + getTileID(getPlayerTileLocationX(), getPlayerTileLocationY(), 3));
			font.drawString(5, 105, "Map Layer 4: " + getTileID(getPlayerTileLocationX(), getPlayerTileLocationY(), 4));
			font.drawString(5, 115, "Map Layer 5: " + getTileID(getPlayerTileLocationX(), getPlayerTileLocationY(), 5));
			font.drawString(5, 125, "Map Layer 6: " + getTileID(getPlayerTileLocationX(), getPlayerTileLocationY(), 6));
			font.drawString(5, 135, "Map Layer 7: " + getTileID(getPlayerTileLocationX(), getPlayerTileLocationY(), 7));
			font.drawString(5, 145, "Map Layer 8: " + getTileID(getPlayerTileLocationX(), getPlayerTileLocationY(), 8));
			font.drawString(5, 155, "Map Layer 9: " + getTileID(getPlayerTileLocationX(), getPlayerTileLocationY(), 9));
			font.drawString(5, 165, "Map Layer 10: " + getTileID(getPlayerTileLocationX(), getPlayerTileLocationY(), 10));
			font.drawString(5, 185, "Save Tick: " + saveTick);
			font.drawString(5, 205, mouseX + " " + mouseY);
			//Render collision boxes.
			for (int i = 0; i < COLLISION_ARRAY.size(); i++) {
				COLLISION_ARRAY.get(i).render(g);
			}
		}

then just under update(); just toggle the debug boolean true/false on a keypress

In update();
(NOTE: change SettingsParser.getKeyDebug to INPUT_)


			if(input.isKeyPressed(SettingsParser.getKeyDebug())){
				if (getDebugMode()){
					setDebugMode(false);
					input.clearKeyPressedRecord();
					return;
				}else{
					setDebugMode(true);
					input.clearKeyPressedRecord();
					return;
				}
			}

Alright the same thing happened. What do I do?

hrm… does this happen as soon as you release the key, or literally just after a few seconds?

Does any of the IF statements in your code that fire in your code have a return; statement? If you have a return; statement in your input code, it’ll override/ignore the collision detection if collision(); is under them but suddenly fire as soon as the key released. (it’s an odd glitch in slick I think)

We might need to see your input control code as well, or whatever is one step higher than the update() method you showed us.

What’s above tht is render… There are no return statements… I rlly just think is a lag thing… Imma just screw slick and go boilerplate again… Everything is harder but it seems to work better… Or maybe you could recommend a better library?

but something has to be turning on that “right” boolean? :o

Thts in the constructor. It basically checks what side of the player it on so tht I don’t have to make a right zombie and a left zombie

ohh, I see. This is code for the zombie. I thought this was attached to the player.

Well in that case, I am honestly clueless what is going on. You must have some wacky code somewhere you didn’t post, that or I’m having a blind-moment.

Does Slick use bottom-up coordinates, or top-down like J2D? If it uses bottom-up and you are using J2D rectangles, wouldn’t the rectangles then be in the ground? Just something to check. (Doubt it would cause this problem, but still check that)

Also, your getBounds() creates a rectangle object every call, probably not a great idea, esp. if you plan on having lots of zombies.

Unfortunately I don’t yet see why this is happening.

What is that main.update()?

Ya slick has it’s own rectangle class tht I assume is exactly like the J2D one. And yes I know. I had it in its own var but I changed back to the get bounds method to make it more readable.

Top (0) -> Bottom (Total Height)

Just store a private rect and return it in getBounds(). More readable and efficient.

Also, perhaps the zombie’s update() isn’t called often? What happens if the collision check is in the player class?

Also try a println() statement and watch for it instead of exit()ing if you haven’t, it might just be that the VM doesn’t exit right away.

I didn’t think of that. Thanks so much BurntPizza +1. I’ll do that after I’m done watching Harry Potter. :stuck_out_tongue: Thanks to you too ray, ur my go to guy when it comes to slick2D.

I tried the println method, it also has a lag time.

Yeah, it might take the JVM a few seconds to deallocate all the data and actually send the shutdown signal. Just to elaborate on what BurntPizza said.

Wouldn’t the game theoretically freeze while it’s doing that though?

[quote]It only happens when you walk directly towards the zombie though. Otherwise its quite responsive.
[/quote]
My guess is that some event handling or something is preventing the VM from exiting, aka while key is being held down for movement, but that’s just a hunch.