5 Replies - 1452 Views - Last Post: 03 March 2013 - 07:28 AM Rate Topic: -----

#1 thig1002  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 30-January 13

Collision Intersection causes movement to stop

Posted 28 February 2013 - 01:54 PM

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);
}



Is This A Good Question/Topic? 0
  • +

Replies To: Collision Intersection causes movement to stop

#2 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Collision Intersection causes movement to stop

Posted 28 February 2013 - 03:17 PM

You need to do something once a collision is registered, if you don't it gets stuck.

This post has been edited by ButchDean: 28 February 2013 - 03:18 PM

Was This Post Helpful? 0
  • +
  • -

#3 LiberLogic969  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 34
  • View blog
  • Posts: 85
  • Joined: 03-September 12

Re: Collision Intersection causes movement to stop

Posted 01 March 2013 - 04:10 AM

Maybe You could try making a variable that holds the previous dudeX value from the last frame and when a collision is triggered set the dudeX variable to the previous DudeX value.
Was This Post Helpful? 0
  • +
  • -

#4 thig1002  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 30-January 13

Re: Collision Intersection causes movement to stop

Posted 01 March 2013 - 09:52 AM

Previous Position would work, only I would still get the bounce I think....

What about this? I can't add this to the code right now, but I will as soon as it's in front of me.

if(input.isKeyDown(KEY_W){

   sprite = up;
   key = "W";
   sprite.update(in);
   dudeY -= in * 0.1f;
   dude.setY(dudeY);

   if(entityCollisionWith()){
      dudeY += in * 0.1f;
      dude.setY(dudeY);
   }
}


Was This Post Helpful? 0
  • +
  • -

#5 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 841
  • View blog
  • Posts: 2,576
  • Joined: 29-July 11

Re: Collision Intersection causes movement to stop

Posted 03 March 2013 - 06:46 AM

I know I am a little late on this, but what you need to do is place your player one pixel outside of the wall when a collision occurs. No one would notice the bounce for 1 pixel.
Was This Post Helpful? 0
  • +
  • -

#6 thig1002  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 30-January 13

Re: Collision Intersection causes movement to stop

Posted 03 March 2013 - 07:28 AM

Thanks for the replies. The code I suggested actually works. There is no bounce (because you only move 1 pixel), and the animation continues.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1