Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Collision Detection Problem Using Rectangles - Player sometimes passes through the tiles.

  1. #1
    Junior Member RKJ (linktoinfinity)'s Avatar
    Join Date
    May 2014
    Posts
    1
    My Mood
    Tired
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Collision Detection Problem Using Rectangles - Player sometimes passes through the tiles.

    I have already tried every possible way for removing this bug but all failed. The problem is that sometimes my player passes through the map tiles, i mean, the algorithm is like -- if not colliding, move in required direction; else if colliding, move in opposite direction. But sometimes the second condition fails i dont understand why.

    private void movement(){
    	left=bindObject.getLeft();
    	right=bindObject.getRight();
    	up=bindObject.getUp();
    	down=bindObject.getDown();
    	bindObject.setColliding(colliding);
    	lastHorz=bindObject.getLastHorz();
    	lastVert=bindObject.getLastVert();
     
    	//IF NOT COLLIDING
    	if(colliding==false){
    		if(left==true){
    			x--;
    		}
    		else if(right==true){
    			x++;
    		}
    		if(up==true){
    			y--;
    		}
    		else if(down==true){
    			y++;
    		}
    		if(left==false && right==false){
    			bindObject.setLastHorz(null);
    		}
    		if(up==false && down==false){
    	         	bindObject.setLastVert(null);
    		}
    	}
     
    	//IF COLLIDING
    	if(colliding==true){
    		if(lastHorz=="l" && lastVert==null){
     
    		        x++;
    		}
    		else if(lastHorz=="r" && lastVert==null){
    			x--;
    		}
    		else if(lastVert=="u" && lastHorz==null){
    			y++;
    		}
    		else if(lastVert=="d" && lastHorz==null){
    			y--;
    		}
     
    		else if(lastHorz=="l" && lastVert=="u" && y>=(int)cRec.getY()+49){
    			y++;
    		}
    		else if(lastHorz=="l" && lastVert=="u" && y<(int)cRec.getY()+49){
    			x++;
    		}
     
    		else if(lastHorz=="r" && lastVert=="u" && y>=(int)cRec.getY()+49){
    			y++;
    		}
    		else if(lastHorz=="r" && lastVert=="u" && y<(int)cRec.getY()+49){
    			x--;
    		}
     
    		else if(lastHorz=="l" && lastVert=="d" && y<=(int)cRec.getY()-49){
    			y--;
    		}
    		else if(lastHorz=="l" && lastVert=="d" && y>(int)cRec.getY()-49){
    			x++;
    		}
     
    		else if(lastHorz=="r" && lastVert=="d" && y<=(int)cRec.getY()-49){
    			y--;
    		}
    		else if(lastHorz=="r" && lastVert=="d" && y>(int)cRec.getY()-49){
    			x--;
    		}
    	}
     
    }
     
    private void collision(){
    	for(Rectangle temp: tileList){
    		if(player.intersects(temp)){
    			colliding=true;
    			cRec=temp;
    			break;
    		}
    	}
    	if(cRec!=null){
    		if(player.intersects(cRec)==false){
    			colliding=false;
    		}
    	}
    }

    , where bindObject is a class which contains key bindings of following keys:-

    left arrow key (when pressed) , left arrow key (when released),
    right arrow key (when pressed), right arrow key (when released),
    up arrow key (when pressed), up arrow key (when released),
    down arrow key (when pressed), down arrow key (when released)
    Last edited by RKJ (linktoinfinity); May 20th, 2014 at 05:01 AM. Reason: more details


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,358
    Thanks
    182
    Thanked 834 Times in 777 Posts
    Blog Entries
    5

    Default Re: Collision Detection Problem Using Rectangles - Player sometimes passes through the tiles.

    Please see Announcements - What's Wrong With My Code? for useful information, including how to use the code tags to preserve code formatting. Given your code is difficult to read as currently posted, a quick glance shows issues such as

    lastVert=="d"

    Presuming lastVert is a String type, use the equals method of the String class to compare Strings. See Common Java Mistakes

  3. The Following User Says Thank You to copeg For This Useful Post:

    RKJ (linktoinfinity) (May 20th, 2014)

Similar Threads

  1. Collision Detection
    By Jobrien15 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 17th, 2013, 09:00 PM
  2. Collision Detection
    By Jobrien15 in forum Java Programming Tutorials
    Replies: 1
    Last Post: October 2nd, 2013, 09:46 AM
  3. Collision detection
    By Cutupangels in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 10th, 2013, 08:48 PM
  4. As my player goes lower on-screen my tiles become smaller in height? D:
    By WolfNinja2 in forum What's Wrong With My Code?
    Replies: 36
    Last Post: December 23rd, 2012, 09:25 PM
  5. 2D Collision Detection
    By Cuju in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 3rd, 2010, 11:39 AM