1 Replies - 1787 Views - Last Post: 05 April 2012 - 02:51 PM Rate Topic: -----

#1 CreativiaGames   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 05-April 12

I have a problem with the collision in my 2D tile map collision

Posted 05 April 2012 - 12:28 PM

Hello,

I have come here in hope of getting a problem fixed or get given advice on a problem i am having on a tile engine (map) i have been making with java and lwjgl.

My problem is that when i have one collision tile on the map my collision works fine but when i have 2 or more collision tiles on the map my player doesn't collide with any of them he just slows down.

I have run through the game numerous times in debug mode and i have found out what is causing my problem. Because i am iterating through my collision tiles like so

public void checkMapCollision(){
        for(Collision collision : _worldObjects._map._collisionLayer.c)
        Camera.checkTileCollision( collision.rect,_entityObjects._player.rect);
    }
}



and because it is iterating through the tiles it runs the method Camera.checkTileCollision once for one tile then once for the other tile then once for one tile then once for the other tile which completely foils my collision code which i will post below

This is my Camera.java which holds the collision detection code:

package org.camera;

import java.awt.Rectangle;

public class Camera {

	public static boolean cantMoveRight, cantMoveLeft, cantMoveUp, cantMoveDown;
	public static int rv, lv, uv, dv;

	public static void checkTileCollision(Rectangle r, Rectangle rr) {
		if (r.intersects(rr)) {
			
			if (rv > 0) {
				cantMoveRight = true;
			}

			if (lv > 0) {
				cantMoveLeft = true;
			}

			if (uv > 0) {
				cantMoveUp = true;
			}

			if (dv > 0) {
				cantMoveDown = true;
			}
			
			if(cantMoveRight)
				rv = 0;
			else
			if(cantMoveLeft)
				lv = 0;
			
			if(cantMoveUp)
				uv = 0;
			else
			if(cantMoveDown)
				dv = 0;
			
		}else
		{
			cantMoveRight = false;
			cantMoveLeft = false;
			cantMoveUp = false;
			cantMoveDown = false;
		}
	}

}



Hope i can get some help or advice:)

Thanks,
Dennis

Is This A Good Question/Topic? 0
  • +

Replies To: I have a problem with the collision in my 2D tile map collision

#2 anonymous26   User is offline

  • D.I.C Lover

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

Re: I have a problem with the collision in my 2D tile map collision

Posted 05 April 2012 - 02:51 PM

As vague as this may sound, your code effectively checks for collisions against a pair of objects rather than more than two objects. You can fix this by having objects in the vicinity of the player check that a collision has been made against them by the player, rather than the player checking for a collision against everything else. Can you see what I mean?
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1