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:
Code Java:
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{
}
}
}
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.