8 Replies - 458 Views - Last Post: 06 January 2013 - 07:53 AM Rate Topic: -----

#1 vladim0105  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 22-March 09

Collision Detection ArrayList problem

Posted 05 January 2013 - 04:59 AM

Hey!

I've run into a problem when detecting collision between the player and many of the same objects(in this case wall tiles). The problem is that my program only detects the collision when I'm in contact with the last added wall tile.

I am using rectangles(their intersects method) to detect collision.

Code samples:

Update tiles:
for (int i = 0; i < wallTileArr.size(); i++)
		{
			wallTile = wallTileArr.get(i);
			wallTile.update(p);
			if(wallTile.getBounds().intersects(Screen.getBounds()))
			{
				wallTile.draw = true;
			}
			else if(!wallTile.getBounds().intersects(Screen.getBounds()))
			{
				wallTile.draw = false;
			}
		}


Here is my class that makes a wall out of the tiles:

public Wall(int x, int y, int length, int dir, WallTile wallTile)
 {
	 this.x = x;
	 this.y = y;
	 this.length = length;
	 this.wallTile = wallTile;
	 
	 
		for(int i = 0; i < length; i++)
		{
			if (dir == 0)
			{
				Methods.addWallTile(new WallTile(x+(wallTile.size*i), y));
			}
			else if (dir == 1)
			{
				Methods.addWallTile(new WallTile(x, y+(wallTile.size*i)));
			}
			
			else if (dir != 0 || dir != 1)
			{
				System.out.println("Error in setting direction: 0 = side way, 1 = vertical. Your input is: "+dir );
				Methods.Exit(1);
			}

		}
 }



And here is my class that makes a room or house with walls:

public HouseSmall_1(int x, int y,int size, int rotation, WallTile wallTile)
	{
		this.x = x;
		this.y = y;
		this.wallTile = wallTile;
		this.size = size;
		
		
		if (rotation == 0)
		{
			Methods.addWall(new Wall(x, y, size, 1, wallTile));
			Methods.addWall(new Wall(x+(size*wallTile.size), y, size, 1, wallTile));
			
			Methods.addWall(new Wall(x, y, size, 0, wallTile));
			Methods.addWall(new Wall(x, y+(size*wallTile.size), (size)/2, 0, wallTile));
			Methods.addWall(new Wall(x+wallTile.size+((size*wallTile.size)/2), y+(size*wallTile.size),(size/2), 0, wallTile));
		}
		

		
		
	}


The code that adds the tiles and walls into their arrayList:
	public static void addWallTile(WallTile wallTile)
	{
		Controller.wallTileArr.add(wallTile);
	}
	
	public static void addWall(Wall wall)
	{
		Controller.wallArr.add(wall);
	}




Feel free to ask for more code samples!

Is This A Good Question/Topic? 0
  • +

Replies To: Collision Detection ArrayList problem

#2 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: Collision Detection ArrayList problem

Posted 05 January 2013 - 07:35 AM

Are you sure your collision code is iterating through each element? It may help to post that, because as far as I can see, your tiles should be getting added to the array.
Was This Post Helpful? 0
  • +
  • -

#3 vladim0105  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 22-March 09

Re: Collision Detection ArrayList problem

Posted 05 January 2013 - 10:56 AM

View PostMylo, on 05 January 2013 - 07:35 AM, said:

Are you sure your collision code is iterating through each element? It may help to post that, because as far as I can see, your tiles should be getting added to the array.



Update code for the wall tile which contains the collision detection:
	public void update(Player p)
	{
		x+=Screen.movementX;
		y+=Screen.movementY;
		
		if(p.upGetBounds().intersects(getBounds()))
		{
			
			p.walk = false;
			
		}
		if (!p.upGetBounds().intersects(getBounds()))
		{
			p.walk = true;
		}
	}

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: Collision Detection ArrayList problem

Posted 05 January 2013 - 11:21 AM

Why not ?

     if(p.upGetBounds().intersects(getBounds()))  
     {              
         p.walk = false;              
     }  
     else  
     {  
         p.walk = true;  
     }  
} 


or even more simply
         p.walk = !p.upGetBounds().intersects(getBounds());              



Is there a reason to call intersects() or getBounds() more than once ?
Was This Post Helpful? 0
  • +
  • -

#5 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: Collision Detection ArrayList problem

Posted 05 January 2013 - 11:41 AM

	x+=Screen.movementX;
	y+=Screen.movementY;


Why is the x and y values of your tiles being changed?

And I think the problem may be that in your update code, you set the walk variable to false when you intersect a wall. However, what then happens is the next tile is not colliding, and walk is now set to true.

This post has been edited by Mylo: 05 January 2013 - 11:48 AM

Was This Post Helpful? 0
  • +
  • -

#6 vladim0105  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 22-March 09

Re: Collision Detection ArrayList problem

Posted 05 January 2013 - 11:51 AM

View PostMylo, on 05 January 2013 - 11:41 AM, said:

And I think the problem may be that in your update code, you set the walk variable to false when you intersect a wall. However, what then happens is the next tile is not colliding, and walk is now set to true.


Do you have any suggestions on how to fix that?
Was This Post Helpful? 0
  • +
  • -

#7 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: Collision Detection ArrayList problem

Posted 05 January 2013 - 11:56 AM

I'm no game programmer, but I'd suggest taking an approach where you check for a collision before the character moves. E.g If there will be a block in the way if I move in direction X, don't move me, or at least move me to the edge of the tile.
Was This Post Helpful? 0
  • +
  • -

#8 vladim0105  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 22-March 09

Re: Collision Detection ArrayList problem

Posted 06 January 2013 - 05:39 AM

Fixed it!

Asked on stack overflow and this is what they said:

if(getBounds().intersects(p.upGetBounds()))
{

    p.walk = false;

}
else
{
    p.walk = true;
}


The problem is this code. You are resetting the flag for every tile that is NOT colliding with the player, and thus only last tile can collide successfully with the player and not get the flag reset.

i.e - Lets say there are 2 tiles and #1 is colliding and #2 is not.

In first iteration of the loop -

#1 is colliding => p.walk = false;

In second iteration of the loop -

#2 is NOT colliding => p.walk = true; //value has been reset.

However if #2 was the one coliding, the flag cannot get reset.

To fix : remove the else clause and reset it once to true before the for loop.
Was This Post Helpful? 0
  • +
  • -

#9 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: Collision Detection ArrayList problem

Posted 06 January 2013 - 07:53 AM

Isn't that exactly what I said? :plain:

I never thought to do that as your solution though. I'll remember that.

And for future reference, cross posting like that is frowned upon as it wastes effort answering questions between the two communities.

This post has been edited by Mylo: 06 January 2013 - 07:56 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1