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: Trouble with Object Collision

  1. #1
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Trouble with Object Collision

    I'm trying to make objects of the same class collide with each other. Unfortunately, they only will collide with the last object created of the class.

    Movement Code:
    canMove=true;
    		for(int i=0;i<Level.People.size();i++){
    			if(Level.People.get(i).x+Frame.sx+32>x&&Level.People.get(i).x+Frame.sx<x+width&&Level.People.get(i).y+Frame.sy+32>y&&Level.People.get(i).y+Frame.sy<y+32){
    			canMove=false;
    			Level.People.get(i).canMove=false;
    			}else{
    				canMove=true;
    			}
    		}
    		if(canMove){
    		if(destx>x+moveSpeed){
    			x+=moveSpeed;
    		}else if(destx<x-moveSpeed){
    			x-=moveSpeed;
    		}else{
    			x=destx;
    		}
     
    		if(desty>=y+moveSpeed){
    			y+=moveSpeed;
    		}else if(desty<=y-moveSpeed){
    			y-=moveSpeed;
    		}else{
    			y=desty;
    		}
    	}else{
    		if(destx>x+moveSpeed){
    			x-=moveSpeed-1;
    			canMove=true;
    			destx=x;
    		}else if(destx<x-moveSpeed){
    			x+=moveSpeed+1;
    			destx=x;
    			canMove=true;
    		}else{
     
    		}
     
    		if(desty>=y+moveSpeed){
    			y-=moveSpeed-1;
    			desty=y;
    			canMove=true;
    		}else if(desty<=y-moveSpeed){
    			y+=moveSpeed+1;
    			desty=y;
    			canMove=true;
    		}else{
     
    		}
    	}
    	}
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,094
    Thanks
    48
    Thanked 2,152 Times in 2,125 Posts

    Default Re: Trouble with Object Collision

    Sounds like the list of current objects is missing some entries if only the last object is used for testing for a collision.
    Another possibility is that there is only one object and the references in the list all point to the same object.

    Without a small complete program that compiles, executes and shows the problem, there is no way to test.

    Try debugging the code by adding lots of printlns to print out the values of variables that are used to detect a collision to see what the code is doing and why it is working the way it is.


    BTW The code is poorly formatted making it hard to read. There should be nesting for code inside of if statements.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Having trouble with setting an array object
    By johnnyDidge in forum Object Oriented Programming
    Replies: 0
    Last Post: October 9th, 2012, 03:01 PM
  2. Trouble with Collision Detection
    By Gravity Games in forum What's Wrong With My Code?
    Replies: 11
    Last Post: September 6th, 2012, 02:00 PM
  3. Replies: 3
    Last Post: July 12th, 2012, 07:11 AM
  4. Need help with object collision
    By xDeadScYthe in forum AWT / Java Swing
    Replies: 3
    Last Post: March 23rd, 2012, 07:10 AM
  5. Having trouble printing object information in main class
    By KingLane in forum Object Oriented Programming
    Replies: 1
    Last Post: October 11th, 2009, 06:53 PM