Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Collision Intersection causes movement to stop

$
0
0
I'm having an issue with collision detection, where when I run into walls I can no longer move. I know this is because I'm still "Intersecting" with the collided object, but I don't know how to solve the issue... Any enlightenment is appreciated.

Here is the code, the solution I tried to add is below in a separate code block.

@Override
	public void update(GameContainer gc, int in) throws SlickException {
		
		Input input = gc.getInput();
		if(input.isKeyDown(Input.KEY_W)){
			sprite = up;
			key = "W";
			
			if (!entityCollisionWith()){
				sprite.update(in);
				dudeY -= in * 0.1f;
				dude.setY(dudeY);
			}
		}
		if(input.isKeyDown(Input.KEY_S)){
			sprite = down;
			key = "S";
			
			if (!entityCollisionWith()){
				sprite.update(in);
				dudeY += in * 0.1f;
				dude.setY(dudeY);
			}
		}
		if(input.isKeyDown(Input.KEY_A)){
			sprite = left;
			key = "A";
			
			if (!entityCollisionWith()){
				sprite.update(in);
				dudeX -= in * 0.1f;
				dude.setX(dudeX);
			}
		}
		if(input.isKeyDown(Input.KEY_D)){
			sprite = right;
			key = "D";
			
			if(!entityCollisionWith()){
				sprite.update(in);
				dudeX += in * 0.1f;
				dude.setX(dudeX);
			}
		}
		
		
	}
	
	public boolean entityCollisionWith() throws SlickException {
		for (int i = 0; i < BlockMap.entities.size(); i++) {
			Block entity1 = (Block) BlockMap.entities.get(i);
			if (dude.intersects(entity1.poly)) {
				return true;
			}       
		}       
		return false;
	}


My solution. This makes some very interesting movement problems...

if(!entityCollisionWith()){
     sprite.update(in);
     dudeX += in * 0.1f;
     dude.setX(dudeX);
}
else{
     dudeX --;
     dude.setX(dudeX);
}


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>